MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / _plan_outbound

Function _plan_outbound

src/mcp/shared/jsonrpc_dispatcher.py:201–226  ·  view source on GitHub ↗

Choose the outbound `SessionMessage.metadata` and the abandon-cancellation policy. `related_request_id` wins over resumption hints (they are dropped). Only hints that actually reach the transport suppress the courtesy cancel - a request that is neither resumable nor cancelled would leak

(related_request_id: RequestId | None, opts: CallOptions | None)

Source from the content-addressed store, hash-verified

199
200
201def _plan_outbound(related_request_id: RequestId | None, opts: CallOptions | None) -> _OutboundPlan:
202 """Choose the outbound `SessionMessage.metadata` and the abandon-cancellation policy.
203
204 `related_request_id` wins over resumption hints (they are dropped). Only
205 hints that actually reach the transport suppress the courtesy cancel - a
206 request that is neither resumable nor cancelled would leak the peer's work.
207 """
208 opts = opts or {}
209 cancel_on_abandon = opts.get("cancel_on_abandon", True)
210 token = opts.get("resumption_token")
211 on_token = opts.get("on_resumption_token")
212 headers = opts.get("headers")
213 if related_request_id is not None:
214 if token is not None or on_token is not None:
215 logger.debug(
216 "dropping resumption hints: related_request_id %r takes precedence on metadata", related_request_id
217 )
218 return _OutboundPlan(ServerMessageMetadata(related_request_id=related_request_id), cancel_on_abandon)
219 if token is not None or on_token is not None:
220 return _OutboundPlan(
221 ClientMessageMetadata(resumption_token=token, on_resumption_token_update=on_token, headers=headers),
222 cancel_on_abandon=False,
223 )
224 if headers:
225 return _OutboundPlan(ClientMessageMetadata(headers=headers), cancel_on_abandon)
226 return _OutboundPlan(None, cancel_on_abandon)
227
228
229class JSONRPCDispatcher(Dispatcher[TransportT]):

Calls 4

_OutboundPlanClass · 0.85
debugMethod · 0.80