Skip to content

Commit 1456b89

Browse files
committed
simplifying
1 parent 56d811e commit 1456b89

File tree

2 files changed

+24
-55
lines changed

2 files changed

+24
-55
lines changed

src/mcp/shared/httpx_utils.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,19 @@
1010

1111

1212
def create_mcp_http_client(
13-
*,
1413
headers: dict[str, Any] | None = None,
1514
timeout: httpx.Timeout | None = None,
16-
**kwargs: Any,
1715
) -> httpx.AsyncClient:
1816
"""Create a standardized httpx AsyncClient with MCP defaults.
1917
2018
This function provides common defaults used throughout the MCP codebase:
2119
- follow_redirects=True (always enabled)
2220
- Default timeout of 30 seconds if not specified
23-
- Headers will be merged with any existing headers in kwargs
2421
2522
Args:
2623
headers: Optional headers to include with all requests.
2724
timeout: Request timeout as httpx.Timeout object.
2825
Defaults to 30 seconds if not specified.
29-
**kwargs: Additional keyword arguments to pass to AsyncClient.
3026
3127
Returns:
3228
Configured httpx.AsyncClient instance with MCP defaults.
@@ -42,32 +38,27 @@ def create_mcp_http_client(
4238
4339
# With custom headers
4440
headers = {"Authorization": "Bearer token"}
45-
async with create_mcp_http_client(headers=headers) as client:
41+
async with create_mcp_http_client(headers) as client:
4642
response = await client.get("/endpoint")
4743
48-
# With custom timeout
44+
# With both custom headers and timeout
4945
timeout = httpx.Timeout(60.0, read=300.0)
50-
async with create_mcp_http_client(timeout=timeout) as client:
46+
async with create_mcp_http_client(headers, timeout) as client:
5147
response = await client.get("/long-request")
5248
"""
5349
# Set MCP defaults
54-
defaults: dict[str, Any] = {
50+
kwargs: dict[str, Any] = {
5551
"follow_redirects": True,
5652
}
5753

5854
# Handle timeout
5955
if timeout is None:
60-
defaults["timeout"] = httpx.Timeout(30.0)
56+
kwargs["timeout"] = httpx.Timeout(30.0)
6157
else:
62-
defaults["timeout"] = timeout
58+
kwargs["timeout"] = timeout
6359

64-
# Handle headers with proper merging
60+
# Handle headers
6561
if headers is not None:
66-
existing_headers = kwargs.get("headers", {})
67-
merged_headers = {**existing_headers, **headers}
68-
kwargs["headers"] = merged_headers
62+
kwargs["headers"] = headers
6963

70-
# Merge kwargs with defaults (defaults take precedence)
71-
kwargs = {**kwargs, **defaults}
72-
73-
return httpx.AsyncClient(**kwargs)
64+
return httpx.AsyncClient(**kwargs)

tests/shared/test_httpx_utils.py

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,20 @@
55
from mcp.shared.httpx_utils import create_mcp_http_client
66

77

8-
class TestCreateMcpHttpClient:
9-
"""Test create_mcp_http_client function."""
8+
def test_default_settings():
9+
"""Test that default settings are applied correctly."""
10+
client = create_mcp_http_client()
11+
12+
assert client.follow_redirects is True
13+
assert client.timeout.connect == 30.0
1014

11-
def test_default_settings(self):
12-
"""Test that default settings are applied correctly."""
13-
client = create_mcp_http_client()
14-
15-
# Check follow_redirects is True
16-
assert client.follow_redirects is True
17-
18-
# Check default timeout is 30 seconds
19-
assert client.timeout.connect == 30.0
20-
assert client.timeout.read == 30.0
21-
assert client.timeout.write == 30.0
22-
assert client.timeout.pool == 30.0
2315

24-
def test_custom_parameters(self):
25-
"""Test custom headers and timeout are set correctly."""
26-
headers = {"Authorization": "Bearer token", "X-Custom": "value"}
27-
timeout = httpx.Timeout(connect=5.0, read=10.0, write=15.0, pool=20.0)
28-
29-
client = create_mcp_http_client(headers=headers, timeout=timeout)
30-
31-
# Check headers
32-
assert client.headers["Authorization"] == "Bearer token"
33-
assert client.headers["X-Custom"] == "value"
34-
35-
# Check custom timeout
36-
assert client.timeout.connect == 5.0
37-
assert client.timeout.read == 10.0
38-
assert client.timeout.write == 15.0
39-
assert client.timeout.pool == 20.0
40-
41-
def test_follow_redirects_enforced(self):
42-
"""Test follow_redirects is always True even if False is passed."""
43-
client = create_mcp_http_client(follow_redirects=False)
44-
45-
# Should still be True because our defaults override user input
46-
assert client.follow_redirects is True
16+
def test_custom_parameters():
17+
"""Test custom headers and timeout are set correctly."""
18+
headers = {"Authorization": "Bearer token"}
19+
timeout = httpx.Timeout(60.0)
20+
21+
client = create_mcp_http_client(headers, timeout)
22+
23+
assert client.headers["Authorization"] == "Bearer token"
24+
assert client.timeout.connect == 60.0

0 commit comments

Comments
 (0)