You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[v1.x] Follow redirects only within the MCP endpoint's origin
Backport of #3397 to the 1.x line (httpx / httpx-sse).
The Streamable HTTP and SSE client transports now send every request with
redirect following off and follow a redirect themselves only when it stays
on the endpoint's origin (same scheme, host and port, or http -> https on
the same host with default ports), keeps the request method, and carries no
userinfo. Any other redirect is handed back unfollowed and fails the way a
non-2xx response does on 1.x (httpx.HTTPStatusError), with a message naming
the location. The caller's client `follow_redirects` setting is not
consulted either way, so the usual `/mcp` -> `/mcp/` trailing-slash redirect
keeps working with any client.
OAuthClientProvider applies the same rule to the requests its flow makes
(metadata discovery, registration, token) via a RedirectAwareAuth base; a
3xx from a discovery URL is treated like a 4xx (try the next candidate).
create_mcp_http_client no longer sets follow_redirects=True; the simple-tool
example that used it to fetch arbitrary pages gets its own plain client.
sse_client now also closes its receive streams when connecting fails.
To configure headers, authentication or timeouts, create an `httpx.AsyncClient` and pass it as `http_client=`.
134
+
135
+
## HTTP redirects
136
+
137
+
The transport connects to the URL you gave it, and only that origin.
138
+
139
+
* A `307`/`308` redirect that stays on the same scheme, host and port is followed, and so is `http://` → `https://` on the same host. That covers the usual `/mcp` → `/mcp/` trailing-slash redirect.
140
+
* A redirect anywhere else is **not** followed. Connecting fails with:
141
+
142
+
```text
143
+
httpx.HTTPStatusError: Redirect to https://other.example.com/mcp not followed; use that URL as the endpoint if it is the intended server
144
+
```
145
+
146
+
If that URL is the server you meant, put it in your config. If it isn't, the server or a proxy in front of it is misconfigured.
147
+
148
+
This holds for any `httpx.AsyncClient` you pass in: its `follow_redirects` setting is not consulted for MCP requests, in either direction. The SDK's OAuth providers apply the same rule to their own requests, and so does `sse_client()`.
149
+
150
+
!!! tip
151
+
`Redirect to http://… not followed: it would downgrade this HTTPS endpoint to plain HTTP` means the
152
+
server sits behind a TLS-terminating proxy it doesn't know about and is issuing `http://` redirects.
153
+
That is fixed on the server (for uvicorn: `--proxy-headers` and `--forwarded-allow-ips`), or by
154
+
using the exact `https://…/` URL the message suggests.
155
+
133
156
## Client Display Utilities
134
157
135
158
When building MCP clients, the SDK provides utilities to help display human-readable names for tools, resources, and prompts:
0 commit comments