Files
modelcontextprotocol--pytho…/docs_src/subscriptions/tutorial005.py
T
Max Isbey 8c88d2792b Tighten the public API surface: root exports, listen types, dead-code removal
Root `mcp` package: export `MCPServer` (alongside the existing `Client`,
so `from mcp import MCPServer, Client` works) and `NoBackChannelError`;
drop the v1-carryover `SamplingRole` alias (`mcp.types.Role`). `mcp.client`
now re-exports the types `Client.listen()` yields and raises: `Subscription`,
`SubscriptionLost`, `ListenNotSupportedError`.

Remove dead surface: `StreamableHTTPError`/`ResumptionError` (never raised
from a reachable path) and `StreamableHTTPTransport.get_session_id()` on the
client transport; the no-op `RegistrationRequest`/`TokenSuccessResponse`
auth-handler aliases; the `validate_extension_identifier` re-export alias;
the unreachable `__main__` guard in `mcp/cli/__init__.py`. The unused
lowlevel server `Context` moves to the private `mcp.server._context`
module.

`_handle_resumption_request` now takes the resumption token as a required
argument instead of re-deriving it and carrying an unreachable no-token
branch.

Enable ruff `RUF022` and sort every `__all__`; `mcp_types/__init__.py` is
exempted since its `__all__` is grouped by section comments on purpose.
2026-07-28 11:25:13 +00:00

21 lines
680 B
Python

import anyio
from mcp import Client
from mcp.client import SubscriptionLost
from .tutorial003 import read_board
async def keep_following(client: Client) -> None:
while True:
try:
async with client.listen(resource_subscriptions=["board://sprint"]) as sub:
print(await read_board(client)) # refetch: no replay across streams
async for _event in sub:
print(await read_board(client))
except SubscriptionLost:
pass
# Either ending means the stream is gone. Back off before re-listening:
# a graceful close may be the server shedding load.
await anyio.sleep(1)