SIGN IN SIGN UP

The official Python SDK for Model Context Protocol servers and clients

0 0 0 Python

Extract JSON-RPC wrapping into a Dispatcher component

BaseSession previously did two jobs: MCP protocol semantics (progress
tokens, cancellation, in-flight tracking, result validation) and JSON-RPC
wire encoding (wrap/unwrap envelopes, ID correlation, the receive loop).
This split those into a composition: BaseSession owns the MCP semantics,
a Dispatcher owns the wire protocol.

The default JSONRPCDispatcher is the old _receive_loop + send_request +
send_notification + _send_response logic extracted verbatim. BaseSession
constructs one from the supplied streams, so every existing transport
(stdio, SHTTP, WebSocket, in-memory) works unchanged.

The Dispatcher Protocol deals in {"method": str, "params": dict} — the
same dict request.model_dump() already produces. A custom dispatcher
(gRPC stub, message broker, anything) implements five methods and passes
itself as ClientSession(dispatcher=...). All of initialize(),
list_tools(), call_tool() work unchanged on top; no parallel Client
hierarchy, no fat Protocol.

This addresses the composition-not-inheritance point raised in review of
the previous pluggable-transport PRs. Closes the SessionMessage/JSON-RPC
coupling that was the remaining blocker.

Github-Issue: #1690
M
Max Isbey committed
5ddf1dd40481b09e7c63fd9fe7f9c9f309e52e5f
Parent: 883d893