context
BaseContext - the user-facing per-request context.
Composition over a DispatchContext: forwards the transport metadata, the
back-channel (send_raw_request/notify), progress reporting, and the cancel
event. Adds meta (the inbound request's _meta field).
Satisfies Outbound, so ClientPeer can wrap it. Shared between client and
server: the server's Context extends this with lifespan/connection;
ClientContext is just an alias.
BaseContext
Bases: Generic[TransportT]
Per-request context wrapping a DispatchContext.
ServerRunner constructs one per inbound request and passes it to the
user's handler.
Source code in src/mcp/shared/context.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
transport
property
transport: TransportT
Transport-specific metadata for this inbound request.
cancel_requested
property
cancel_requested: Event
Set when the peer sends notifications/cancelled for this request.
can_send_request
property
can_send_request: bool
Whether the back-channel can currently deliver server-initiated requests.
False when the transport has no back-channel, or when the underlying
dispatch context has been closed because the inbound request finished.
send_raw_request
async
send_raw_request(
method: str,
params: Mapping[str, Any] | None,
opts: CallOptions | None = None,
) -> dict[str, Any]
Send a request to the peer on the back-channel.
Raises:
| Type | Description |
|---|---|
MCPError
|
The peer responded with an error. |
NoBackChannelError
|
|
Source code in src/mcp/shared/context.py
62 63 64 65 66 67 68 69 70 71 72 73 74 | |
notify
async
Send a notification to the peer on the back-channel.
Source code in src/mcp/shared/context.py
76 77 78 | |
report_progress
async
Report progress for this request, if the peer supplied a progress token.
A no-op when no token was supplied.
Source code in src/mcp/shared/context.py
80 81 82 83 84 85 | |