|
| 1 | +"""Open a `subscriptions/listen` stream, watch one URI and the tool list, then close it.""" |
| 2 | + |
| 3 | +import anyio |
| 4 | +import mcp_types as types |
| 5 | + |
| 6 | +from mcp.client import Client |
| 7 | +from stories._harness import Target, run_client |
| 8 | + |
| 9 | +SUBSCRIPTION_ID = "io.modelcontextprotocol/subscriptionId" |
| 10 | + |
| 11 | + |
| 12 | +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: |
| 36 | + before = await client.list_tools() |
| 37 | + assert "search" not in {tool.name for tool in before.tools} |
| 38 | + |
| 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 and the client closes the stream by |
| 43 | + # cancelling it (the spec's client-side close). |
| 44 | + async def listen() -> None: |
| 45 | + request = types.SubscriptionsListenRequest( |
| 46 | + params=types.SubscriptionsListenRequestParams( |
| 47 | + notifications=types.SubscriptionFilter( |
| 48 | + tools_list_changed=True, resource_subscriptions=["note://todo"] |
| 49 | + ) |
| 50 | + ) |
| 51 | + ) |
| 52 | + await client.session.send_request(request, types.SubscriptionsListenResult) |
| 53 | + |
| 54 | + tg.start_soon(listen) |
| 55 | + |
| 56 | + # ── the ack is the first frame: it echoes the honored filter, tagged ── |
| 57 | + await wait_for(1) |
| 58 | + ack = received[0] |
| 59 | + assert isinstance(ack, types.SubscriptionsAcknowledgedNotification), ack |
| 60 | + assert ack.params.notifications.tools_list_changed is True |
| 61 | + assert ack.params.notifications.resource_subscriptions == ["note://todo"] |
| 62 | + assert ack.params.meta is not None and SUBSCRIPTION_ID in ack.params.meta |
| 63 | + |
| 64 | + # ── exact-URI filtering: an unsubscribed note edit stays silent ── |
| 65 | + await client.call_tool("edit_note", {"name": "journal", "text": "day two"}) |
| 66 | + # ── the subscribed URI delivers, carrying the same subscription id ── |
| 67 | + await client.call_tool("edit_note", {"name": "todo", "text": "water plants"}) |
| 68 | + await wait_for(2) |
| 69 | + updated = received[1] |
| 70 | + assert isinstance(updated, types.ResourceUpdatedNotification), updated |
| 71 | + assert updated.params.uri == "note://todo" |
| 72 | + assert updated.params.meta == ack.params.meta |
| 73 | + assert len(received) == 2, "the journal edit must not have been delivered" |
| 74 | + |
| 75 | + # ── a runtime tool registration announces itself ── |
| 76 | + await client.call_tool("enable_search", {}) |
| 77 | + await wait_for(3) |
| 78 | + assert isinstance(received[2], types.ToolListChangedNotification), received[2] |
| 79 | + |
| 80 | + # The client is done listening: closing the stream is cancelling the |
| 81 | + # parked request's scope. |
| 82 | + tg.cancel_scope.cancel() |
| 83 | + |
| 84 | + # list_changed told us to re-fetch - the new tool is callable, and the |
| 85 | + # session outlives the closed stream. |
| 86 | + tools = await client.list_tools() |
| 87 | + assert "search" in {tool.name for tool in tools.tools} |
| 88 | + result = await client.call_tool("search", {"query": "water"}) |
| 89 | + content = result.content[0] |
| 90 | + assert isinstance(content, types.TextContent) and content.text == "todo", result |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + run_client(main) |
0 commit comments