Repository navigation
Request-scoped _meta on MCP tool calls from chat()
#2100
OAmsalem2002
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Problem
I use
chat({ mcp })with a sharedcreateMCPClientspool, created once at module load. I need to send per-request context to MCP servers on each tool call: tenant and user ids, trace ids, and structured data.Today the only option is headers via a custom transport:
Putting the values in tool args is worse: the model sees them and can change them.
MCP's request
_metasolves this on every transport, because it is part of the JSON-RPC params.Option A:
toolCallMetaonchat({ mcp })chat()runs once per request, so this is request-scoped with no extra plumbing, even on a shared pool.serverIdandtoolName, so the caller can send context only to the servers that should get it. A pool can mix internal and third-party servers.undefined, or a Promise of either.progressToken, are not overwritten.Option B:
onBeforeToolCallreturnsmetaLet the existing middleware hook return
metafor MCP tools. This reuses an existing API, and because middleware runs for every tool call, it also covers manual mode. LangChain JS took a similar path in@langchain/mcp-adapters.The main open question: today the first middleware that returns a decision from
onBeforeToolCallends the chain for that call. Ametadecision must not do that, or it would block later middleware such astoolCacheMiddleware(or be blocked by it). Sometawould need to be a non-terminal decision. Riding ontransformArgswouldn't avoid this on its own, sincetransformArgsalso ends the chain. A separate piped hook (likeonConfig) is another option. It also puts MCP-specific behaviour in core middleware.Things I've thought about
onBeforeToolCall. AtoolCacheMiddlewarehit skips the call, so no meta is computed.keyFn. I'd document this.Client.callTool, whose params accept_meta, so there's a natural place to pass it through.Out of scope (for now)
tools: [...await mcp.tools()]):chat()never sees the MCP config there.tools/listmeta: discovery may be cached or shared across runs, so request-scoped meta could leak between runs.chat()doesn't request them.Questions
toolCallMetaOK?Happy to go whichever direction you prefer.
All reactions