|
4 | 4 | import mcp_types as types |
5 | 5 |
|
6 | 6 | from mcp.client import Client |
| 7 | +from mcp.client.subscriptions import ResourceUpdated, ToolsListChanged |
7 | 8 | from stories._harness import Target, run_client |
8 | 9 |
|
9 | | -SUBSCRIPTION_ID = "io.modelcontextprotocol/subscriptionId" |
10 | | - |
11 | 10 |
|
12 | 11 | async def main(target: Target, *, mode: str = "auto") -> None: |
13 | | - # Stream frames arrive as ordinary server notifications; `message_handler` |
14 | | - # is constructor-only on `Client`, so the list it fills exists first. |
15 | | - received: list[types.ServerNotification] = [] |
16 | | - arrival = anyio.Event() |
17 | | - |
18 | | - async def on_message(message: object) -> None: |
19 | | - nonlocal arrival |
20 | | - if isinstance( |
21 | | - message, |
22 | | - types.SubscriptionsAcknowledgedNotification |
23 | | - | types.ResourceUpdatedNotification |
24 | | - | types.ToolListChangedNotification, |
25 | | - ): |
26 | | - received.append(message) |
27 | | - arrival.set() |
28 | | - arrival = anyio.Event() |
29 | | - |
30 | | - async def wait_for(count: int) -> None: |
31 | | - with anyio.fail_after(10): |
32 | | - while len(received) < count: |
33 | | - await arrival.wait() |
34 | | - |
35 | | - async with Client(target, mode=mode, message_handler=on_message) as client: |
| 12 | + async with Client(target, mode=mode) as client: |
36 | 13 | before = await client.list_tools() |
37 | 14 | assert "search" not in {tool.name for tool in before.tools} |
38 | 15 |
|
39 | | - async with anyio.create_task_group() as tg: |
40 | | - # There is no client-side listen API yet, so the story drops to the |
41 | | - # `client.session` escape hatch. The request parks for the stream's |
42 | | - # lifetime, so it runs as a task; cancelling it releases the local |
43 | | - # awaiting scope. In-memory that also ends the server's stream; over |
44 | | - # HTTP today nothing aborts the POST, so the server-side stream ends |
45 | | - # when the connection closes (the `Client` exit right below). |
46 | | - async def listen() -> None: |
47 | | - request = types.SubscriptionsListenRequest( |
48 | | - params=types.SubscriptionsListenRequestParams( |
49 | | - notifications=types.SubscriptionFilter( |
50 | | - tools_list_changed=True, resource_subscriptions=["note://todo"] |
51 | | - ) |
52 | | - ) |
53 | | - ) |
54 | | - await client.session.send_request(request, types.SubscriptionsListenResult) |
55 | | - |
56 | | - tg.start_soon(listen) |
57 | | - |
58 | | - # ── the ack is the first frame: it echoes the honored filter, tagged ── |
59 | | - await wait_for(1) |
60 | | - ack = received[0] |
61 | | - assert isinstance(ack, types.SubscriptionsAcknowledgedNotification), ack |
62 | | - assert ack.params.notifications.tools_list_changed is True |
63 | | - assert ack.params.notifications.resource_subscriptions == ["note://todo"] |
64 | | - assert ack.params.meta is not None and SUBSCRIPTION_ID in ack.params.meta |
| 16 | + async with client.listen(tools_list_changed=True, resource_subscriptions=["note://todo"]) as sub: |
| 17 | + # ── entering waited for the ack: the honored filter is already in hand ── |
| 18 | + assert sub.honored.tools_list_changed is True |
| 19 | + assert sub.honored.resource_subscriptions == ["note://todo"] |
65 | 20 |
|
66 | 21 | # ── exact-URI filtering: an unsubscribed note edit stays silent ── |
67 | 22 | await client.call_tool("edit_note", {"name": "journal", "text": "day two"}) |
68 | | - # ── the subscribed URI delivers, carrying the same subscription id ── |
| 23 | + # ── the subscribed URI delivers ── |
69 | 24 | await client.call_tool("edit_note", {"name": "todo", "text": "water plants"}) |
70 | | - await wait_for(2) |
71 | | - updated = received[1] |
72 | | - assert isinstance(updated, types.ResourceUpdatedNotification), updated |
73 | | - assert updated.params.uri == "note://todo" |
74 | | - assert updated.params.meta is not None |
75 | | - assert updated.params.meta[SUBSCRIPTION_ID] == ack.params.meta[SUBSCRIPTION_ID] |
76 | | - assert len(received) == 2, "the journal edit must not have been delivered" |
| 25 | + with anyio.fail_after(10): |
| 26 | + event = await anext(sub) |
| 27 | + assert event == ResourceUpdated(uri="note://todo"), "the journal edit must not have been delivered" |
77 | 28 |
|
78 | 29 | # ── a runtime tool registration announces itself ── |
79 | 30 | await client.call_tool("enable_search", {}) |
80 | | - await wait_for(3) |
81 | | - assert isinstance(received[2], types.ToolListChangedNotification), received[2] |
82 | | - |
83 | | - # The client is done listening: cancel the parked request and let |
84 | | - # the connection teardown below end the stream server-side. |
85 | | - tg.cancel_scope.cancel() |
| 31 | + with anyio.fail_after(10): |
| 32 | + assert await anext(sub) == ToolsListChanged() |
86 | 33 |
|
87 | | - # list_changed told us to re-fetch - the new tool is callable, and the |
88 | | - # session outlives the closed stream. |
| 34 | + # ── leaving the block closed the stream; the session lives on ── |
89 | 35 | tools = await client.list_tools() |
90 | 36 | assert "search" in {tool.name for tool in tools.tools} |
91 | 37 | result = await client.call_tool("search", {"query": "water"}) |
|
0 commit comments