-
|
I have a case where I need to send some additional data (e.g. context data) along with tool arguments to MCP servers' tool calls using dotnet. The data I want to send is not static, it might change per request so I cannot send as HTTP headers because there isn't a way to set HTTP headers per tool call request as far as I know. Is there a way to handle this? I saw #2290 and tried to write a custom middleware where I intercept tool calls to modify the request, but could not found a way to pass the additional data I want to send to middleware or a property in the request object to set the values to MCP tool call. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
|
For anybody wondering, I have found a solution by writing an McpClientTool wrapper that implements the AIFunction class and setting the values I want to send to ModelContextProtocol.RequestOptions' Meta object. During agent initialization wrap your existing MCP tools with this class. class CustomTool(McpClientTool tool) : AIFunction
{
private readonly McpClientTool _internalTool = internalTool;
protected override async ValueTask<object?> InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken)
{
var meta = new JsonObject();
meta.Add("key1", JsonValue.Create("value1"));
var requestOptions = new ModelContextProtocol.RequestOptions { Meta = meta };
return await _internalTool.CallAsync(arguments, options: requestOptions, cancellationToken: cancellationToken);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Hi, I had a similar request and discussion here: modelcontextprotocol/modelcontextprotocol#1701. There seems to be no valid solution yet, and I'm "happy" to see that I'm not the only one experiencing this requirement. Though this workaround with CustomTool and meta works, I was/am hoping for an official and better solution that more tools can support as it is part of the protocol. Willing to discuss and follow up on this .... |
Beta Was this translation helpful? Give feedback.
For anybody wondering, I have found a solution by writing an McpClientTool wrapper that implements the AIFunction class and setting the values I want to send to ModelContextProtocol.RequestOptions' Meta object. During agent initialization wrap your existing MCP tools with this class.