Pass context from request endpoint to message handler
还没有人认领这个 Issue。
评估
调研方向
从 McpEndpointRouteBuilderExtensions 和 McpServer 开始,跟踪 OnMessageReceivedAsync 以及 issue 中所示的 RequestHandlers.Set 调用。确定如何让不透明的调用方上下文与 IJsonRpcMessage 一起传递到工具处理程序或 MCP 请求上下文中。在长时间运行的工具调用期间可以使用端点提供的上下文,且无需存储或传递已完成的 HttpContext,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Is your feature request related to a problem? Please describe.
When the server is started, it runs in a scope that has the current HttpContext. In that context there might be useful headers that are relevant for that session. But when you need to call a tool the message endpoint might have a different HttpContext and different headers.
For example you might want to use the bearer token from the message endpoint to call a tool or to get the list of tools. In the case of SSE transport, you will need to transfer this token to the tool handler.
Describe the solution you'd like
Be able to build an object (could be opaque) on the message endpoint (can contain anything you want, you can include headers or other info) and pass that object alongside with the IJsonRpcMessage to the tool hander. Maybe make it available on the mcp request context.
Example call:
McpEndpointRouteBuilderExtensions:
await transport.OnMessageReceivedAsync(message, callerContext, context.RequestAborted);
McpServer:
RequestHandlers.Set(
RequestMethods.ToolsCall,
(request, callerContext, cancellationToken) => callToolHandler(new(this, request, callerContext), cancellationToken),
McpJsonUtilities.JsonContext.Default.CallToolRequestParams,
McpJsonUtilities.JsonContext.Default.CallToolResponse);
This could be extended to other requests as well.
Describe alternatives you've considered
Storing the context in memory introduces additional overhead since it has to be cleaned up and introduces unneeded complexity.
Storing the context in a cache is not ok because we might want to pass sensitive information.
Passing the http context directly is not ok because the post /message request could be finished by the time the tool call is actually processed.
Additional context
In this case we have tool calls with long timeout, they might take a long time to complete, in the order of minutes at least.
- 主要语言
- C#
- 星标
- 4.5k
- 派生
- 814
- 平均合并
- 9 天 19 小时
- 30 天内合并 PR
- 4
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/csharp-sdk 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
modelcontextprotocol/csharp-sdk#1867 ·
-
难度 1/5 1 小时以内 新手友好度 88/100
modelcontextprotocol/csharp-sdk#1840 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
modelcontextprotocol/csharp-sdk#1836 ·
-
enhancement needs confirmation
难度 2/5 1-3 小时 新手友好度 64/100
modelcontextprotocol/csharp-sdk#678 · 1 条评论 ·
-
enhancement needs confirmation P3 ready for work
难度 2/5 1-3 小时 新手友好度 68/100
modelcontextprotocol/csharp-sdk#515 · 6 条评论 · 3 个 reaction ·
查看 modelcontextprotocol/csharp-sdk 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
难度 2/5 1-3 小时 新手友好度 92/100
dotnet/AspNetCore.Docs#37699 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
SubtitleEdit/subtitleedit#15108 · 1 条评论 ·
-
area/docs-content Bug pulumi/docs
难度 1/5 1-3 小时 新手友好度 94/100
-
agentic-workflows untriaged
难度 2/5 1-3 小时 新手友好度 76/100