[v1.x] Follow redirects only within the MCP endpoint's origin (#3448)
This commit is contained in:
parent
3eed7ce0a6
commit
2106335745
4 changed files with 948 additions and 192 deletions
|
|
@ -5,6 +5,7 @@ Tests for refactored OAuth client authentication implementation.
|
|||
import base64
|
||||
import json
|
||||
import time
|
||||
from collections.abc import AsyncGenerator
|
||||
from unittest import mock
|
||||
from urllib.parse import parse_qs, unquote, urlparse
|
||||
|
||||
|
|
@ -26,6 +27,7 @@ from mcp.client.auth.utils import (
|
|||
extract_resource_metadata_from_www_auth,
|
||||
extract_scope_from_www_auth,
|
||||
get_client_metadata_scopes,
|
||||
handle_auth_metadata_response,
|
||||
handle_registration_response,
|
||||
is_valid_client_metadata_url,
|
||||
should_use_client_metadata_url,
|
||||
|
|
@ -824,40 +826,129 @@ class TestProtectedResourceMetadata:
|
|||
assert "resource=" in content
|
||||
|
||||
|
||||
async def _start_discovery(
|
||||
provider: OAuthClientProvider,
|
||||
) -> tuple[AsyncGenerator[httpx.Request, httpx.Response], httpx.Request]:
|
||||
"""Drive `provider`'s auth flow to the point where it has sent the MCP request, seen a 401 and
|
||||
issued its first protected-resource-metadata request; returns (flow, that request)."""
|
||||
provider.context.current_tokens = None
|
||||
provider.context.token_expiry_time = None
|
||||
provider._initialized = True
|
||||
mcp_request = httpx.Request("POST", "https://api.example.com/v1/mcp")
|
||||
flow = provider.async_auth_flow(mcp_request)
|
||||
sent = await flow.__anext__()
|
||||
assert sent is mcp_request
|
||||
# No resource_metadata hint, so discovery tries the path-based well-known URL, then the root one.
|
||||
unauthorized = httpx.Response(401, request=mcp_request)
|
||||
prm_request = await flow.asend(unauthorized)
|
||||
assert (prm_request.method, str(prm_request.url)) == (
|
||||
"GET",
|
||||
"https://api.example.com/.well-known/oauth-protected-resource/v1/mcp",
|
||||
)
|
||||
return flow, prm_request
|
||||
|
||||
|
||||
async def _redirect(request: httpx.Request, status: int, location: str) -> httpx.Response:
|
||||
"""A redirect answer to `request`, as httpx hands it back when it does not follow it."""
|
||||
transport = httpx.MockTransport(lambda r: httpx.Response(status, headers={"location": location}))
|
||||
async with httpx.AsyncClient(transport=transport) as client:
|
||||
return await client.send(request)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_auth_flow_follows_a_same_origin_redirect_of_its_own_request(oauth_provider: OAuthClientProvider):
|
||||
"""SDK-defined: a request the OAuth flow makes (here protected-resource metadata discovery)
|
||||
follows a redirect that stays within its origin and keeps its method, like an MCP request."""
|
||||
flow, prm_request = await _start_discovery(oauth_provider)
|
||||
|
||||
follow_up = await flow.asend(await _redirect(prm_request, 307, "/.well-known/oauth-protected-resource/v1/mcp/"))
|
||||
|
||||
assert (follow_up.method, str(follow_up.url)) == (
|
||||
"GET",
|
||||
"https://api.example.com/.well-known/oauth-protected-resource/v1/mcp/",
|
||||
)
|
||||
await flow.aclose()
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_auth_flow_does_not_follow_a_redirect_of_its_own_request_to_another_origin(
|
||||
oauth_provider: OAuthClientProvider,
|
||||
):
|
||||
"""SDK-defined: a redirect of a flow request to another origin is handed to the flow unfollowed,
|
||||
which treats it as "not served here" and moves to its next discovery URL."""
|
||||
flow, prm_request = await _start_discovery(oauth_provider)
|
||||
|
||||
next_request = await flow.asend(await _redirect(prm_request, 307, "https://elsewhere.example/prm"))
|
||||
|
||||
assert (next_request.method, str(next_request.url)) == (
|
||||
"GET",
|
||||
"https://api.example.com/.well-known/oauth-protected-resource",
|
||||
)
|
||||
await flow.aclose()
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_auth_flow_stops_following_a_redirecting_request_after_a_few_hops(
|
||||
oauth_provider: OAuthClientProvider,
|
||||
):
|
||||
"""SDK-defined: a flow request that keeps redirecting within its origin is followed a bounded
|
||||
number of times; the redirect after that is handed to the flow unfollowed."""
|
||||
flow, request = await _start_discovery(oauth_provider)
|
||||
|
||||
hops = 0
|
||||
while str(request.url) != "https://api.example.com/.well-known/oauth-protected-resource":
|
||||
request = await flow.asend(
|
||||
await _redirect(request, 307, f"/.well-known/oauth-protected-resource/v1/mcp/{hops}")
|
||||
)
|
||||
hops += 1
|
||||
|
||||
assert hops == 6 # five followed, the sixth handed back and taken as "try the next URL"
|
||||
await flow.aclose()
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
@pytest.mark.parametrize(("status", "keep_trying"), [(404, True), (307, True), (500, False)])
|
||||
async def test_auth_metadata_response_says_whether_to_try_the_next_discovery_url(
|
||||
status: int, keep_trying: bool
|
||||
) -> None:
|
||||
"""SDK-defined: a 4xx or a 3xx (redirects are not followed on these requests) from a discovery
|
||||
candidate means the metadata is not served there and the next well-known URL is tried; a 5xx
|
||||
stops discovery."""
|
||||
assert await handle_auth_metadata_response(httpx.Response(status)) == (keep_trying, None)
|
||||
|
||||
|
||||
class TestRegistrationResponse:
|
||||
"""Test client registration response handling."""
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_handle_registration_response_reads_before_accessing_text(self):
|
||||
"""Test that response.aread() is called before accessing response.text."""
|
||||
"""The registration error carries the response text, which for a streamed response means
|
||||
reading it first (a streamed httpx response raises ResponseNotRead otherwise)."""
|
||||
response = httpx.Response(400, stream=httpx.ByteStream(b"Registration failed with error"))
|
||||
|
||||
# Track if aread() was called
|
||||
class MockResponse(httpx.Response):
|
||||
def __init__(self):
|
||||
self.status_code = 400
|
||||
self._aread_called = False
|
||||
self._text = "Registration failed with error"
|
||||
with pytest.raises(OAuthRegistrationError) as exc_info:
|
||||
await handle_registration_response(response)
|
||||
|
||||
async def aread(self):
|
||||
self._aread_called = True
|
||||
return b"test content"
|
||||
assert str(exc_info.value) == snapshot("Registration failed: 400 Registration failed with error")
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
if not self._aread_called:
|
||||
raise RuntimeError("Response.text accessed before response.aread()") # pragma: no cover
|
||||
return self._text
|
||||
@pytest.mark.anyio
|
||||
async def test_registration_error_names_an_unfollowed_redirect(self):
|
||||
"""SDK-defined: when the registration endpoint answered with a redirect that was not followed,
|
||||
the error says where it pointed (without userinfo or query) instead of only the bare status."""
|
||||
request = httpx.Request("POST", "https://as.example/register")
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(
|
||||
lambda r: httpx.Response(307, headers={"location": "https://u:p@elsewhere.example/register?state=x"})
|
||||
)
|
||||
) as client:
|
||||
response = await client.send(request)
|
||||
|
||||
mock_response = MockResponse()
|
||||
with pytest.raises(OAuthRegistrationError) as exc_info:
|
||||
await handle_registration_response(response)
|
||||
|
||||
# This should call aread() before accessing text
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
await handle_registration_response(mock_response)
|
||||
|
||||
# Verify aread() was called
|
||||
assert mock_response._aread_called
|
||||
# Verify the error message includes the response text
|
||||
assert "Registration failed: 400" in str(exc_info.value)
|
||||
assert str(exc_info.value) == snapshot(
|
||||
"Registration failed: 307 (redirected to https://elsewhere.example/register; not followed) "
|
||||
)
|
||||
|
||||
|
||||
class TestCreateClientRegistrationRequest:
|
||||
|
|
|
|||
|
|
@ -1,16 +1,27 @@
|
|||
"""Tests for httpx utility functions."""
|
||||
"""Tests for the httpx helpers the client transports are built on."""
|
||||
|
||||
from collections.abc import AsyncGenerator, AsyncIterator
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from mcp.shared._httpx_utils import create_mcp_http_client
|
||||
from mcp.shared._httpx_utils import (
|
||||
create_mcp_http_client,
|
||||
request_within_origin,
|
||||
sse_within_origin,
|
||||
stream_within_origin,
|
||||
)
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
|
||||
def test_default_settings():
|
||||
"""Test that default settings are applied correctly."""
|
||||
def test_default_client_uses_mcp_timeouts_and_httpx_redirect_default():
|
||||
"""The factory applies the transports' timeouts and leaves redirect following to the transports."""
|
||||
client = create_mcp_http_client()
|
||||
|
||||
assert client.follow_redirects is True
|
||||
assert client.timeout.connect == 30.0
|
||||
assert client.follow_redirects is False
|
||||
assert client.timeout == httpx.Timeout(30.0, read=300.0)
|
||||
|
||||
|
||||
def test_custom_parameters():
|
||||
|
|
@ -22,3 +33,253 @@ def test_custom_parameters():
|
|||
|
||||
assert client.headers["Authorization"] == "Bearer token"
|
||||
assert client.timeout.connect == 60.0
|
||||
|
||||
|
||||
class _Body(httpx.AsyncByteStream):
|
||||
"""A response body served as a real stream, recording whether the client closed it."""
|
||||
|
||||
def __init__(self, data: bytes, closed: list[bool]) -> None:
|
||||
self._data = data
|
||||
self._closed = closed
|
||||
|
||||
async def __aiter__(self) -> AsyncIterator[bytes]:
|
||||
yield self._data
|
||||
|
||||
async def aclose(self) -> None:
|
||||
self._closed.append(True)
|
||||
|
||||
|
||||
def _recording_client(
|
||||
redirects: dict[str, tuple[int, str]], **client_kwargs: Any
|
||||
) -> tuple[httpx.AsyncClient, list[str], list[bool]]:
|
||||
"""A client whose server redirects each URL in `redirects` (status, Location) and answers 200
|
||||
to anything else; plus the `METHOD url` lines the server received and one entry per redirect
|
||||
response body the client closed."""
|
||||
received: list[str] = []
|
||||
closed: list[bool] = []
|
||||
|
||||
def serve(request: httpx.Request) -> httpx.Response:
|
||||
received.append(f"{request.method} {request.url}")
|
||||
if str(request.url) in redirects:
|
||||
status, location = redirects[str(request.url)]
|
||||
return httpx.Response(status, headers={"location": location}, stream=_Body(b"moved", closed))
|
||||
return httpx.Response(200, text=request.content.decode() or "ok")
|
||||
|
||||
return httpx.AsyncClient(transport=httpx.MockTransport(serve), **client_kwargs), received, closed
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("url", "location"),
|
||||
[
|
||||
("http://mcp.example/mcp", "http://mcp.example/mcp/"),
|
||||
("http://mcp.example/mcp", "/other/path"),
|
||||
("http://mcp.example:8080/mcp", "http://mcp.example:8080/v2/mcp"),
|
||||
("http://mcp.example/mcp", "http://MCP.EXAMPLE:80/mcp/"),
|
||||
("http://mcp.example/mcp", "https://mcp.example:443/mcp"),
|
||||
],
|
||||
)
|
||||
async def test_redirect_within_origin_is_followed_with_method_and_body(url: str, location: str):
|
||||
"""A redirect that stays on the request's origin (or upgrades it to https) is followed, and a
|
||||
307 keeps the method and body (SDK-defined policy; the re-send itself is httpx's)."""
|
||||
client, received, closed = _recording_client({url: (307, location)})
|
||||
|
||||
async with client, stream_within_origin(client, "POST", url, content=b"payload") as response:
|
||||
await response.aread()
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.text == "payload"
|
||||
assert received == [f"POST {url}", f"POST {httpx.URL(url).join(location)}"]
|
||||
assert closed == [True]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"location",
|
||||
[
|
||||
"http://other.example/mcp",
|
||||
"http://mcp.example:8080/mcp",
|
||||
"http://sub.mcp.example/mcp",
|
||||
"https://mcp.example:8443/mcp",
|
||||
"ftp://mcp.example/mcp",
|
||||
],
|
||||
)
|
||||
async def test_redirect_outside_origin_is_not_followed(location: str):
|
||||
"""A redirect to another origin is handed back unfollowed, the way httpx hands back a redirect
|
||||
with following off, and the location is never requested (SDK-defined policy)."""
|
||||
url = "http://mcp.example/mcp"
|
||||
client, received, closed = _recording_client({url: (307, location)})
|
||||
|
||||
async with client, stream_within_origin(client, "POST", url, content=b"payload") as response:
|
||||
pass
|
||||
|
||||
assert response.status_code == 307
|
||||
assert response.next_request is not None
|
||||
assert response.next_request.url == location
|
||||
assert received == [f"POST {url}"]
|
||||
assert closed == [True]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status", [301, 302, 303])
|
||||
async def test_method_changing_redirect_of_a_post_is_not_followed(status: int):
|
||||
"""httpx turns a POST into a body-less GET for 301/302/303, which would drop the message, so a
|
||||
same-origin redirect with one of those codes is handed back unfollowed (SDK-defined)."""
|
||||
url = "http://mcp.example/mcp"
|
||||
client, received, _ = _recording_client({url: (status, "/mcp/")})
|
||||
|
||||
async with client, stream_within_origin(client, "POST", url, content=b"payload") as response:
|
||||
pass
|
||||
|
||||
assert response.status_code == status
|
||||
assert received == [f"POST {url}"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status", [301, 302, 303, 307, 308])
|
||||
async def test_same_origin_redirect_of_a_get_is_followed_for_every_redirect_status(status: int):
|
||||
"""A GET keeps its method under every redirect status, so the SSE GET follows all of them
|
||||
within the origin (SDK-defined policy over httpx's method rules)."""
|
||||
url = "http://mcp.example/sse"
|
||||
client, received, _ = _recording_client({url: (status, "/sse/")})
|
||||
|
||||
async with client, stream_within_origin(client, "GET", url) as response:
|
||||
await response.aread()
|
||||
|
||||
assert response.status_code == 200
|
||||
assert received == [f"GET {url}", "GET http://mcp.example/sse/"]
|
||||
|
||||
|
||||
async def test_https_to_http_on_same_host_is_outside_origin():
|
||||
"""Only the upgrade direction counts as staying on the origin; a downgrade is not followed."""
|
||||
url = "https://mcp.example/mcp"
|
||||
client, received, _ = _recording_client({url: (302, "http://mcp.example/mcp")})
|
||||
|
||||
async with client, stream_within_origin(client, "GET", url) as response:
|
||||
pass
|
||||
|
||||
assert response.status_code == 302
|
||||
assert received == [f"GET {url}"]
|
||||
|
||||
|
||||
async def test_client_configured_to_follow_redirects_is_still_scoped_to_origin():
|
||||
"""The client's own follow_redirects=True does not widen the policy: the transport helper
|
||||
decides per request (SDK-defined)."""
|
||||
url = "http://mcp.example/mcp"
|
||||
client, received, _ = _recording_client({url: (307, "http://other.example/mcp")}, follow_redirects=True)
|
||||
|
||||
async with client, stream_within_origin(client, "POST", url) as response:
|
||||
pass
|
||||
|
||||
assert response.status_code == 307
|
||||
assert received == [f"POST {url}"]
|
||||
|
||||
|
||||
async def test_redirect_past_the_client_max_redirects_budget_is_handed_back_unfollowed():
|
||||
"""Same-origin hops are bounded by the client's max_redirects; the redirect after that is not
|
||||
followed but handed back like any other, so a loop fails the one call rather than raising
|
||||
(SDK-defined; max_redirects=0 therefore means "follow none")."""
|
||||
url = "http://mcp.example/a"
|
||||
client, received, closed = _recording_client(
|
||||
{
|
||||
"http://mcp.example/a": (307, "/b"),
|
||||
"http://mcp.example/b": (307, "/c"),
|
||||
"http://mcp.example/c": (307, "/d"),
|
||||
},
|
||||
max_redirects=2,
|
||||
)
|
||||
|
||||
async with client:
|
||||
response = await request_within_origin(client, "GET", url)
|
||||
|
||||
assert response.status_code == 307
|
||||
assert response.next_request is not None
|
||||
assert response.next_request.url == "http://mcp.example/d"
|
||||
assert received == ["GET http://mcp.example/a", "GET http://mcp.example/b", "GET http://mcp.example/c"]
|
||||
assert closed == [True, True, True]
|
||||
|
||||
|
||||
async def test_redirect_location_with_userinfo_is_not_followed():
|
||||
"""A Location carrying user:password is handed back unfollowed even within the origin, since
|
||||
httpx would otherwise send that userinfo as Basic auth (SDK-defined)."""
|
||||
url = "http://mcp.example/mcp"
|
||||
client, received, _ = _recording_client({url: (307, "http://user:secret@mcp.example/mcp/")})
|
||||
|
||||
async with client, stream_within_origin(client, "POST", url) as response:
|
||||
pass
|
||||
|
||||
assert response.status_code == 307
|
||||
assert received == [f"POST {url}"]
|
||||
|
||||
|
||||
async def test_userinfo_of_the_configured_url_kept_by_a_relative_location_is_followed():
|
||||
"""Userinfo the caller put in the endpoint URL is carried over by a relative Location (URL join
|
||||
keeps the authority); that is the caller's own credential for the same origin, so the redirect
|
||||
is followed as httpx itself would (SDK-defined)."""
|
||||
url = "http://user:secret@mcp.example/mcp"
|
||||
client, received, _ = _recording_client({url: (307, "/mcp/")})
|
||||
|
||||
async with client, stream_within_origin(client, "POST", url, content=b"payload") as response:
|
||||
await response.aread()
|
||||
|
||||
assert response.status_code == 200
|
||||
assert received == [f"POST {url}", "POST http://user:secret@mcp.example/mcp/"]
|
||||
|
||||
|
||||
async def test_request_within_origin_returns_a_read_response():
|
||||
"""The non-streaming form hands back a response whose body is already read."""
|
||||
url = "http://mcp.example/mcp"
|
||||
client, received, _ = _recording_client({url: (307, "/mcp/")})
|
||||
|
||||
async with client:
|
||||
response = await request_within_origin(client, "DELETE", url)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.text == "ok"
|
||||
assert received == [f"DELETE {url}", "DELETE http://mcp.example/mcp/"]
|
||||
|
||||
|
||||
async def test_sse_within_origin_sends_event_stream_headers_and_caller_headers():
|
||||
"""The SSE form asks for an event stream exactly as httpx_sse.aconnect_sse() does, merged case-insensitively
|
||||
with the caller's headers, and yields an EventSource over the final response."""
|
||||
seen: list[httpx.Headers] = []
|
||||
|
||||
def serve(request: httpx.Request) -> httpx.Response:
|
||||
seen.append(request.headers)
|
||||
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text="data: hello\n\n")
|
||||
|
||||
client = httpx.AsyncClient(transport=httpx.MockTransport(serve))
|
||||
async with client:
|
||||
async with sse_within_origin(client, "http://mcp.example/sse") as source:
|
||||
events = [event.data async for event in source.aiter_sse()]
|
||||
async with sse_within_origin(client, "http://mcp.example/sse", headers={"accept": "x/y", "k": "v"}):
|
||||
pass
|
||||
|
||||
assert events == ["hello"]
|
||||
assert seen[0]["accept"] == "text/event-stream"
|
||||
assert seen[0]["cache-control"] == "no-store"
|
||||
assert seen[1].get_list("accept") == ["x/y"]
|
||||
assert seen[1]["cache-control"] == "no-store"
|
||||
assert seen[1]["k"] == "v"
|
||||
|
||||
|
||||
async def test_auth_flow_requests_are_not_redirected():
|
||||
"""Requests an httpx Auth flow issues while a transport request is in flight (a token refresh,
|
||||
say) inherit the per-request no-follow setting, so a redirect on them is handed back to the
|
||||
auth flow rather than followed (httpx behaviour the transports rely on)."""
|
||||
received: list[str] = []
|
||||
|
||||
class TokenThenRequest(httpx.Auth):
|
||||
async def async_auth_flow(self, request: httpx.Request) -> AsyncGenerator[httpx.Request, httpx.Response]:
|
||||
token_response = yield httpx.Request("POST", "http://mcp.example/token", content=b"grant")
|
||||
request.headers["x-token-status"] = str(token_response.status_code)
|
||||
yield request
|
||||
|
||||
def serve(request: httpx.Request) -> httpx.Response:
|
||||
received.append(f"{request.method} {request.url}")
|
||||
if request.url.path == "/token":
|
||||
return httpx.Response(307, headers={"location": "http://other.example/token"})
|
||||
return httpx.Response(200, text=request.headers["x-token-status"])
|
||||
|
||||
client = httpx.AsyncClient(transport=httpx.MockTransport(serve), auth=TokenThenRequest(), follow_redirects=True)
|
||||
async with client:
|
||||
response = await request_within_origin(client, "POST", "http://mcp.example/mcp")
|
||||
|
||||
assert response.text == "307"
|
||||
assert received == ["POST http://mcp.example/token", "POST http://mcp.example/mcp"]
|
||||
|
|
|
|||
|
|
@ -4,13 +4,12 @@ import socket
|
|||
import time
|
||||
from collections.abc import AsyncGenerator, Generator
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
from unittest.mock import Mock
|
||||
|
||||
import anyio
|
||||
import httpx
|
||||
import pytest
|
||||
import uvicorn
|
||||
from httpx_sse import ServerSentEvent
|
||||
from inline_snapshot import snapshot
|
||||
from pydantic import AnyUrl
|
||||
from starlette.applications import Starlette
|
||||
|
|
@ -26,6 +25,7 @@ from mcp.server import Server
|
|||
from mcp.server.sse import SseServerTransport
|
||||
from mcp.server.transport_security import TransportSecuritySettings
|
||||
from mcp.shared.exceptions import McpError
|
||||
from mcp.shared.message import SessionMessage
|
||||
from mcp.types import (
|
||||
EmptyResult,
|
||||
ErrorData,
|
||||
|
|
@ -538,12 +538,6 @@ def test_sse_server_transport_endpoint_validation(endpoint: str, expected_result
|
|||
assert sse._endpoint.startswith("/")
|
||||
|
||||
|
||||
# ResourceWarning filter: When mocking aconnect_sse, the sse_client's internal task
|
||||
# group doesn't receive proper cancellation signals, so the sse_reader task's finally
|
||||
# block (which closes read_stream_writer) doesn't execute. This is a test artifact -
|
||||
# the actual code path (`if not sse.data: continue`) IS exercised and works correctly.
|
||||
# Production code with real SSE connections cleans up properly.
|
||||
@pytest.mark.filterwarnings("ignore::ResourceWarning")
|
||||
@pytest.mark.anyio
|
||||
async def test_sse_client_handles_empty_keepalive_pings() -> None:
|
||||
"""Test that SSE client properly handles empty data lines (keep-alive pings).
|
||||
|
|
@ -552,10 +546,10 @@ async def test_sse_client_handles_empty_keepalive_pings() -> None:
|
|||
send an SSE event consisting of an event ID and an empty data field in order
|
||||
to prime the client to reconnect."
|
||||
|
||||
This test mocks the SSE event stream to include empty "message" events and
|
||||
verifies the client skips them without crashing.
|
||||
The event stream served here carries an endpoint event, an empty "message"
|
||||
event (the case under test), then a real response; the client must skip the
|
||||
empty one and deliver the response.
|
||||
"""
|
||||
# Build a proper JSON-RPC response using types (not hardcoded strings)
|
||||
init_result = InitializeResult(
|
||||
protocolVersion="2024-11-05",
|
||||
capabilities=ServerCapabilities(),
|
||||
|
|
@ -567,38 +561,82 @@ async def test_sse_client_handles_empty_keepalive_pings() -> None:
|
|||
result=init_result.model_dump(by_alias=True, exclude_none=True),
|
||||
)
|
||||
response_json = response.model_dump_json(by_alias=True, exclude_none=True)
|
||||
event_stream = (
|
||||
"event: endpoint\ndata: /messages/?session_id=abc123\n\n"
|
||||
"event: message\ndata: \n\n"
|
||||
f"event: message\ndata: {response_json}\n\n"
|
||||
)
|
||||
|
||||
# Create mock SSE events using httpx_sse's ServerSentEvent
|
||||
async def mock_aiter_sse() -> AsyncGenerator[ServerSentEvent, None]:
|
||||
# First: endpoint event
|
||||
yield ServerSentEvent(event="endpoint", data="/messages/?session_id=abc123")
|
||||
# Empty data keep-alive ping - this is what we're testing
|
||||
yield ServerSentEvent(event="message", data="")
|
||||
# Real JSON-RPC response
|
||||
yield ServerSentEvent(event="message", data=response_json)
|
||||
def serve(request: httpx.Request) -> httpx.Response:
|
||||
assert request.url.path == "/sse"
|
||||
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=event_stream)
|
||||
|
||||
mock_event_source = MagicMock()
|
||||
mock_event_source.aiter_sse.return_value = mock_aiter_sse()
|
||||
mock_event_source.response = MagicMock()
|
||||
mock_event_source.response.raise_for_status = MagicMock()
|
||||
def factory(
|
||||
headers: dict[str, str] | None = None,
|
||||
timeout: httpx.Timeout | None = None,
|
||||
auth: httpx.Auth | None = None,
|
||||
) -> httpx.AsyncClient:
|
||||
return httpx.AsyncClient(transport=httpx.MockTransport(serve))
|
||||
|
||||
mock_aconnect_sse = MagicMock()
|
||||
mock_aconnect_sse.__aenter__ = AsyncMock(return_value=mock_event_source)
|
||||
mock_aconnect_sse.__aexit__ = AsyncMock(return_value=None)
|
||||
|
||||
mock_client = MagicMock()
|
||||
mock_client.__aenter__ = AsyncMock(return_value=mock_client)
|
||||
mock_client.__aexit__ = AsyncMock(return_value=None)
|
||||
mock_client.post = AsyncMock(return_value=MagicMock(status_code=200, raise_for_status=MagicMock()))
|
||||
|
||||
with (
|
||||
patch("mcp.client.sse.create_mcp_http_client", return_value=mock_client),
|
||||
patch("mcp.client.sse.aconnect_sse", return_value=mock_aconnect_sse),
|
||||
):
|
||||
async with sse_client("http://test/sse") as (read_stream, _):
|
||||
# Read the message - should skip the empty one and get the real response
|
||||
with anyio.fail_after(5):
|
||||
async with sse_client("http://test/sse", httpx_client_factory=factory) as (read_stream, _):
|
||||
msg = await read_stream.receive()
|
||||
# If we get here without error, the empty message was skipped successfully
|
||||
assert not isinstance(msg, Exception)
|
||||
assert isinstance(msg, SessionMessage)
|
||||
assert isinstance(msg.message.root, types.JSONRPCResponse)
|
||||
assert msg.message.root.id == 1
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_sse_client_follows_redirect_within_origin_on_connect() -> None:
|
||||
"""SDK-defined: a redirect of the SSE GET that stays on the endpoint's origin is followed by
|
||||
the transport itself, with a client left at httpx's no-follow default."""
|
||||
received: list[str] = []
|
||||
|
||||
def serve(request: httpx.Request) -> httpx.Response:
|
||||
received.append(str(request.url))
|
||||
if request.url.path == "/sse":
|
||||
return httpx.Response(307, headers={"location": "/sse/"})
|
||||
assert request.url.path == "/sse/"
|
||||
return httpx.Response(
|
||||
200, headers={"content-type": "text/event-stream"}, text="event: endpoint\ndata: /messages/\n\n"
|
||||
)
|
||||
|
||||
def factory(
|
||||
headers: dict[str, str] | None = None,
|
||||
timeout: httpx.Timeout | None = None,
|
||||
auth: httpx.Auth | None = None,
|
||||
) -> httpx.AsyncClient:
|
||||
return httpx.AsyncClient(transport=httpx.MockTransport(serve))
|
||||
|
||||
with anyio.fail_after(5):
|
||||
async with sse_client("http://test/sse", httpx_client_factory=factory):
|
||||
pass
|
||||
|
||||
assert received == ["http://test/sse", "http://test/sse/"]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_sse_client_does_not_follow_redirect_to_another_origin_on_connect() -> None:
|
||||
"""SDK-defined: a redirect of the SSE GET to another origin is not followed, even with a client
|
||||
configured to follow redirects: connecting fails with HTTPStatusError for the redirect response
|
||||
(raised inside sse_client's task group) and that origin is never contacted."""
|
||||
received: list[str] = []
|
||||
|
||||
def serve(request: httpx.Request) -> httpx.Response:
|
||||
received.append(str(request.url))
|
||||
return httpx.Response(307, headers={"location": "http://other.example/sse"})
|
||||
|
||||
def factory(
|
||||
headers: dict[str, str] | None = None,
|
||||
timeout: httpx.Timeout | None = None,
|
||||
auth: httpx.Auth | None = None,
|
||||
) -> httpx.AsyncClient:
|
||||
return httpx.AsyncClient(transport=httpx.MockTransport(serve), follow_redirects=True)
|
||||
|
||||
with anyio.fail_after(5):
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
async with sse_client("http://test/sse", httpx_client_factory=factory):
|
||||
pytest.fail("should not connect") # pragma: no cover
|
||||
|
||||
assert exc_info.group_contains(httpx.HTTPStatusError, match="307 Temporary Redirect")
|
||||
assert received == ["http://test/sse"]
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import pytest
|
|||
import requests
|
||||
import uvicorn
|
||||
from httpx_sse import ServerSentEvent
|
||||
from inline_snapshot import snapshot
|
||||
from pydantic import AnyUrl
|
||||
from starlette.applications import Starlette
|
||||
from starlette.requests import Request
|
||||
|
|
@ -1255,41 +1256,36 @@ async def test_streamable_http_client_session_termination(basic_server: None, ba
|
|||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_streamable_http_client_session_termination_204(
|
||||
basic_server: None, basic_server_url: str, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
async def test_streamable_http_client_session_termination_204(basic_server: None, basic_server_url: str):
|
||||
"""Test client session termination functionality with a 204 response.
|
||||
|
||||
This test patches the httpx client to return a 204 response for DELETEs.
|
||||
The server answers the DELETE with 200; a wrapping HTTP transport rewrites that to 204 on the
|
||||
way back, which is what some servers send.
|
||||
"""
|
||||
|
||||
# Save the original delete method to restore later
|
||||
original_delete = httpx.AsyncClient.delete
|
||||
class AnswerDeleteWith204(httpx.AsyncBaseTransport):
|
||||
def __init__(self) -> None:
|
||||
self.inner = httpx.AsyncHTTPTransport()
|
||||
|
||||
# Mock the client's delete method to return a 204
|
||||
async def mock_delete(self: httpx.AsyncClient, *args: Any, **kwargs: Any) -> httpx.Response:
|
||||
# Call the original method to get the real response
|
||||
response = await original_delete(self, *args, **kwargs)
|
||||
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
|
||||
response = await self.inner.handle_async_request(request)
|
||||
if request.method != "DELETE" or response.status_code != 200:
|
||||
return response
|
||||
await response.aread()
|
||||
return httpx.Response(204, headers=response.headers, request=request)
|
||||
|
||||
# Create a new response with 204 status code but same headers
|
||||
mocked_response = httpx.Response(
|
||||
204,
|
||||
headers=response.headers,
|
||||
content=response.content,
|
||||
request=response.request,
|
||||
)
|
||||
return mocked_response
|
||||
|
||||
# Apply the patch to the httpx client
|
||||
monkeypatch.setattr(httpx.AsyncClient, "delete", mock_delete)
|
||||
async def aclose(self) -> None:
|
||||
await self.inner.aclose()
|
||||
|
||||
captured_session_id = None
|
||||
|
||||
# Create the streamable_http_client with a custom httpx client to capture headers
|
||||
async with streamable_http_client(f"{basic_server_url}/mcp") as (
|
||||
read_stream,
|
||||
write_stream,
|
||||
get_session_id,
|
||||
async with (
|
||||
httpx.AsyncClient(transport=AnswerDeleteWith204()) as terminating_client,
|
||||
streamable_http_client(f"{basic_server_url}/mcp", http_client=terminating_client) as (
|
||||
read_stream,
|
||||
write_stream,
|
||||
get_session_id,
|
||||
),
|
||||
):
|
||||
async with ClientSession(read_stream, write_stream) as session:
|
||||
# Initialize the session
|
||||
|
|
@ -2297,7 +2293,7 @@ async def test_streamable_http_client_does_not_mutate_provided_client(
|
|||
"Authorization": "Bearer test-token",
|
||||
}
|
||||
|
||||
async with httpx.AsyncClient(headers=original_headers, follow_redirects=True) as custom_client:
|
||||
async with httpx.AsyncClient(headers=original_headers) as custom_client:
|
||||
# Use the client with streamable_http_client
|
||||
async with streamable_http_client(f"{basic_server_url}/mcp", http_client=custom_client) as (
|
||||
read_stream,
|
||||
|
|
@ -2328,7 +2324,7 @@ async def test_streamable_http_client_mcp_headers_override_defaults(
|
|||
# httpx.AsyncClient has default "accept: */*" header
|
||||
# We need to verify that our MCP accept header overrides it in actual requests
|
||||
|
||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||
async with httpx.AsyncClient() as client:
|
||||
# Verify client has default accept header
|
||||
assert client.headers.get("accept") == "*/*"
|
||||
|
||||
|
|
@ -2366,7 +2362,7 @@ async def test_streamable_http_client_preserves_custom_with_mcp_headers(
|
|||
"Authorization": "Bearer test-token",
|
||||
}
|
||||
|
||||
async with httpx.AsyncClient(headers=custom_headers, follow_redirects=True) as client:
|
||||
async with httpx.AsyncClient(headers=custom_headers) as client:
|
||||
async with streamable_http_client(f"{basic_server_url}/mcp", http_client=client) as (
|
||||
read_stream,
|
||||
write_stream,
|
||||
|
|
@ -2426,3 +2422,172 @@ async def test_streamablehttp_client_deprecation_warning(basic_server: None, bas
|
|||
await session.initialize()
|
||||
tools = await session.list_tools()
|
||||
assert len(tools.tools) > 0
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_trailing_slash_redirect_within_origin_is_followed_by_the_transport(
|
||||
basic_server: None, basic_server_url: str
|
||||
) -> None:
|
||||
"""SDK-defined: a redirect that stays on the endpoint's origin (here Starlette's Mount sending
|
||||
/mcp to /mcp/) is followed by the transport itself, so a caller-supplied client left at
|
||||
httpx's no-follow default still connects."""
|
||||
urls: list[str] = []
|
||||
|
||||
async def record(request: httpx.Request) -> None:
|
||||
urls.append(str(request.url))
|
||||
|
||||
with anyio.fail_after(10):
|
||||
async with (
|
||||
httpx.AsyncClient(event_hooks={"request": [record]}) as http,
|
||||
streamable_http_client(f"{basic_server_url}/mcp", http_client=http) as (read_stream, write_stream, _),
|
||||
ClientSession(read_stream, write_stream) as session,
|
||||
):
|
||||
result = await session.initialize()
|
||||
|
||||
assert result.serverInfo.name == SERVER_NAME
|
||||
assert urls[:2] == [f"{basic_server_url}/mcp", f"{basic_server_url}/mcp/"]
|
||||
|
||||
|
||||
def _leaf_exception(exc: BaseException) -> BaseException:
|
||||
"""The one exception inside the (possibly nested) exception group an anyio task group raises."""
|
||||
while (inner := getattr(exc, "exceptions", None)) is not None:
|
||||
(exc,) = inner
|
||||
return exc
|
||||
|
||||
|
||||
async def _assert_redirected_post_fails(url: str, location: str, expected_message: str) -> None:
|
||||
"""Send one request through streamable_http_client, with a client configured to follow redirects,
|
||||
to a server answering `url` with a 307 to `location`, and check that the connection ends with
|
||||
HTTPStatusError carrying `expected_message` and that nothing but `url` was requested."""
|
||||
urls: list[str] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
urls.append(str(request.url))
|
||||
return httpx.Response(307, headers={"location": location})
|
||||
|
||||
with anyio.fail_after(5):
|
||||
# The request's POST fails inside the transport's task group, which ends the connection.
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
async with ( # pragma: no branch
|
||||
httpx.AsyncClient(transport=httpx.MockTransport(handler), follow_redirects=True) as http,
|
||||
streamable_http_client(url, http_client=http) as (read_stream, write_stream, _),
|
||||
read_stream,
|
||||
write_stream,
|
||||
):
|
||||
request = JSONRPCRequest(jsonrpc="2.0", id=1, method="tools/list", params={})
|
||||
await write_stream.send(SessionMessage(JSONRPCMessage(request)))
|
||||
await read_stream.receive()
|
||||
error = _leaf_exception(exc_info.value)
|
||||
assert isinstance(error, httpx.HTTPStatusError)
|
||||
assert error.response.status_code == 307
|
||||
assert str(error) == expected_message
|
||||
assert urls == [url]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_redirect_to_another_origin_is_not_followed_and_fails_the_request() -> None:
|
||||
"""SDK-defined: a redirect pointing outside the endpoint's origin is not followed, whatever the
|
||||
caller's client is configured to do: nothing is sent to the other origin, and the request fails
|
||||
the way any non-2xx response does, with HTTPStatusError naming the location."""
|
||||
await _assert_redirected_post_fails(
|
||||
"http://mcp.example/mcp",
|
||||
"http://other.example/x",
|
||||
snapshot(
|
||||
"Redirect to http://other.example/x not followed; use that URL as the endpoint if it is the intended server"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_https_endpoint_redirected_to_plain_http_is_explained_and_the_https_form_suggested() -> None:
|
||||
"""SDK-authored text: a redirect of an HTTPS endpoint to plain HTTP on the same host (the usual
|
||||
sign of a TLS-terminating proxy the server does not trust) never suggests the http:// URL."""
|
||||
await _assert_redirected_post_fails(
|
||||
"https://mcp.example/mcp",
|
||||
"http://mcp.example/mcp/",
|
||||
snapshot("""\
|
||||
Redirect to http://mcp.example/mcp/ not followed: it would downgrade this HTTPS endpoint to plain HTTP.
|
||||
The server is likely behind a TLS-terminating proxy whose forwarded headers it does not trust,
|
||||
often combined with a trailing-slash difference. Try https://mcp.example/mcp/ instead, or fix the proxy settings.\
|
||||
"""),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_unfollowed_redirect_location_is_named_without_its_query_string() -> None:
|
||||
"""SDK-authored text: the location is reported without query or userinfo, which may carry state
|
||||
that does not belong in an error message or a log line."""
|
||||
await _assert_redirected_post_fails(
|
||||
"http://mcp.example/mcp",
|
||||
"https://idp.example/l?state=s3cr3t&nonce=n",
|
||||
snapshot(
|
||||
"Redirect to https://idp.example/l not followed; use that URL as the endpoint if it is the intended server"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_https_endpoint_redirected_to_plain_http_elsewhere_never_suggests_the_http_url() -> None:
|
||||
"""SDK-authored text: the downgrade explanation applies whatever host the http:// location names,
|
||||
so the message never offers a plain-HTTP URL as the endpoint to configure."""
|
||||
await _assert_redirected_post_fails(
|
||||
"https://mcp.example/mcp",
|
||||
"http://backend.lan:8000/mcp/",
|
||||
snapshot("""\
|
||||
Redirect to http://backend.lan:8000/mcp/ not followed: it would downgrade this HTTPS endpoint to plain HTTP.
|
||||
The server is likely behind a TLS-terminating proxy whose forwarded headers it does not trust,
|
||||
often combined with a trailing-slash difference. Try https://backend.lan:8000/mcp/ instead, or fix the proxy settings.\
|
||||
"""),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_get_stream_gives_up_without_retrying_when_the_endpoint_redirects_elsewhere() -> None:
|
||||
"""SDK-defined: the standalone GET stream is not opened through a redirect to another origin,
|
||||
and since the same GET would be redirected again the transport logs it and stops instead of
|
||||
spending its reconnection attempts."""
|
||||
gets: list[str] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
gets.append(str(request.url))
|
||||
return httpx.Response(307, headers={"location": "http://other.example/mcp"})
|
||||
|
||||
transport = StreamableHTTPTransport("http://test/mcp")
|
||||
transport.session_id = "session-1"
|
||||
writer, reader = anyio.create_memory_object_stream[SessionMessage | Exception](1)
|
||||
with anyio.fail_after(5):
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler), follow_redirects=True) as http:
|
||||
await transport.handle_get_stream(http, writer)
|
||||
writer.close()
|
||||
reader.close()
|
||||
assert gets == ["http://test/mcp"]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_resumption_redirected_elsewhere_fails_the_resumed_request() -> None:
|
||||
"""SDK-defined: a resumption GET answered with a redirect to another origin is not followed;
|
||||
the resumed request fails with HTTPStatusError naming the location, like a redirected POST."""
|
||||
seen: list[tuple[str, str | None]] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
seen.append((f"{request.method} {request.url}", request.headers.get("last-event-id")))
|
||||
return httpx.Response(307, headers={"location": "http://other.example/mcp"})
|
||||
|
||||
with anyio.fail_after(5):
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
async with ( # pragma: no branch
|
||||
httpx.AsyncClient(transport=httpx.MockTransport(handler), follow_redirects=True) as http,
|
||||
streamable_http_client("http://test/mcp", http_client=http) as (read_stream, write_stream, _),
|
||||
read_stream,
|
||||
write_stream,
|
||||
):
|
||||
request = JSONRPCRequest(jsonrpc="2.0", id="resume-1", method="tools/call", params={})
|
||||
metadata = ClientMessageMetadata(resumption_token="evt-41")
|
||||
await write_stream.send(SessionMessage(JSONRPCMessage(request), metadata=metadata))
|
||||
await read_stream.receive()
|
||||
error = _leaf_exception(exc_info.value)
|
||||
assert isinstance(error, httpx.HTTPStatusError)
|
||||
assert str(error) == snapshot(
|
||||
"Redirect to http://other.example/mcp not followed; use that URL as the endpoint if it is the intended server"
|
||||
)
|
||||
assert seen == [("GET http://test/mcp", "evt-41")]
|
||||
|
|
|
|||
Loading…
Reference in a new issue