modelcontextprotocol / modelcontextprotocol/csharp-sdk

Pass context from request endpoint to message handler

Open
#236 2 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement needs confirmation
Dominant language
C#
Stars
4.5k
Forks
814
Avg merge
9d 19h
Merged PRs (30d)
4

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at McpEndpointRouteBuilderExtensions and McpServer, tracing OnMessageReceivedAsync and the RequestHandlers.Set call shown in the issue. Determine how an opaque caller context can travel with the IJsonRpcMessage into the tool handler or MCP request context. Done means endpoint-supplied context is available during long-running tool calls without storing or passing the completed HttpContext.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.