modelcontextprotocol / modelcontextprotocol/java-sdk

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

Open
#257 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/client enhancement help wanted P2
Dominant language
Java
Stars
3.7k
Forks
1.1k
Avg merge
1d 15h
Merged PRs (30d)
9

Description

Please do a quick search on GitHub issues first, there might be already a duplicate issue for the one you are about to create.
If the bug is trivial, just go ahead and create the issue. Otherwise, please take a few moments and fill in the following sections:

Bug description

It took me four hours to locate and fix this Bug,
When I create a simple link, it always reminds me of http status 302. Finally, it was located that the SSE MCP Server of the business side was redirected.

Environment
Please provide as many details as possible: Spring MCP version, Java version, which vector store you use if any, etc

Java SDK

Steps to reproduce
It's very simple. When you have an SSE MCP SERVER but the interface does not end with /sse, an error will definitely be reported when creating the connection.

Expected behavior

Adapt to the SSE MCP SERVER with non-standard users

I first fork and change the IO. Modelcontextprotocol. Util. Utils# resolveUri logic is as follows

old code

	public static URI resolveUri(URI baseUrl, String endpointUrl) {
		URI endpointUri = URI.create(endpointUrl);
		if (endpointUri.isAbsolute() && !isUnderBaseUri(baseUrl, endpointUri)) {
			throw new IllegalArgumentException("Absolute endpoint URL does not match the base URL.");
		}
		else {
			return baseUrl.resolve(endpointUri);
		}
	}

new code

	public static URI resolveUri(URI baseUrl, String endpointUrl) {
		if (endpointUrl.equals(DEFAULT_SSE_ENDPOINT)) {
			return baseUrl;
		}
		else {
			URI endpointUri = URI.create(endpointUrl);
			return baseUrl.resolve(endpointUri);
		}
	}

Minimal Complete Reproducible example
You only need to replace the following URL with a URL that does not end with SSE, and it can be reproduced

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.junit.Test;

import java.time.Duration;

public class SSEClientTest {
	private String youUrl = "https://host:port/aaa/bbb/cccc";

	@Test
	public void testSSE() {
		mockSSEClient(youUrl);
	}

	/**
	 * Test MCP Client
	 */
	private void mockSSEClient(String url) {
		McpSyncClient client = McpClient.sync(create(url))
			.requestTimeout(Duration.ofSeconds(3))
			.initializationTimeout(Duration.ofSeconds(1))
			.capabilities(McpSchema.ClientCapabilities.builder().build())
			.build();

		client.initialize();
		for (McpSchema.Tool tool : client.listTools().tools()) {
			System.out.println(" Tool :" + tool);
		}

	}

	private HttpClientSseClientTransport create(String url) {
		return HttpClientSseClientTransport.builder(url).build();
	}
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at io.modelcontextprotocol.util.Utils#resolveUri and inspect how the base URL and endpoint URL are resolved for nested paths. Run the provided SSEClientTest reproduction with an SSE server URL that does not end in /sse; done means the client connects successfully without the reported redirect-related error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.