fix(mcp): honor custom endpoint in StreamableHttpServerParameters#1204
Open
YuqiGuo105 wants to merge 1 commit into
Open
fix(mcp): honor custom endpoint in StreamableHttpServerParameters#1204YuqiGuo105 wants to merge 1 commit into
YuqiGuo105 wants to merge 1 commit into
Conversation
When a user passed a URL with a custom sub-path to
StreamableHttpServerParameters (e.g. http://localhost:8080/mcp/stream),
the MCP library's Utils.resolveUri() would call Java's URI.resolve() with
the default endpoint '/mcp', which—per RFC 3986—replaces the entire path,
producing http://localhost:8080/mcp instead of the intended URL. This
caused every request to return 404 if the server was not exposed on /mcp.
Fix:
- Add an optional endpoint field to StreamableHttpServerParameters
(and its Builder) that mirrors the pattern of SseServerParameters.sseEndpoint.
- In DefaultMcpTransportBuilder, pass the base URL (host only) to the
library builder and, when the user has set an endpoint, call
HttpClientStreamableHttpTransport.Builder.endpoint() to set the path
separately. This way resolveUri(baseUri, endpoint) works correctly.
- When no endpoint is provided the library default ('/mcp') is preserved,
keeping full backward compatibility.
Fixes google#1196
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1196
When a user passed a full URL with a custom sub-path to
StreamableHttpServerParameters(e.g.http://localhost:8080/mcp/stream), the MCP library'sUtils.resolveUri()would call Java'sURI.resolve()with the default endpoint"/mcp". Per RFC 3986, when the second argument is an absolute path (starts with/), it replaces the entire path of the base URI. The result was alwayshttp://localhost:8080/mcp— the custom sub-path was silently dropped — causing a404 Not Foundon every request against servers not exposed on/mcp.Root Cause
HttpClientStreamableHttpTransportinternally stores abaseUriand anendpoint, and usesUtils.resolveUri(baseUri, endpoint)for every request. The defaultendpointis"/mcp". When the full custom URL was passed as thebaseUri,URI.resolve("/mcp")stripped the custom suffix:Fix
endpointfield (and matchingBuilder.endpoint()setter) toStreamableHttpServerParameters, following the same pattern asSseServerParameters.sseEndpoint.DefaultMcpTransportBuilder, the base URL (host only, e.g.http://localhost:8080) is passed toHttpClientStreamableHttpTransport.Builder, and when the user has provided a custom endpoint path, it is passed to the library's own.endpoint()method.This makes
resolveUriwork correctly:endpointis set, the library's default"/mcp"is preserved — full backward compatibility.Changes
core/src/main/java/…/mcp/StreamableHttpServerParameters.javaendpointfield, constructor param, getter, andBuilder.endpoint()core/src/main/java/…/mcp/DefaultMcpTransportBuilder.java.endpoint()on the transport buildercore/src/test/…/mcp/StreamableHttpServerParametersTest.javacore/src/test/…/mcp/DefaultMcpTransportBuilderTest.javaTesting
All 22 new tests pass. Existing core tests unaffected.