MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / adopt

Method adopt

src/mcp/client/session.py:376–404  ·  view source on GitHub ↗

Install negotiated state from a result the caller already holds (no wire traffic). Clears the opposite slot, so at most one of `initialize_result` / `discover_result` is ever non-None. Raises: RuntimeError: `result` is a `DiscoverResult` whose `supported_version

(self, result: types.InitializeResult | types.DiscoverResult)

Source from the content-addressed store, hash-verified

374 return result
375
376 def adopt(self, result: types.InitializeResult | types.DiscoverResult) -> None:
377 """Install negotiated state from a result the caller already holds (no wire traffic).
378
379 Clears the opposite slot, so at most one of `initialize_result` /
380 `discover_result` is ever non-None.
381
382 Raises:
383 RuntimeError: `result` is a `DiscoverResult` whose `supported_versions`
384 shares nothing with this client's `MODERN_PROTOCOL_VERSIONS`.
385 """
386 if isinstance(result, types.DiscoverResult):
387 # ordered oldest→newest via MODERN_PROTOCOL_VERSIONS
388 mutual = [v for v in MODERN_PROTOCOL_VERSIONS if v in result.supported_versions]
389 if not mutual:
390 raise RuntimeError(
391 f"No mutually supported modern protocol version "
392 f"(server: {result.supported_versions}, client: {list(MODERN_PROTOCOL_VERSIONS)})"
393 )
394 client_info = self._client_info.model_dump(by_alias=True, mode="json", exclude_none=True)
395 capabilities = self._build_capabilities().model_dump(by_alias=True, mode="json", exclude_none=True)
396 self._stamp = _make_modern_stamp(mutual[-1], client_info, capabilities)
397 self._discover_result = result
398 self._initialize_result = None
399 self._negotiated_version = mutual[-1]
400 else:
401 self._stamp = _make_handshake_stamp(result.protocol_version)
402 self._initialize_result = result
403 self._discover_result = None
404 self._negotiated_version = result.protocol_version
405
406 async def send_discover(self, version: str) -> dict[str, Any]:
407 """Send a single ``server/discover`` at ``version`` and return the raw result dict.

Callers 5

initializeMethod · 0.95
discoverMethod · 0.95
__aenter__Method · 0.45
negotiate_autoFunction · 0.45

Calls 3

_build_capabilitiesMethod · 0.95
_make_modern_stampFunction · 0.85
_make_handshake_stampFunction · 0.85