Skip to content

Conversation

@ElectricCookie
Copy link
Contributor

I wanted to be able to have a way to inject some logic into the tool calling at runtime, (my use case was confirming the use of certain tools).

This PR adds the ability to inject Middleware into the agent loop to control the usage of tools.

Middleware can intercept the tool call, pre and post process it.

The implementation allows for chaining multiple middlware(s? :D).

@csells
Copy link
Owner

csells commented Dec 31, 2025

That is super interesting! Can I ask for an example and a new docs page, too? Thank you for this!

1 similar comment
@csells
Copy link
Owner

csells commented Dec 31, 2025

That is super interesting! Can I ask for an example and a new docs page, too? Thank you for this!

@ElectricCookie
Copy link
Contributor Author

I'm going to play with this in my app for a bit and see if the API I defined works out when using it. The most simple middlware example:

 final agent = ai.Agent(
    "provider:model",

    tools: [...],
    middleware: [
      FunctionToolMiddleware((toolPart, tool, next) async {
        print('Logging Middleware - toolPart: ${toolPart.toJson()}');
        print('Logging Middleware - Tool: ${tool?.name}');
        final result = await next();
        print('Logging Middleware - Result: ${result}');
        return result;
      }),
    ],
  );

FunctionToolMiddleware is just a helper class implementation of the abstract middlware class, to inline a middlware in a functional style. This might not always be the cleanest approach in case you have some dependency injection or complicated logic :) therefore the pattern also allows extending the middleware class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants