Skip to content

Commit 53257ca

Browse files
committed
test: JSON-RPC-specific dispatcher tests + coverage to 100%
Covers behaviors with no DirectDispatcher analog: out-of-order response correlation, INTERNAL_ERROR over the wire, peer-cancel in interrupt and signal modes, CONNECTION_CLOSED on stream EOF mid-await, late-response drop, raise_handler_exceptions propagation, ServerMessageMetadata tagging on ctx.send_request, null-id JSONRPCError drop, ValidationError->INVALID_PARAMS, contextvar propagation via _spawn, and the defensive Broken/Closed/WouldBlock catches. Two small src tweaks for coverage: - _cancel_outbound: combine the two except arms into one tuple - _dispatch: pragma no-branch on the final case (match is exhaustive over JSONRPCMessage; the no-match arc is unreachable) 43 tests, 100% coverage on all PR2 modules, 0.15s wall-clock.
1 parent 200a607 commit 53257ca

2 files changed

Lines changed: 535 additions & 4 deletions

File tree

src/mcp/shared/jsonrpc_dispatcher.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,10 @@ def _dispatch(
359359
self._dispatch_notification(msg, metadata, on_notify, sender_ctx)
360360
case JSONRPCResponse():
361361
self._resolve_pending(msg.id, msg.result)
362-
case JSONRPCError():
362+
case JSONRPCError(): # pragma: no branch
363363
# `id` may be None per JSON-RPC (parse error before id known).
364+
# The match is exhaustive over JSONRPCMessage; the no-match arc
365+
# on this final case is unreachable.
364366
self._resolve_pending(msg.id, msg.error)
365367

366368
def _dispatch_request(
@@ -537,7 +539,5 @@ async def _write_error(self, request_id: RequestId, error: ErrorData) -> None:
537539
async def _cancel_outbound(self, request_id: RequestId, reason: str) -> None:
538540
try:
539541
await self.notify("notifications/cancelled", {"requestId": request_id, "reason": reason})
540-
except anyio.BrokenResourceError:
541-
pass
542-
except anyio.ClosedResourceError:
542+
except (anyio.BrokenResourceError, anyio.ClosedResourceError):
543543
pass

0 commit comments

Comments
 (0)