python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

SSE does not work on nested routes

Open kocsszi opened this issue 7 months ago • 3 comments

Describe the bug URL parts are being omitted on SSE client session connect.

INFO:mcp.client.sse:Connecting to SSE endpoint: https://url-part1.url-part2.com/route1/route2/sse INFO:httpx:HTTP Request: GET https://url-part1.url-part2.com/route1/route2/sse "HTTP/1.1 200 OK" INFO:mcp.client.sse:Received endpoint URL: https://url-part1.url-part2.com/route2/messages/?session_id=a2bd62740b69466c8b27043ccda2513f INFO:mcp.client.sse:Starting post writer with endpoint URL: https://url-part1.url-part2.com/route2/messages/?session_id=a2bd62740b69466c8b27043ccda2513f

To Reproduce

import asyncio
import logging
from fastmcp import Client
from fastmcp.client.transports import (
    SSETransport
)


sse_url = "https://url-part1.url-part2.com/route1/route2/sse"
async def main():
    # Connect to a server over SSE (common for web-based MCP servers)
    async with Client(SSETransport(url=sse_url)) as client:
        # Send a message to the server
        result = await client.list_tools()
        print(result)


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

Expected behavior route1 is not omitted

Desktop (please complete the following information):

  • OS: Ubuntu
  • Version 1.9.0

Additional context issue stems from: .venv/lib/python3.12/site-packages/mcp/client/sse.py line 66

kocsszi avatar May 16 '25 10:05 kocsszi

I have met the same problem. This issue has been closed, did you find a solution?

Col0ring avatar May 23 '25 06:05 Col0ring

Hello - yes

For SSE - I had to monkey patch mcp.server.sse.SseServerTransport's connect_sse method.

Somewhere in the middle add your route parts into the root_path variable, for me the patch is

root_path = scope.get("root_path", "") # this is in the mcp codebase
if not LOCAL_SESSION and not root_path.startswith(f"/{MAIN_ROUTE}"): # LOCAL_SESSION identifies if it is local dev run or not
  root_path = f"/{MAIN_ROUTE}" + root_path

In theory you can use some middleware as well in the starlette application and update the root_path in the scope there, but I just could not get that working yet properly.

I'll reopen this issue, as hoping for some guidance as well from maintainers, especially since this method just was updated and still does not work for our use-case properly.

kocsszi avatar May 23 '25 07:05 kocsszi

Hello - yes

For SSE - I had to monkey patch mcp.server.sse.SseServerTransport's connect_sse method.

Somewhere in the middle add your route parts into the root_path variable, for me the patch is

root_path = scope.get("root_path", "") # this is in the mcp codebase
if not LOCAL_SESSION and not root_path.startswith(f"/{MAIN_ROUTE}"): # LOCAL_SESSION identifies if it is local dev run or not
  root_path = f"/{MAIN_ROUTE}" + root_path

In theory you can use some middleware as well in the starlette application and update the root_path in the scope there, but I just could not get that working yet properly.

I'll reopen this issue, as hoping for some guidance as well from maintainers, especially since this method just was updated and still does not work for our use-case properly.

Thanks for your reply, I just found there is already a PR trying to resolve this, and I also found the official approach https://github.com/modelcontextprotocol/python-sdk/pull/540. I think these might be helpful.

Col0ring avatar May 23 '25 07:05 Col0ring

This issue looks like it may have been resolved, if not please feel free to reopen.

felixweinberger avatar Oct 03 '25 13:10 felixweinberger