Add the client-side subscriptions/listen driver (#3047)
This commit is contained in:
parent
6d2e908f2b
commit
9bdc03d54e
1 changed files with 2587 additions and 295 deletions
20
docs_src/subscriptions/tutorial005.py
Normal file
20
docs_src/subscriptions/tutorial005.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import anyio
|
||||
|
||||
from mcp import Client
|
||||
from mcp.client.subscriptions 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)
|
||||
Loading…
Reference in a new issue