eddfa29d9d
`ServerSession.send_progress_notification` takes an explicit progress token
decoupled from the request it belongs to, so it can keep emitting progress
for a request that has already completed -- which the spec forbids
("Progress notifications MUST stop after completion"). The request-scoped
`report_progress` (and `Context.report_progress`) is the supported path: it
reports against the inbound request's own token, no-ops when the caller did
not ask for progress, and stops when the request completes. The deprecated
method keeps working and emits `MCPDeprecationWarning`.
The warning message deliberately departs from the "<X> is deprecated as of
<version>" pattern used by the spec-driven deprecations: this one is an SDK
API decision, not a spec retirement (2026-07-28 does not retire
server-to-client progress).
For "stops when the request completes" to hold on every dispatcher,
`_DirectDispatchContext` now closes with its request the way
`_JSONRPCDispatchContext` already did: `close()` runs in the dispatch
handler's `finally`, after which `progress`/`notify` deliver nothing,
`can_send_request` is False, and `send_raw_request` raises
`NoBackChannelError` -- the closed state the `DispatchContext` protocol
documents. Two pre-existing tests that asserted `can_send_request` on a
context captured after its handler returned now sample it in-handler, and
the closed-state contract tests are parametrized over both dispatchers.
The interaction test that covered both the server and client side of late
progress is split in two. The server-side property is proved positively on
the wire through `report_progress`; it no longer relies on a session-bound
standalone stream, so it also runs on the stateless streamable-http arm.
The client-side late-drop test keeps using the deprecated explicit-token
method -- the only API that can still produce a late notification -- under
`pytest.warns`, and its arms are unchanged. The migration guide stops
recommending the deprecated method anywhere and documents the replacement.
101 lines
3.8 KiB
Python
101 lines
3.8 KiB
Python
"""Tests for the server-side `Context`.
|
|
|
|
`Context` extends `BaseContext` (forwarding to a `DispatchContext`) with
|
|
`lifespan`, `connection`, and request-scoped `log`. End-to-end tested over
|
|
`DirectDispatcher`.
|
|
"""
|
|
|
|
from collections.abc import Mapping
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
import anyio
|
|
import pytest
|
|
|
|
from mcp.server.connection import Connection
|
|
from mcp.server.context import Context
|
|
from mcp.shared.dispatcher import DispatchContext
|
|
from mcp.shared.transport_context import TransportContext
|
|
|
|
from ..shared.conftest import direct_pair
|
|
from ..shared.test_dispatcher import Recorder, echo_handlers, running_pair
|
|
|
|
DCtx = DispatchContext[TransportContext]
|
|
|
|
|
|
@dataclass
|
|
class _Lifespan:
|
|
name: str
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_context_exposes_lifespan_and_connection_and_forwards_base_context():
|
|
captured: list[Context[_Lifespan]] = []
|
|
conn_holder: list[Connection] = []
|
|
open_while_handling: list[bool] = []
|
|
|
|
async def server_on_request(dctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]:
|
|
ctx: Context[_Lifespan] = Context(dctx, lifespan=_Lifespan("app"), connection=conn_holder[0])
|
|
captured.append(ctx)
|
|
# `can_send_request` is sampled in-handler: the dispatch context closes when the
|
|
# request returns, after which it is False on every dispatcher.
|
|
open_while_handling.append(ctx.can_send_request)
|
|
return {}
|
|
|
|
async with running_pair(direct_pair, server_on_request=server_on_request) as (client, server, *_):
|
|
conn_holder.append(Connection.for_loop(server, session_id="sess-1"))
|
|
with anyio.fail_after(5):
|
|
await client.send_raw_request("t", None)
|
|
ctx = captured[0]
|
|
assert ctx.lifespan.name == "app"
|
|
assert ctx.connection is conn_holder[0]
|
|
assert ctx.transport.kind == "direct"
|
|
assert open_while_handling == [True]
|
|
assert ctx.session_id == "sess-1"
|
|
assert ctx.headers is None
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_context_log_sends_request_scoped_message_notification():
|
|
crec = Recorder()
|
|
_, c_notify = echo_handlers(crec)
|
|
|
|
async def server_on_request(dctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]:
|
|
ctx: Context[_Lifespan] = Context(dctx, lifespan=_Lifespan("app"), connection=Connection.for_loop(dctx))
|
|
await ctx.log("debug", "hello") # pyright: ignore[reportDeprecated]
|
|
return {}
|
|
|
|
async with running_pair(direct_pair, server_on_request=server_on_request, client_on_notify=c_notify) as (
|
|
client,
|
|
*_,
|
|
):
|
|
with anyio.fail_after(5):
|
|
await client.send_raw_request("t", None)
|
|
await crec.notified.wait()
|
|
method, params = crec.notifications[0]
|
|
assert method == "notifications/message"
|
|
assert params is not None and params["level"] == "debug" and params["data"] == "hello"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_context_log_includes_logger_and_meta_when_supplied():
|
|
crec = Recorder()
|
|
_, c_notify = echo_handlers(crec)
|
|
|
|
async def server_on_request(dctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]:
|
|
ctx: Context[_Lifespan] = Context(dctx, lifespan=_Lifespan("app"), connection=Connection.for_loop(dctx))
|
|
await ctx.log("info", "x", logger="my.log", meta={"traceId": "t"}) # pyright: ignore[reportDeprecated]
|
|
return {}
|
|
|
|
async with running_pair(direct_pair, server_on_request=server_on_request, client_on_notify=c_notify) as (
|
|
client,
|
|
*_,
|
|
):
|
|
with anyio.fail_after(5):
|
|
await client.send_raw_request("t", None)
|
|
await crec.notified.wait()
|
|
_, params = crec.notifications[0]
|
|
assert params is not None
|
|
assert params["logger"] == "my.log"
|
|
assert params["_meta"] == {"traceId": "t"}
|