Skip to content

Fix nested path URI resolution - #974

Open
Qshuai0213 wants to merge 2 commits into
modelcontextprotocol:mainfrom
Qshuai0213:fix/resolve-uri-nested-paths
Open

Qshuai0213 wants to merge 2 commits into
modelcontextprotocol:mainfrom
Qshuai0213:fix/resolve-uri-nested-paths

Conversation

@Qshuai0213

@Qshuai0213 Qshuai0213 commented May 19, 2026 •

Copy link
Copy Markdown

Summary

Fixes #257

Utils.resolveUri incorrectly handled nested base paths — relative endpoints with a leading slash (e.g. /api/v1) would replace the base path entirely instead of being appended to it. This affected HttpClientSseClientTransport and HttpClientStreamableHttpTransport when the base URL had a non-root path (e.g. https://example.com/some).

Root cause

URI.resolve() follows RFC 3986 §5.2.3: when the relative reference starts with /, it replaces the entire path of the base URI. So URI.create("https://example.com/some").resolve("/path") yields https://example.com/path, dropping /some.

Fix approach

  • Ensure the base URL always has a trailing slash (e.g. /root → /root/)
  • Strip leading slashes from the endpoint (e.g. /api/v1 → api/v1)
  • URI.resolve() then performs path concatenation (/root/ + api/v1 = /root/api/v1), consistent with RFC 3986
  • Absolute endpoint validation behavior is preserved unchanged

Testing

  • ./mvnw.cmd -pl mcp-core -Dtest=UtilsTests test
  • ./mvnw.cmd -pl mcp-core test
  • Added parameterized test cases covering: nested base paths, leading/trailing slashes, and query strings

@Qshuai0213

Copy link
Copy Markdown
Author

@chemicL Hi, since you've looked into this issue in both #257 and #701, could you take a look at this PR when you have time? Happy to adjust the approach if needed. Thanks!

@RyanxLowz

Copy link
Copy Markdown

I pulled this branch (3ac51071) and ran it locally. UtilsTests passes (13/13), but I think the change to leading-slash handling breaks some existing setups. resolveUri is used by both HTTP client transports, not only for the SSE endpoint.

Here's what I get calling Utils.resolveUri directly, main vs. this PR:

baseUri endpoint main this PR
http://localhost:8080/mcp /mcp (Streamable HTTP default) http://localhost:8080/mcp http://localhost:8080/mcp/mcp
http://localhost:8080/api /api/messages?sessionId=1 (SSE endpoint event with full path) http://localhost:8080/api/messages?sessionId=1 http://localhost:8080/api/api/messages?sessionId=1
http://localhost:8080 /mcp http://localhost:8080/mcp http://localhost:8080/mcp
  1. Streamable HTTP: HttpClientStreamableHttpTransport.builder("http://localhost:8080/mcp").build() works on main today. That's the full server URL plus the default /mcp endpoint. With this PR every request goes to /mcp/mcp.
  2. SSE: the endpoint event comes from the server. Servers that are mounted under a prefix and advertise the full path (e.g. /api/messages?...) would get the prefix twice.

Under RFC 3986 §5.2.2, a reference with a leading / replaces the base path. That's what main does now, and what the existing test case /root + /api/v1 -> /api/v1 (changed in this PR) checks.

Something narrower might fix #257 without changing absolute-path references: only append when the endpoint has no leading slash, i.e. treat the base as a directory (add a trailing /) for relative references like api/v1 and keep /api/v1 as-is. Happy to help test whichever direction the maintainers prefer.

How I tested
git fetch upstream pull/974/head:pr-974
git worktree add ../pr-974 pr-974 && cd ../pr-974
./mvnw -pl mcp-core test -Dtest=UtilsTests   # 13/13 pass
# then called Utils.resolveUri(URI.create(base), endpoint) for the rows above
# against mcp-core/target/classes, on this branch and on main (6c0a020b)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

io.modelcontextprotocol.util.Utils#resolveUri incorrectly handles nested paths

2 participants