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

Method discover

src/mcp/client/session.py:438–474  ·  view source on GitHub ↗

Probe `server/discover` and adopt the result. Sends a single `server/discover` proposing the newest modern protocol version. On `UNSUPPORTED_PROTOCOL_VERSION` (-32022) the server's `supported` list is intersected with `MODERN_PROTOCOL_VERSIONS` and the probe is retri

(self)

Source from the content-addressed store, hash-verified

436 return await self._dispatcher.send_raw_request(data["method"], data.get("params"), opts)
437
438 async def discover(self) -> types.DiscoverResult:
439 """Probe `server/discover` and adopt the result.
440
441 Sends a single `server/discover` proposing the newest modern protocol
442 version. On `UNSUPPORTED_PROTOCOL_VERSION` (-32022) the server's
443 `supported` list is intersected with `MODERN_PROTOCOL_VERSIONS` and the
444 probe is retried once at the highest mutual version. Any other error —
445 including `METHOD_NOT_FOUND` (-32601) and `REQUEST_TIMEOUT` (-32001) —
446 propagates; the legacy `initialize()` fallback is the caller's policy.
447
448 Raises:
449 MCPError: The server rejected `server/discover`, the probe timed
450 out, or the -32022 retry found no mutual version / failed again.
451 RuntimeError: `adopt()` found no mutual version in the returned
452 `supported_versions`.
453 """
454 if self._discover_result is not None:
455 return self._discover_result
456
457 try:
458 raw = await self.send_discover(LATEST_MODERN_VERSION)
459 except MCPError as e:
460 if e.code != UNSUPPORTED_PROTOCOL_VERSION:
461 raise
462 try:
463 data = types.UnsupportedProtocolVersionErrorData.model_validate(e.error.data)
464 except ValidationError:
465 raise e from None
466 # ordered oldest→newest via MODERN_PROTOCOL_VERSIONS
467 mutual = [v for v in MODERN_PROTOCOL_VERSIONS if v in data.supported]
468 if not mutual:
469 raise
470 raw = await self.send_discover(mutual[-1])
471
472 result = types.DiscoverResult.model_validate(raw)
473 self.adopt(result)
474 return result
475
476 @property
477 def initialize_result(self) -> types.InitializeResult | None:

Calls 2

send_discoverMethod · 0.95
adoptMethod · 0.95