modelcontextprotocol / modelcontextprotocol/python-sdk

Expose create_mcp_http_client and McpHttpClientFactory as public API (2.0 made them private-only)

未關閉 適合新手
#3,238 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

P1 v2
主要語言
Python
星號
24.3k
分支
4k
平均合併
1 天 1 小時
30 天內合併 PR
31

描述

Summary

Customizing the HTTP client for streamable_http_client (custom headers, auth, timeout, proxy, etc.) is a common and legitimate need, but as of 2.0.0 the only supported way to build a conforming client is through helpers that live in the private module mcp.shared._httpx_utils. Users are therefore forced to depend on a private API.

What changed in 2.0

In 1.x, streamable_http_client accepted convenience kwargs directly:

streamable_http_client(url, headers=..., timeout=..., sse_read_timeout=..., auth=...)

2.0 (via #2972, which replaced httpx/httpx-sse with httpx2) removed those kwargs. The signature is now:

async def streamable_http_client(
    url: str,
    *,
    http_client: httpx2.AsyncClient | None = None,
    terminate_on_close: bool = True,
) -> ...

So the only way to pass custom headers/auth/timeout is to build an httpx2.AsyncClient yourself and pass it as http_client=. The standardized factory for doing so is create_mcp_http_client — but it is only available at the private path:

from mcp.shared._httpx_utils import create_mcp_http_client  # private module

The same applies to McpHttpClientFactory: in 1.x it was importable from the public mcp.client.streamable_http, but in 2.0 it is defined in mcp.shared._httpx_utils and is no longer re-exported from any public module.

Why this is a problem

  • "Connect to an MCP server that requires auth headers / a custom timeout / a proxy" is a standard use case, not an edge case.
  • The leading underscore on mcp.shared._httpx_utils signals "private, may change without notice", so every downstream project that needs a custom client has to take on that fragility.
  • It's inconsistent: the consumption side (streamable_http_client(url, http_client=...)) is public, but the construction side (create_mcp_http_client, McpHttpClientFactory) is private.

Suggestion

Re-export create_mcp_http_client and McpHttpClientFactory from a public module — e.g. mcp.client.streamable_http (where McpHttpClientFactory used to live) or mcp.shared — so building a custom HTTP client does not require importing a private module.

(Related: because the HTTP layer now uses httpx2, a short note in the migration docs on how to build/pass a custom http_client would also help.)

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先檢查 mcp/shared/_httpx_utils.py 和 mcp/client/streamable_http.py,以比較目前的定義和公開匯出。從公開模組重新匯出 create_mcp_http_client 和 McpHttpClientFactory,並考慮 issue 中提到的遷移文件註記。完成的標準是使用者可以建構並傳入自訂用戶端,而不必匯入私有模組。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
api
Issue 類型
功能
難度
2/5
預估耗時
1-3 小時
活躍度
冷清
描述清晰度
基本清楚
新手友好度
68/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。