Title: [Fix] SSE client to support MCP server mounted under a base path on a different ASGI server by rkondra-eightfold · Pull Request #524 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: [Fix] SSE client to support MCP server mounted under a base path on a different ASGI server by rkondra-eightfold · Pull Request #524 · modelcontextprotocol/python-sdk
X Title: [Fix] SSE client to support MCP server mounted under a base path on a different ASGI server by rkondra-eightfold · Pull Request #524 · modelcontextprotocol/python-sdk
Description: What does this PR do? Fixes incorrect resolution of the /messages endpoint URL in the SSE client when the FastAPI app is mounted under a base path (e.g., /mcp). You can now provide a server_mount_path to ensure correct endpoint resolution. Motivation and Context Problem # Create a FastAPI app app = FastAPI() # Create an MCP server and link it to the FastAPI app server = FastMCP('test_app') # Mount the server's SSE API on /mcp app.mount('/mcp', server.sse_app()) The server sends a relative endpoint like /messages?session_id=..., the client resolves it using urljoin(base_url, relative_path) When the app is mounted (e.g., /mcp/sse), this strips the subpath and constructs an invalid endpoint (e.g., http://host/messages?session_id=... instead of http://host/mcp/messages?session_id=...). Fix This PR: Extracts the origin and mounted path prefix from the SSE base URL Correctly reassembles the endpoint URL using that prefix async with sse_client(base_url, server_mount_path='/mcp') as (read, write): async with ClientSession(read, write) as session: await session.initialize() # Initialize the session print('✅ Connection established!') How Has This Been Tested? server: from fastapi import FastAPI from models import User from mcp.server.fastmcp import FastMCP # Create a FastAPI app app = FastAPI() # Create an MCP server and link it to the FastAPI app server = FastMCP('test_app') # Mount the server's SSE API on /mcp app.mount('/mcp', server.sse_app()) # Start the server when this file is run directly if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) client: import asyncio from mcp import ClientSession from mcp.client.sse import sse_client async def run(): base_url = 'http://0.0.0.0:8000/mcp/sse' # Make sure the URL is correct for the SSE connection print(f'🔗 Connecting to SSE MCP server at {base_url}...') try: async with sse_client(base_url, server_mount_path='/mcp') as (read, write): async with ClientSession(read, write) as session: await session.initialize() # Initialize the session print('✅ Connection established!') except Exception as e: print(f'[ERROR] Connection to SSE server failed: {e}') # Run the test if __name__ == '__main__': print('🚀 Running MCP SSE client tests...\n') asyncio.run(run()) This test should pass now. Breaking Changes None identified, backward compatible Types of changes Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context NA
Open Graph Description: What does this PR do? Fixes incorrect resolution of the /messages endpoint URL in the SSE client when the FastAPI app is mounted under a base path (e.g., /mcp). You can now provide a server_mount_...
X Description: What does this PR do? Fixes incorrect resolution of the /messages endpoint URL in the SSE client when the FastAPI app is mounted under a base path (e.g., /mcp). You can now provide a server_mount_...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/524
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:3be6be9e-d59e-dfab-7369-aaecd1811e20 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9464:33F7F9:15D8C73:1F46542:6A634EBC |
| html-safe-nonce | b4f0519d8314c3f578c74df2d0084f2330f3e77ab8b209282be819af2079ac9d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NDY0OjMzRjdGOToxNUQ4QzczOjFGNDY1NDI6NkE2MzRFQkMiLCJ2aXNpdG9yX2lkIjoiNjY0OTE1NjgzMzk5OTI3Nzc1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f450034946ced109ea6716915fc9ed95d1a71f9dbe954d33bf4fb04e580287c1 |
| hovercard-subject-tag | pull_request:2461362380 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/524/files |
| twitter:image | https://avatars.githubusercontent.com/u/96047250?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/96047250?s=400&v=4 |
| og:image:alt | What does this PR do? Fixes incorrect resolution of the /messages endpoint URL in the SSE client when the FastAPI app is mounted under a base path (e.g., /mcp). You can now provide a server_mount_... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 59e55daad7174ca59d63c6974d58276ccb5477442e550bebb3c035e1bef11c94 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 990295d92a4cc7b63fbbd83a046217cd7d77d49c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width