langchain4j / langchain4j

Java version of LangChain
https://docs.langchain4j.dev
Apache License 2.0
8.03k stars 1.47k forks source link

[BUG] [MCP] No option to provide context path to message endpoint (/mcp/message) with SSE transport #3062

Closed RamAlapure closed 3 weeks ago

RamAlapure commented 4 weeks ago

/I have a MCP server running with SSE transport on http://localhost:8080/mcp-server Now the SSE Endpoint= http://localhost:8080/mcp-server/sse This can be configured using MCP transport

SSE Message Endpoint= http://localhost:8080/mcp-server/mcp/message Not found an option to configure/change this.

Please help if any option to configure message endpoint with the server.servlet.context-path

jmartisk commented 4 weeks ago

You don't need to configure the message endpoint, the server sends the client that URL during the initialization

See https://modelcontextprotocol.io/specification/2024-11-05/basic/transports#http-with-sse

When a client connects, the server MUST send an endpoint event containing a URI for the client to use for sending messages. All subsequent client messages MUST be sent as HTTP POST requests to this endpoint.

RamAlapure commented 4 weeks ago

I have a spring-boot based microservice and trying to use same code base with service as MCP Server. The app context path is configured with spring property server.servlet.context-path causing an issue identifying the MCP server endpoints (Not found).

But if I remove this property server.servlet.context-path it works.

There is issue building a postUrl in HttpMcpTransport startSseChannel method from /sse response, it doesn't create valid url with base path (context-path)

Why it Fails

When Spring Boot app has server.servlet.context-path=/mcp-server, all endpoints are effectively served under:

http://localhost:8080/mcp-server/**

HttpMcpTransport uses the response to /sse to infer the message endpoint, assuming it's at /mcp/message (without context path). So it ends up calling:

http://localhost:8080/mcp/message ❌ (Wrong - 404)

Instead of:

http://localhost:8080/mcp-server/mcp/message ✅ (Correct)

jmartisk commented 4 weeks ago

And what are you using as the server library? Spring AI? If the server tells the client that the URL is /mcp/message instead of /mcp-server/mcp/message, then this is an issue of the server library, not the client.

RamAlapure commented 3 weeks ago

@jmartisk you are correct, it was a spring ai issue. Thank you for a help. I'm active user of Langchain4j and it's a great.

dliubarskyi commented 3 weeks ago

@RamAlapure then I will close this issue, but feel free to reopen if needed.