Replies: 1 comment
|
Treat the HTTP connection pool and the logical MCP client/session as different lifetimes. Reuse sockets, but do not keep one startup-wide McpClient and replace its bearer token for whichever user happens to make the next request. There are two reasons:
A safe shape is:
Key caches by stable subject + tenant + MCP audience, never by a caller-supplied token string, and do not share one stateful MCP session across subjects. IHttpClientFactory still gives connection pooling, so per-user logical clients do not require a new TCP connection for every tool call. If the token expires, acquire a fresh downstream token; for a stateful session, keep the authenticated subject invariant. On the server, use ordinary ASP.NET Core JWT bearer authentication, MapMcp().RequireAuthorization(), and AddAuthorizationFilters() when tools/resources have authorization metadata. Tool handlers can receive ClaimsPrincipal directly. Current Streamable HTTP handling uses fresh HttpContext authentication on each request; legacy SSE has a stale-context caveat because its HttpContext belongs to the long-lived stream. Current SDK references:
So the short answer is: reuse the underlying HttpClient infrastructure, but scope the McpClient/session and downstream credential to one authenticated principal. |
Uh oh!
There was an error while loading. Please reload this page.
Pre-submission Checklist
Question Category
Your Question
Let's say I have:
When the WebAPI starts, I also connect to the MCP Server so that the tool calls are faster.
Now, when the user make a requests to the WebAPI, I want to use the "access token" (JWT) to call the MCP Server so that the tools calls authorize the user.
I have some ideas, but I wonder whether there is some "best practice" to follow, without reinventing the wheel.
Thanks
All reactions