Do exist any way to add a middleware to "count" all tool invokations ? #1220
Replies: 1 comment
|
For the current 2.x SDK, builder.Services
.AddMcpServer()
.WithRequestFilters(filters =>
{
filters.AddCallToolFilter(next => async (context, cancellationToken) =>
{
var toolName = context.Params.Name;
var started = Stopwatch.GetTimestamp();
try
{
var result = await next(context, cancellationToken);
ToolCalls.Record(
1,
new KeyValuePair<string, object?>("mcp.tool.name", toolName),
new KeyValuePair<string, object?>("mcp.tool.error", result.IsError is true));
ToolDuration.Record(
Stopwatch.GetElapsedTime(started).TotalMilliseconds,
new KeyValuePair<string, object?>("mcp.tool.name", toolName));
return result;
}
catch
{
ToolFailures.Add(1, new KeyValuePair<string, object?>("mcp.tool.name", toolName));
throw;
}
});
})
.WithTools<MyTools>();The ordinary call-tool filter runs exactly once before the registered tool. With the Tasks extension it still runs once, in the background after task creation. Configure Tasks before the ordinary filters so the documented ordering is preserved. If you need to observe all JSON-RPC traffic rather than only tool invocations, use Current filter documentation: One caution: tool arguments frequently contain credentials or user data. Count by bounded tool name and error state; avoid putting raw parameters, tenant IDs, or arbitrary error text into metric labels. If arguments are needed for an audit record, use explicit allowlisting/redaction and access-controlled logs rather than metrics. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Pre-submission Checklist
Question Category
Your Question
My goal is to accumulate statistics about tools usage server side.
I know i can use mpcBuilder.AddCallToolFilter() but this is a fallback called only if requested tool is not found as an already registered tool. What i really want is to monitor all tools invokation, possibly with name and parameters as well.
Do exist any way to be triggered when a tool_call is pushed to the server ?
All reactions