|
| 1 | +"""Transport-parametrized connection factories for the interaction suite. |
| 2 | +
|
| 3 | +The `connect` fixture (see conftest.py) hands tests one of these factories so the same test body |
| 4 | +runs over the in-memory transport and over streamable HTTP without naming either: the factory is a |
| 5 | +drop-in replacement for constructing `Client(server, ...)` and yields the connected client. The |
| 6 | +streamable HTTP factory drives the server's real Starlette app through the in-process streaming |
| 7 | +bridge, so the full HTTP framing layer (session ids, SSE encoding, session management) runs with |
| 8 | +no sockets, threads, or subprocesses. |
| 9 | +""" |
| 10 | + |
| 11 | +from collections.abc import AsyncIterator |
| 12 | +from contextlib import AbstractAsyncContextManager, asynccontextmanager |
| 13 | +from typing import Protocol |
| 14 | + |
| 15 | +import httpx |
| 16 | + |
| 17 | +from mcp.client.client import Client |
| 18 | +from mcp.client.session import ElicitationFnT, ListRootsFnT, LoggingFnT, MessageHandlerFnT, SamplingFnT |
| 19 | +from mcp.client.streamable_http import streamable_http_client |
| 20 | +from mcp.server import Server |
| 21 | +from mcp.server.mcpserver import MCPServer |
| 22 | +from mcp.server.transport_security import TransportSecuritySettings |
| 23 | +from mcp.types import Implementation |
| 24 | +from tests.interaction.transports._bridge import StreamingASGITransport |
| 25 | + |
| 26 | +# The in-process app is mounted at this origin purely so URLs are well-formed; nothing listens here. |
| 27 | +_BASE_URL = "http://127.0.0.1:8000" |
| 28 | + |
| 29 | + |
| 30 | +class Connect(Protocol): |
| 31 | + """Connect a Client to a server over the transport selected by the `connect` fixture. |
| 32 | +
|
| 33 | + Accepts the same keyword arguments as `Client` and yields the connected client. |
| 34 | + """ |
| 35 | + |
| 36 | + def __call__( |
| 37 | + self, |
| 38 | + server: Server | MCPServer, |
| 39 | + *, |
| 40 | + read_timeout_seconds: float | None = None, |
| 41 | + sampling_callback: SamplingFnT | None = None, |
| 42 | + list_roots_callback: ListRootsFnT | None = None, |
| 43 | + logging_callback: LoggingFnT | None = None, |
| 44 | + message_handler: MessageHandlerFnT | None = None, |
| 45 | + client_info: Implementation | None = None, |
| 46 | + elicitation_callback: ElicitationFnT | None = None, |
| 47 | + ) -> AbstractAsyncContextManager[Client]: ... |
| 48 | + |
| 49 | + |
| 50 | +@asynccontextmanager |
| 51 | +async def connect_in_memory( |
| 52 | + server: Server | MCPServer, |
| 53 | + *, |
| 54 | + read_timeout_seconds: float | None = None, |
| 55 | + sampling_callback: SamplingFnT | None = None, |
| 56 | + list_roots_callback: ListRootsFnT | None = None, |
| 57 | + logging_callback: LoggingFnT | None = None, |
| 58 | + message_handler: MessageHandlerFnT | None = None, |
| 59 | + client_info: Implementation | None = None, |
| 60 | + elicitation_callback: ElicitationFnT | None = None, |
| 61 | +) -> AsyncIterator[Client]: |
| 62 | + """Yield a Client connected to the server over the in-memory transport.""" |
| 63 | + async with Client( |
| 64 | + server, |
| 65 | + read_timeout_seconds=read_timeout_seconds, |
| 66 | + sampling_callback=sampling_callback, |
| 67 | + list_roots_callback=list_roots_callback, |
| 68 | + logging_callback=logging_callback, |
| 69 | + message_handler=message_handler, |
| 70 | + client_info=client_info, |
| 71 | + elicitation_callback=elicitation_callback, |
| 72 | + ) as client: |
| 73 | + yield client |
| 74 | + |
| 75 | + |
| 76 | +@asynccontextmanager |
| 77 | +async def connect_over_streamable_http( |
| 78 | + server: Server | MCPServer, |
| 79 | + *, |
| 80 | + stateless_http: bool = False, |
| 81 | + json_response: bool = False, |
| 82 | + read_timeout_seconds: float | None = None, |
| 83 | + sampling_callback: SamplingFnT | None = None, |
| 84 | + list_roots_callback: ListRootsFnT | None = None, |
| 85 | + logging_callback: LoggingFnT | None = None, |
| 86 | + message_handler: MessageHandlerFnT | None = None, |
| 87 | + client_info: Implementation | None = None, |
| 88 | + elicitation_callback: ElicitationFnT | None = None, |
| 89 | +) -> AsyncIterator[Client]: |
| 90 | + """Yield a Client connected to the server's streamable HTTP app, entirely in process. |
| 91 | +
|
| 92 | + With the defaults this is the matrix leg (stateful sessions, SSE responses); the |
| 93 | + transport-specific tests pass `stateless_http` or `json_response` to select the other |
| 94 | + server modes. |
| 95 | + """ |
| 96 | + # DNS-rebinding protection validates Host/Origin headers against a real network attack that |
| 97 | + # cannot exist for an in-process ASGI app; leaving it on would also pull the origin-validation |
| 98 | + # branch (deliberately uncovered in src) into coverage. |
| 99 | + app = server.streamable_http_app( |
| 100 | + stateless_http=stateless_http, |
| 101 | + json_response=json_response, |
| 102 | + transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False), |
| 103 | + ) |
| 104 | + async with server.session_manager.run(): |
| 105 | + async with httpx.AsyncClient(transport=StreamingASGITransport(app), base_url=_BASE_URL) as http_client: |
| 106 | + transport = streamable_http_client(f"{_BASE_URL}/mcp", http_client=http_client) |
| 107 | + async with Client( |
| 108 | + transport, |
| 109 | + read_timeout_seconds=read_timeout_seconds, |
| 110 | + sampling_callback=sampling_callback, |
| 111 | + list_roots_callback=list_roots_callback, |
| 112 | + logging_callback=logging_callback, |
| 113 | + message_handler=message_handler, |
| 114 | + client_info=client_info, |
| 115 | + elicitation_callback=elicitation_callback, |
| 116 | + ) as client: |
| 117 | + yield client |
0 commit comments