Skip to content

Build and run agents you can see, understand and trust.

fix(mcp): build the owned streamable-HTTP client through the SDK factory (#3010)

Ziyang Guo0fd09de09e91
diff --git a/src/agentscope/mcp/_mcp_client.py b/src/agentscope/mcp/_mcp_client.py
index 820572314..d8f48adf4 100644
--- a/src/agentscope/mcp/_mcp_client.py
+++ b/src/agentscope/mcp/_mcp_client.py
@@ -234,13 +234,15 @@ class MCPClient(BaseModel):
) -> AsyncGenerator[Any, None]:
"""Create an owned HTTP client that runtime headers can update."""
config = self.mcp_config
- if config.headers or config.timeout:
- client = httpx.AsyncClient(
- headers=config.headers,
- timeout=config.timeout,
- )
- else:
- client = create_mcp_http_client()
+ # Use the SDK factory to keep the transport's own client defaults
+ client = create_mcp_http_client(
+ headers=config.headers,
+ timeout=(
+ httpx.Timeout(config.timeout)
+ if config.timeout is not None
+ else None
+ ),
+ )
# Snapshot before overlaying: clearing runtime headers restores it.
self._static_headers = httpx.Headers(client.headers)
client.headers.update(self._runtime_headers)
diff --git a/tests/mcp_runtime_headers_test.py b/tests/mcp_runtime_headers_test.py
index 6dbf6452d..2bd2d61ad 100644
--- a/tests/mcp_runtime_headers_test.py
+++ b/tests/mcp_runtime_headers_test.py
@@ -174,6 +174,14 @@ class MCPRuntimeHeadersTest(IsolatedAsyncioTestCase):
"untimed",
HttpMCPConfig(url="https://example.com/mcp", timeout=None),
),
+ (
+ "untimed_with_headers",
+ HttpMCPConfig(
+ url="https://example.com/mcp",
+ headers={"X-Static": "static"},
+ timeout=None,
+ ),
+ ),
):
client = MCPClient(
name="runtime_headers",
@@ -185,16 +193,25 @@ class MCPRuntimeHeadersTest(IsolatedAsyncioTestCase):
defaults[label] = {
"follow_redirects": http_client.follow_redirects,
"read_timeout": http_client.timeout.read,
+ "static_header": http_client.headers.get("X-Static"),
}
self.assertDictEqual(
defaults,
{
"configured": {
- "follow_redirects": False,
+ "follow_redirects": transport_defaults["follow_redirects"],
"read_timeout": 30.0,
+ "static_header": "static",
+ },
+ "untimed": {
+ **transport_defaults,
+ "static_header": None,
+ },
+ "untimed_with_headers": {
+ **transport_defaults,
+ "static_header": "static",
},
- "untimed": transport_defaults,
},
)