Title: Support for API Gateway Path Prefixes in SSE Client URL Handling · Issue #795 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Support for API Gateway Path Prefixes in SSE Client URL Handling · Issue #795 · modelcontextprotocol/python-sdk
X Title: Support for API Gateway Path Prefixes in SSE Client URL Handling · Issue #795 · modelcontextprotocol/python-sdk
Description: Is your feature request related to a problem? Please describe. When using the client SDK to connect MCP server behind an API gateway that adds a deployment-specific prefix (e.g., /gateway_prefix/
Open Graph Description: Is your feature request related to a problem? Please describe. When using the client SDK to connect MCP server behind an API gateway that adds a deployment-specific prefix (e.g., /gateway_prefix/ X Description: Is your feature request related to a problem? Please describe. When using the client SDK to connect MCP server behind an API gateway that adds a deployment-specific prefix (e.g., /gateway_prefix/&l...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/795
X: @github
Domain: github.com
Links:
Viewport: width=device-width
Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support for API Gateway Path Prefixes in SSE Client URL Handling","articleBody":"**Is your feature request related to a problem? Please describe.**\n\nWhen using the client SDK to connect MCP server behind an `API gateway` that adds a deployment-specific prefix (e.g., `/gateway_prefix/\u003cdeployment_name\u003e`), the backend mcp server is unaware of this prefix and only exposes routes like `/v1/sse` and `/v1/messages`. The client SDK currently only supports [url](https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/sse.py#L25) and uses [urljoin](https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/sse.py#L75) to resolve endpoint URLs, which causes the gateway prefix to be dropped when joining paths that start with /. This results in incorrect routing through the gateway.\n\ndebug logs:\n\n```\n❯ python3 client.py\nDEBUG:asyncio:Using selector: KqueueSelector\nINFO:mcp.client.sse:Connecting to SSE endpoint: https://example.com/gateway_prefix/\u003cdeployment_name\u003e/v1/sse\nDEBUG:httpcore.connection:connect_tcp.started host='https://example.com' port=443 local_address=None timeout=5 socket_options=None\nDEBUG:httpcore.connection:connect_tcp.complete return_value=\u003chttpcore._backends.anyio.AnyIOStream object at 0x10889fef0\u003e\nDEBUG:httpcore.connection:start_tls.started ssl_context=\u003cssl.SSLContext object at 0x10895ca50\u003e server_hostname='example.com' timeout=5\nDEBUG:httpcore.connection:start_tls.complete return_value=\u003chttpcore._backends.anyio.AnyIOStream object at 0x107fb45c0\u003e\nDEBUG:httpcore.http11:send_request_headers.started request=\u003cRequest [b'GET']\u003e\nDEBUG:httpcore.http11:send_request_headers.complete\nDEBUG:httpcore.http11:send_request_body.started request=\u003cRequest [b'GET']\u003e\nDEBUG:httpcore.http11:send_request_body.complete\nDEBUG:httpcore.http11:receive_response_headers.started request=\u003cRequest [b'GET']\u003e\nDEBUG:httpcore.http11:receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'cache-control', b'no-store'), (b'content-type', b'text/event-stream; charset=utf-8'), (b'date', b'Fri, 23 May 2025 15:45:20 GMT'), (b'x-accel-buffering', b'no'), (b'x-upstream-service-time', b'7'), (b'transfer-encoding', b'chunked')])\nINFO:httpx:HTTP Request: GET https://example.com/gateway_prefix/\u003cdeployment_name\u003e/v1/sse \"HTTP/1.1 200 OK\" \u003c== correct sse endpoint\nDEBUG:mcp.client.sse:SSE connection established\nDEBUG:httpcore.http11:receive_response_body.started request=\u003cRequest [b'GET']\u003e\nDEBUG:mcp.client.sse:Received SSE event: endpoint\nDEBUG:mcp.client.sse:urljoin: https://example.com/gateway_prefix/\u003cdeployment_name\u003e/v1/sse + /v1/messages/?session_id=80b1237100a34eaa9795a5993661c76a \u003c== wrong messages endpoint\nINFO:mcp.client.sse:Received endpoint URL: https://example.com/v1/messages/?session_id=80b1237100a34eaa9795a5993661c76a\nINFO:mcp.client.sse:Starting post writer with endpoint URL: https://example.com/v1/messages/?session_id=80b1237100a34eaa9795a5993661c76a\n```\n\n**Describe the solution you'd like**\nThe client SDK should preserve the gateway prefix when joining endpoint URLs, so that requests are routed correctly through the gateway. Ideally, the SDK should provide a way to configure or detect the gateway prefix and ensure all endpoint URLs are constructed with it.\n\n**Describe alternatives you've considered**\n\n**Additional context**\n\nExample:\n```python\nurl = \"https://example.com/gateway_prefix/\u003cdeployment_name\u003e/v1/sse\"\npath = \"/v1/messages\"\nurljoin(url, path)\n# Actual: https://example.com/v1/messages\n# Expected: https://example.com/gateway_prefix/\u003cdeployment_name\u003e/v1/messages\n```\nThis is a common pattern for API gateways and reverse proxies, so supporting this would improve compatibility.\n\nsimilar request: \nhttps://github.com/modelcontextprotocol/python-sdk/issues/412#issuecomment-2820077952\nhttps://github.com/modelcontextprotocol/python-sdk/issues/733\nhttps://github.com/modelcontextprotocol/python-sdk/pull/386\n","author":{"url":"https://github.com/lizzzcai","@type":"Person","name":"lizzzcai"},"datePublished":"2025-05-23T16:14:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/795/python-sdk/issues/795"}
route-pattern /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) route-controller voltron_issues_fragments route-action issue_layout fetch-nonce v2:718edc41-6eff-1b88-d387-2572a750b308 current-catalog-service-hash 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 request-id CAC8:2F70B7:86410A:BE9809:6A6319C7 html-safe-nonce 7f22f2208578e2820ff8739c90733610ae546fa1bd596ea21674adf08ebf73f3 visitor-payload eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUM4OjJGNzBCNzo4NjQxMEE6QkU5ODA5OjZBNjMxOUM3IiwidmlzaXRvcl9pZCI6IjE0MzUwMTA2MjA0NzYwMzc1NzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== visitor-hmac 3103c993af2f43f32eb5d4d6f7c2502306d55f38b3a264a14cf1fff2ff69671b hovercard-subject-tag issue:3086877237 github-keyboard-shortcuts repository,issues,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/_view_fragments/issues/show/modelcontextprotocol/python-sdk/795/issue_layout twitter:image https://opengraph.githubassets.com/e752e67510a15c94f7b6873fd844cbff05777aff282ad47830e1f649074a4900/modelcontextprotocol/python-sdk/issues/795 twitter:card summary_large_image og:image https://opengraph.githubassets.com/e752e67510a15c94f7b6873fd844cbff05777aff282ad47830e1f649074a4900/modelcontextprotocol/python-sdk/issues/795 og:image:alt Is your feature request related to a problem? Please describe. When using the client SDK to connect MCP server behind an API gateway that adds a deployment-specific prefix (e.g., /gateway_prefix/ og:image:width 1200 og:image:height 600 og:site_name GitHub og:type object og:author:username lizzzcai hostname github.com expected-hostname github.com None 1a6c056e02f174fffc096c521ec0ff6fb83e40a2ec8cb8875466ec1524872dd6 turbo-cache-control no-preview 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 false browser-stats-url https://api.github.com/_private/browser/stats browser-errors-url https://api.github.com/_private/browser/errors release 6a93e25585f487ddff9e3996c06d5b869d6e1828 ui-target full theme-color #1e2327 color-scheme light dark
URLs of crawlers that visited me.