|
| 1 | +"""Tests for `BaseContext`. |
| 2 | +
|
| 3 | +`BaseContext` is composition over a `DispatchContext` — it forwards |
| 4 | +``transport``/``cancel_requested``/``send_request``/``notify``/``progress`` |
| 5 | +and adds ``meta``. It must satisfy `Outbound` so `PeerMixin` works on it. |
| 6 | +""" |
| 7 | + |
| 8 | +from collections.abc import Mapping |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +import anyio |
| 12 | +import pytest |
| 13 | + |
| 14 | +from mcp.shared.context import BaseContext |
| 15 | +from mcp.shared.dispatcher import DispatchContext |
| 16 | +from mcp.shared.peer import Peer |
| 17 | +from mcp.shared.transport_context import TransportContext |
| 18 | + |
| 19 | +from .conftest import direct_pair |
| 20 | +from .test_dispatcher import Recorder, echo_handlers, running_pair |
| 21 | + |
| 22 | +DCtx = DispatchContext[TransportContext] |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.anyio |
| 26 | +async def test_base_context_forwards_transport_and_cancel_requested(): |
| 27 | + captured: list[BaseContext[TransportContext]] = [] |
| 28 | + |
| 29 | + async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]: |
| 30 | + bctx = BaseContext(ctx) |
| 31 | + captured.append(bctx) |
| 32 | + return {} |
| 33 | + |
| 34 | + async with running_pair(direct_pair, server_on_request=server_on_request) as (client, *_): |
| 35 | + with anyio.fail_after(5): |
| 36 | + await client.send_request("t", None) |
| 37 | + bctx = captured[0] |
| 38 | + assert bctx.transport.kind == "direct" |
| 39 | + assert isinstance(bctx.cancel_requested, anyio.Event) |
| 40 | + assert bctx.can_send_request is True |
| 41 | + assert bctx.meta is None |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.anyio |
| 45 | +async def test_base_context_send_request_and_notify_forward_to_dispatch_context(): |
| 46 | + crec = Recorder() |
| 47 | + c_req, c_notify = echo_handlers(crec) |
| 48 | + |
| 49 | + async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]: |
| 50 | + bctx = BaseContext(ctx) |
| 51 | + sample = await bctx.send_request("sampling/createMessage", {"x": 1}) |
| 52 | + await bctx.notify("notifications/message", {"level": "info"}) |
| 53 | + return {"sample": sample} |
| 54 | + |
| 55 | + async with running_pair( |
| 56 | + direct_pair, |
| 57 | + server_on_request=server_on_request, |
| 58 | + client_on_request=c_req, |
| 59 | + client_on_notify=c_notify, |
| 60 | + ) as (client, *_): |
| 61 | + with anyio.fail_after(5): |
| 62 | + result = await client.send_request("tools/call", None) |
| 63 | + await crec.notified.wait() |
| 64 | + assert crec.requests == [("sampling/createMessage", {"x": 1})] |
| 65 | + assert crec.notifications == [("notifications/message", {"level": "info"})] |
| 66 | + assert result["sample"] == {"echoed": "sampling/createMessage", "params": {"x": 1}} |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.anyio |
| 70 | +async def test_base_context_report_progress_invokes_caller_on_progress(): |
| 71 | + received: list[tuple[float, float | None, str | None]] = [] |
| 72 | + |
| 73 | + async def on_progress(progress: float, total: float | None, message: str | None) -> None: |
| 74 | + received.append((progress, total, message)) |
| 75 | + |
| 76 | + async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]: |
| 77 | + bctx = BaseContext(ctx) |
| 78 | + await bctx.report_progress(0.5, total=1.0, message="halfway") |
| 79 | + return {} |
| 80 | + |
| 81 | + async with running_pair(direct_pair, server_on_request=server_on_request) as (client, *_): |
| 82 | + with anyio.fail_after(5): |
| 83 | + await client.send_request("t", None, {"on_progress": on_progress}) |
| 84 | + assert received == [(0.5, 1.0, "halfway")] |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.anyio |
| 88 | +async def test_base_context_satisfies_outbound_so_peer_mixin_works(): |
| 89 | + """Wrapping a BaseContext in Peer proves it satisfies Outbound structurally.""" |
| 90 | + |
| 91 | + async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]: |
| 92 | + bctx = BaseContext(ctx) |
| 93 | + await Peer(bctx).ping() |
| 94 | + return {} |
| 95 | + |
| 96 | + crec = Recorder() |
| 97 | + c_req, c_notify = echo_handlers(crec) |
| 98 | + async with running_pair( |
| 99 | + direct_pair, server_on_request=server_on_request, client_on_request=c_req, client_on_notify=c_notify |
| 100 | + ) as (client, *_): |
| 101 | + with anyio.fail_after(5): |
| 102 | + await client.send_request("t", None) |
| 103 | + assert crec.requests == [("ping", None)] |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.anyio |
| 107 | +async def test_base_context_meta_holds_supplied_request_params_meta(): |
| 108 | + async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]: |
| 109 | + bctx = BaseContext(ctx, meta={"progressToken": "abc"}) |
| 110 | + assert bctx.meta is not None and bctx.meta.get("progressToken") == "abc" |
| 111 | + return {} |
| 112 | + |
| 113 | + async with running_pair(direct_pair, server_on_request=server_on_request) as (client, *_): |
| 114 | + with anyio.fail_after(5): |
| 115 | + await client.send_request("t", None) |
0 commit comments