Title: feat: add url_prefix support to FastMCP SSE by pridkett · Pull Request #278 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: feat: add url_prefix support to FastMCP SSE by pridkett · Pull Request #278 · modelcontextprotocol/python-sdk
X Title: feat: add url_prefix support to FastMCP SSE by pridkett · Pull Request #278 · modelcontextprotocol/python-sdk
Description: Previously the paths for SSE were hardcoded to /sse and /messages this made it hard to run behind an API gateway. Requests to the /sse endpoint, even if proxied properly, would return /messages for the location of the /messages endpoint even if it should've been /api/v1/my_mcp_server/messages. This brings in a new parameter to FastMCP, url_prefix, that allows you set a prefix of something such as /api/v1/my_mcp_server and have the serve function properly. This can be passed either on the FastMCP constructor or with the environment variable FASTMCP_URL_PREFIX. DCO-1.1 Signed-off-by: Patrick Wagstrom 160672+pridkett@users.noreply.github.com Motivation and Context There are situations when you may want something like an API Gateway to front multiple MCP servers, in this case they can't all serve the endpoints of /sse and /messages, but unfortunately, those were hardcoded. An because of the way that the connection is kept open on /sse, you can't always use body rewrite features inside of the API gateway to change the URLs. How Has This Been Tested? I created, with the assistance of Claude, tests/server/fastmcp/test_url_prefix.py which covers most of the tests. I've also tested this locally with my API gateway and observed that it works. Breaking Changes None observed 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 On the last one, if the other environment variables like FASTMCP_HOST and FASTMCP_PORT had a spot in the documentation, I'd add FASTMCP_URL_PREFIX next to them, but I didn't see where that might be. Additional context Without this, it still is possible to run multiple MCP servers behind an API gateway, but you need to run them in stdio mode and use Supergateway with the --ssePath and --messagePath flags set.
Open Graph Description: Previously the paths for SSE were hardcoded to /sse and /messages this made it hard to run behind an API gateway. Requests to the /sse endpoint, even if proxied properly, would return /messages fo...
X Description: Previously the paths for SSE were hardcoded to /sse and /messages this made it hard to run behind an API gateway. Requests to the /sse endpoint, even if proxied properly, would return /messages fo...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/278
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:893f0cdd-64ce-d426-a2f3-857bf1856bf2 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | ACB8:2088C9:3121359:444E00E:6A634813 |
| html-safe-nonce | f3cd45daa15bd30820f8cd6d87888736e36860c8c8473b17e1fe372a558cb2f7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQ0I4OjIwODhDOTozMTIxMzU5OjQ0NEUwMEU6NkE2MzQ4MTMiLCJ2aXNpdG9yX2lkIjoiMjI2OTAxMjcwMDA2Nzc0Mzc2MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a02d64a82ab350715b01325bdbb7f80b9f57b0945eb06193063fbe558e63a158 |
| hovercard-subject-tag | pull_request:2392044861 |
| 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/278/files |
| twitter:image | https://avatars.githubusercontent.com/u/160672?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/160672?s=400&v=4 |
| og:image:alt | Previously the paths for SSE were hardcoded to /sse and /messages this made it hard to run behind an API gateway. Requests to the /sse endpoint, even if proxied properly, would return /messages fo... |
| 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 full-width |
| 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