Repository navigation
Expand file tree
/
Copy path_httpx_utils.py
More file actions
180 lines (142 loc) · 5.96 KB
/
Copy path_httpx_utils.py
File metadata and controls
180 lines (142 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Utilities for creating standardized httpx AsyncClient instances."""
import logging
from enum import Enum
from typing import Any, Protocol
import httpx
logger = logging.getLogger(__name__)
__all__ = [
"MCP_DEFAULT_SSE_READ_TIMEOUT",
"MCP_DEFAULT_TIMEOUT",
"RedirectPolicy",
"create_mcp_http_client",
]
# Default MCP timeout configuration
MCP_DEFAULT_TIMEOUT = 30.0 # General operations (seconds)
MCP_DEFAULT_SSE_READ_TIMEOUT = 300.0 # SSE streams - 5 minutes (seconds)
class RedirectPolicy(Enum):
"""Policy for validating HTTP redirects to protect against SSRF attacks.
Attributes:
ALLOW_ALL: No restrictions on redirects (legacy behavior).
BLOCK_SCHEME_DOWNGRADE: Block HTTPS-to-HTTP downgrades on redirect (default).
ENFORCE_HTTPS: Only allow HTTPS redirect destinations.
"""
ALLOW_ALL = "allow_all"
BLOCK_SCHEME_DOWNGRADE = "block_scheme_downgrade"
ENFORCE_HTTPS = "enforce_https"
async def _check_redirect(response: httpx.Response, policy: RedirectPolicy) -> None:
"""Validate redirect responses against the configured policy.
This is installed as an httpx response event hook. It inspects redirect
responses (3xx with a ``Location`` header) and raises
:class:`httpx.HTTPStatusError` when the redirect violates *policy*.
Args:
response: The httpx response to check.
policy: The redirect policy to enforce.
"""
if not response.has_redirect_location:
return
original_url = response.request.url
redirect_url = httpx.URL(response.headers["Location"])
if redirect_url.is_relative_url:
redirect_url = original_url.join(redirect_url)
if policy == RedirectPolicy.BLOCK_SCHEME_DOWNGRADE:
if original_url.scheme == "https" and redirect_url.scheme == "http":
logger.warning(
"Blocked HTTPS-to-HTTP redirect from %s to %s",
original_url,
redirect_url,
)
raise httpx.HTTPStatusError(
f"HTTPS-to-HTTP redirect blocked: {original_url} -> {redirect_url}",
request=response.request,
response=response,
)
elif policy == RedirectPolicy.ENFORCE_HTTPS:
if redirect_url.scheme != "https":
logger.warning(
"Blocked non-HTTPS redirect from %s to %s",
original_url,
redirect_url,
)
raise httpx.HTTPStatusError(
f"Non-HTTPS redirect blocked: {original_url} -> {redirect_url}",
request=response.request,
response=response,
)
class McpHttpClientFactory(Protocol): # pragma: no branch
def __call__( # pragma: no branch
self,
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
) -> httpx.AsyncClient: ...
def create_mcp_http_client(
headers: dict[str, str] | None = None,
timeout: httpx.Timeout | None = None,
auth: httpx.Auth | None = None,
redirect_policy: RedirectPolicy = RedirectPolicy.BLOCK_SCHEME_DOWNGRADE,
) -> httpx.AsyncClient:
"""Create a standardized httpx AsyncClient with MCP defaults.
This function provides common defaults used throughout the MCP codebase:
- follow_redirects=True (always enabled)
- Default timeout of 30 seconds if not specified
- SSRF redirect protection via *redirect_policy*
Args:
headers: Optional headers to include with all requests.
timeout: Request timeout as httpx.Timeout object.
Defaults to 30 seconds if not specified.
auth: Optional authentication handler.
redirect_policy: Policy controlling which redirects are allowed.
Defaults to ``RedirectPolicy.BLOCK_SCHEME_DOWNGRADE`` which blocks
HTTPS-to-HTTP downgrades. Use ``RedirectPolicy.ENFORCE_HTTPS`` to
only allow HTTPS destinations, or ``RedirectPolicy.ALLOW_ALL`` to
disable redirect validation entirely (legacy behavior).
Returns:
Configured httpx.AsyncClient instance with MCP defaults.
Note:
The returned AsyncClient must be used as a context manager to ensure
proper cleanup of connections.
Example:
Basic usage with MCP defaults:
```python
async with create_mcp_http_client() as client:
response = await client.get("https://api.example.com")
```
With custom headers:
```python
headers = {"Authorization": "Bearer token"}
async with create_mcp_http_client(headers) as client:
response = await client.get("/endpoint")
```
With both custom headers and timeout:
```python
timeout = httpx.Timeout(60.0, read=300.0)
async with create_mcp_http_client(headers, timeout) as client:
response = await client.get("/long-request")
```
With authentication:
```python
from httpx import BasicAuth
auth = BasicAuth(username="user", password="pass")
async with create_mcp_http_client(headers, timeout, auth) as client:
response = await client.get("/protected-endpoint")
```
"""
# Set MCP defaults
kwargs: dict[str, Any] = {"follow_redirects": True}
# Handle timeout
if timeout is None:
kwargs["timeout"] = httpx.Timeout(MCP_DEFAULT_TIMEOUT, read=MCP_DEFAULT_SSE_READ_TIMEOUT)
else:
kwargs["timeout"] = timeout
# Handle headers
if headers is not None:
kwargs["headers"] = headers
# Handle authentication
if auth is not None: # pragma: no cover
kwargs["auth"] = auth
# Install redirect validation hook
if redirect_policy != RedirectPolicy.ALLOW_ALL:
async def check_redirect_hook(response: httpx.Response) -> None:
await _check_redirect(response, redirect_policy)
kwargs["event_hooks"] = {"response": [check_redirect_hook]}
return httpx.AsyncClient(**kwargs)