-
Notifications
You must be signed in to change notification settings - Fork 670
feature Add methods to McpTransportContext #570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feature Add methods to McpTransportContext #570
Conversation
This change adds methods to McpTransportContext: `protocolVersion`, `lastEventId`, `sessionId`, and `principal`. It also provides an implementation of Context extract for Servlet requests, which uses the HTTP Headers defined in the specification.
mcp-core/src/main/java/io/modelcontextprotocol/server/McpTransportContextExtractor.java
Outdated
Show resolved
Hide resolved
…portContextExtractor.java
* @return Extracts Map for MCP Transport Context | ||
*/ | ||
protected Map<String, Object> metadata(HttpServletRequest request) { | ||
Map<String, Object> metadata = new HashMap<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be worth passing a size to this hash map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added f02ec6f
…ocol/server/transport/ServerRequestMcpTransportContextExtractor.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. Are these methods of any use for the client-side case (e.g. McpAsyncHttpClientRequestCustomizer
)? The McpTransportContext
is used both in the server and in the client scenarios. Also, coupling McpTransportContext
with java.security.Principal
feels undesired for a general-purpose map-like container.
I am not against the utility of providing a unified context extractor that addresses common server-side concerns, but I don't think this is the right implementation.
Having in mind the client-side and the overfitting aspect of the basic interface, do you think providing a helper proxy for working with the McpTransportContext
as an argument would be useful instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see how that is useful on the server side.
However, initially, the McpTransportContext
was introduced as a very thin, immutable data object (see its javadoc):
/**
* Context associated with the transport layer. It allows to add transport-level metadata
* for use further down the line. Specifically, it can be beneficial to extract HTTP
* request metadata for use in MCP feature implementations.
*
* @author Dariusz Jędrzejczyk
*/
As such, it is used both in the server transports, to capture HTTP request information; as well as the client side to capture "context" information (notably thread-locals), and make that available inside the reactor context. See McpSyncClient and then subsequent usages in transports (e.g. HttpClientStreamableHttpTransport).
On the client side, all these new fields would be null. On the server side, when using stateless transport, I think the sessionId
and lastEventId
are also always null. Maybe the core McpTransportContext
interface is not good candidate to add those fields, at least not in the current shape.
On the Spring side, we indeed need the principal
when authorization is in use, but that's the only element we would be using ; we have extra needs (servlet request & response objects on the client side).
We have not provided McpTransportContextExtractor
in the SDK for now, apart from your previous request #492, there seems to be no demand for this so far.
Should we postpone this until we have a clearer view of the community needs?
mcp-core/src/main/java/io/modelcontextprotocol/server/servlet/package-info.java
Outdated
Show resolved
Hide resolved
mcp-core/src/test/java/io/modelcontextprotocol/common/DefaultMcpTransportContextTest.java
Show resolved
Hide resolved
...modelcontextprotocol/server/servlet/HttpServletRequestMcpTransportContextExtractorTests.java
Show resolved
Hide resolved
...java/io/modelcontextprotocol/server/transport/ServerRequestMcpTransportContextExtractor.java
Outdated
Show resolved
Hide resolved
...a/io/modelcontextprotocol/server/servlet/HttpServletRequestMcpTransportContextExtractor.java
Outdated
Show resolved
Hide resolved
…package-info.java Co-authored-by: Daniel Garnier-Moiroux <[email protected]>
…HttpServletRequestMcpTransportContextExtractor.java Co-authored-by: Daniel Garnier-Moiroux <[email protected]>
perhaps what can be done is a sub-interface of |
Sorry, but I don't understand the pushback. The methods I added are transport concepts - protocol version , session id, Last event Id. Should not transport concepts be represented in Transport Context ( The same with The API return types are optional, and the API methods are default methods with empty options. As an implementer of the context API you don't have to do anything. If you use the context in the client and it doesn't apply to you, don't use these methods. They don't require extra work on the client side. as @Kehrlann pointed out in the javadocs of
That it is exactly what I am doing in If we don't add methods to the |
I add a commit which hopefully illustrates the benefit of this PR Now, whether you use a |
Thinking about it a bit more, there are further concerns. You can implement, say, a tool. And in your implementation you can get the Even in the HTTP context, you are suggesting we add Streamable HTTP concepts to the generic transport context, but e.g. Last-Event-Id does not belong to the legacy MCP SSE spec (it is not specified in the context of MCP despite being part of the generic SSE specification). Not to mention these are probably of no use on the client side. What I'm trying to say is that in order to work with a particular transport, you can create the abstraction about what the tool might need, e.g. an interface called On the transport provider side, a companion As the instantiator of the particular transport implementation, e.g. Let's discuss. Regarding |
Motivation and Context
This change adds methods to McpTransportContext:
protocolVersion
,lastEventId
,sessionId
, andprincipal
. It also provides an implementation of Context extract for Servlet requests, which uses the HTTP Headers defined in the specification.It aims to provide some structure to the
McpTransportContext
API.How Has This Been Tested?
Yes, the change includes tests.
Breaking Changes
No
Types of changes
Checklist
Additional context