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
30
docs_src/subscriptions/tutorial003.py
Normal file
30
docs_src/subscriptions/tutorial003.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from mcp_types import TextResourceContents
|
||||
|
||||
from mcp import Client
|
||||
from mcp.client.subscriptions import ResourceUpdated, ToolsListChanged
|
||||
|
||||
BOARD = "board://sprint"
|
||||
|
||||
|
||||
async def read_board(client: Client, uri: str = BOARD) -> str:
|
||||
[contents] = (await client.read_resource(uri)).contents
|
||||
assert isinstance(contents, TextResourceContents)
|
||||
return contents.text
|
||||
|
||||
|
||||
async def follow_board(client: Client) -> None:
|
||||
async with client.listen(tools_list_changed=True, resource_subscriptions=[BOARD]) as sub:
|
||||
async for event in sub:
|
||||
match event:
|
||||
case ResourceUpdated(uri=uri):
|
||||
print(await read_board(client, uri))
|
||||
case ToolsListChanged():
|
||||
tools = await client.list_tools()
|
||||
print("tools:", [tool.name for tool in tools.tools])
|
||||
case _:
|
||||
pass # kinds the filter did not ask for never arrive
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client("http://localhost:8000/mcp") as client:
|
||||
await follow_board(client)
|
||||
Loading…
Reference in a new issue