modelcontextprotocol / modelcontextprotocol/python-sdk

Support for API Gateway Path Prefixes in SSE Client URL Handling

未关闭
#795 2 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug P1 ready for work
主要语言
Python
星标
24.3k
派生
4k
平均合并
1 天 1 小时
30 天内合并 PR
31

描述

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/<deployment_name>), 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 and uses urljoin 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.

debug logs:

❯ python3 client.py
DEBUG:asyncio:Using selector: KqueueSelector
INFO:mcp.client.sse:Connecting to SSE endpoint: https://example.com/gateway_prefix/<deployment_name>/v1/sse
DEBUG:httpcore.connection:connect_tcp.started host='https://example.com' port=443 local_address=None timeout=5 socket_options=None
DEBUG:httpcore.connection:connect_tcp.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x10889fef0>
DEBUG:httpcore.connection:start_tls.started ssl_context=<ssl.SSLContext object at 0x10895ca50> server_hostname='example.com' timeout=5
DEBUG:httpcore.connection:start_tls.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x107fb45c0>
DEBUG:httpcore.http11:send_request_headers.started request=<Request [b'GET']>
DEBUG:httpcore.http11:send_request_headers.complete
DEBUG:httpcore.http11:send_request_body.started request=<Request [b'GET']>
DEBUG:httpcore.http11:send_request_body.complete
DEBUG:httpcore.http11:receive_response_headers.started request=<Request [b'GET']>
DEBUG: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')])
INFO:httpx:HTTP Request: GET https://example.com/gateway_prefix/<deployment_name>/v1/sse "HTTP/1.1 200 OK" <== correct sse endpoint
DEBUG:mcp.client.sse:SSE connection established
DEBUG:httpcore.http11:receive_response_body.started request=<Request [b'GET']>
DEBUG:mcp.client.sse:Received SSE event: endpoint
DEBUG:mcp.client.sse:urljoin: https://example.com/gateway_prefix/<deployment_name>/v1/sse + /v1/messages/?session_id=80b1237100a34eaa9795a5993661c76a <== wrong messages endpoint
INFO:mcp.client.sse:Received endpoint URL: https://example.com/v1/messages/?session_id=80b1237100a34eaa9795a5993661c76a
INFO:mcp.client.sse:Starting post writer with endpoint URL: https://example.com/v1/messages/?session_id=80b1237100a34eaa9795a5993661c76a

Describe the solution you'd like
The 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.

Describe alternatives you've considered

Additional context

Example:

url = "https://example.com/gateway_prefix/<deployment_name>/v1/sse"
path = "/v1/messages"
urljoin(url, path)
# Actual:   https://example.com/v1/messages
# Expected: https://example.com/gateway_prefix/<deployment_name>/v1/messages

This is a common pattern for API gateways and reverse proxies, so supporting this would improve compatibility.

similar request:
https://github.com/modelcontextprotocol/python-sdk/issues/412#issuecomment-2820077952
https://github.com/modelcontextprotocol/python-sdk/issues/733
https://github.com/modelcontextprotocol/python-sdk/pull/386

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 src/mcp/client/sse.py 中客户端 URL 和 urljoin 调用附近的 URL 处理开始。复现 issue 中的 gateway-prefix 示例,并检查 SSE 端点如何提供 messages 路径。当 messages 端点保留部署特定的前缀,同时保持现有的 URL 行为时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
api
Issue 类型
功能
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
52/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。