Yield a Client connected to the server's legacy SSE transport, entirely in process.
(
server: Server | MCPServer,
*,
read_timeout_seconds: float | None = None,
sampling_callback: SamplingFnT | None = None,
list_roots_callback: ListRootsFnT | None = None,
logging_callback: LoggingFnT | None = None,
message_handler: MessageHandlerFnT | None = None,
client_info: Implementation | None = None,
elicitation_callback: ElicitationFnT | None = None,
spec_version: str = LATEST_HANDSHAKE_VERSION,
)
| 348 | |
| 349 | @asynccontextmanager |
| 350 | async def connect_over_sse( |
| 351 | server: Server | MCPServer, |
| 352 | *, |
| 353 | read_timeout_seconds: float | None = None, |
| 354 | sampling_callback: SamplingFnT | None = None, |
| 355 | list_roots_callback: ListRootsFnT | None = None, |
| 356 | logging_callback: LoggingFnT | None = None, |
| 357 | message_handler: MessageHandlerFnT | None = None, |
| 358 | client_info: Implementation | None = None, |
| 359 | elicitation_callback: ElicitationFnT | None = None, |
| 360 | spec_version: str = LATEST_HANDSHAKE_VERSION, |
| 361 | ) -> AsyncIterator[Client]: |
| 362 | """Yield a Client connected to the server's legacy SSE transport, entirely in process.""" |
| 363 | app, _ = build_sse_app(server) |
| 364 | |
| 365 | def httpx_client_factory( |
| 366 | headers: dict[str, str] | None = None, |
| 367 | timeout: httpx.Timeout | None = None, |
| 368 | auth: httpx.Auth | None = None, |
| 369 | ) -> httpx.AsyncClient: |
| 370 | # The SSE server transport's connect_sse runs the entire MCP session inside the GET |
| 371 | # request and only releases its streams after that request observes a disconnect, so the |
| 372 | # bridge must let the application drain rather than cancelling at close. |
| 373 | return httpx.AsyncClient( |
| 374 | transport=StreamingASGITransport(app, cancel_on_close=False), |
| 375 | base_url=BASE_URL, |
| 376 | headers=headers, |
| 377 | timeout=timeout, |
| 378 | auth=auth, |
| 379 | ) |
| 380 | |
| 381 | transport = sse_client(f"{BASE_URL}/sse", httpx_client_factory=httpx_client_factory) |
| 382 | async with Client( |
| 383 | transport, |
| 384 | # SSE is a legacy-only transport; the modern path has no SSE story. |
| 385 | mode="legacy", |
| 386 | read_timeout_seconds=read_timeout_seconds, |
| 387 | sampling_callback=sampling_callback, |
| 388 | list_roots_callback=list_roots_callback, |
| 389 | logging_callback=logging_callback, |
| 390 | message_handler=message_handler, |
| 391 | client_info=client_info, |
| 392 | elicitation_callback=elicitation_callback, |
| 393 | ) as client: |
| 394 | yield client |