Pass context from request endpoint to message handler

Aperta
#236 2 commenti 8 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
32/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
csharp
Ambito
api, backend

Direzione di ricerca

Parti da McpEndpointRouteBuilderExtensions e McpServer, seguendo OnMessageReceivedAsync e la chiamata a RequestHandlers.Set mostrata nell’issue. Determina come un contesto opaco del chiamante possa viaggiare insieme a IJsonRpcMessage fino all’handler dello strumento o al contesto della richiesta MCP. Il lavoro è completato quando il contesto fornito dall’endpoint è disponibile durante le chiamate agli strumenti di lunga durata senza memorizzare o passare l’HttpContext completato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement needs confirmation

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.

Lingua principale
C#
Stelle
4.5k
Fork
814
Merge medio
9g 19h
PR unite (30g)
4

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/csharp-sdk

Tutte le issue di modelcontextprotocol/csharp-sdk

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.