feat(client): store InitializeResult as initialize_result (#2300)
This commit is contained in:
parent
67201a9bbd
commit
20dd94632e
2 changed files with 64 additions and 37 deletions
|
|
@ -19,6 +19,7 @@ from mcp.types import (
|
|||
EmptyResult,
|
||||
GetPromptResult,
|
||||
Implementation,
|
||||
InitializeResult,
|
||||
ListPromptsResult,
|
||||
ListResourcesResult,
|
||||
ListResourceTemplatesResult,
|
||||
|
|
@ -29,7 +30,6 @@ from mcp.types import (
|
|||
ReadResourceResult,
|
||||
RequestParamsMeta,
|
||||
ResourceTemplateReference,
|
||||
ServerCapabilities,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -155,9 +155,16 @@ class Client:
|
|||
return self._session
|
||||
|
||||
@property
|
||||
def server_capabilities(self) -> ServerCapabilities | None:
|
||||
"""The server capabilities received during initialization, or None if not yet initialized."""
|
||||
return self.session.get_server_capabilities()
|
||||
def initialize_result(self) -> InitializeResult:
|
||||
"""The server's InitializeResult.
|
||||
|
||||
Contains server_info, capabilities, instructions, and the negotiated protocol_version.
|
||||
Raises RuntimeError if accessed outside the context manager.
|
||||
"""
|
||||
result = self.session.initialize_result
|
||||
if result is None: # pragma: no cover
|
||||
raise RuntimeError("Client must be used within an async context manager")
|
||||
return result
|
||||
|
||||
async def send_ping(self, *, meta: RequestParamsMeta | None = None) -> EmptyResult:
|
||||
"""Send a ping request to the server."""
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ class ClientSession(
|
|||
self._logging_callback = logging_callback or _default_logging_callback
|
||||
self._message_handler = message_handler or _default_message_handler
|
||||
self._tool_output_schemas: dict[str, dict[str, Any] | None] = {}
|
||||
self._server_capabilities: types.ServerCapabilities | None = None
|
||||
self._initialize_result: types.InitializeResult | None = None
|
||||
self._experimental_features: ExperimentalClientFeatures | None = None
|
||||
|
||||
# Experimental: Task handlers (use defaults if not provided)
|
||||
|
|
@ -185,18 +185,19 @@ class ClientSession(
|
|||
if result.protocol_version not in SUPPORTED_PROTOCOL_VERSIONS:
|
||||
raise RuntimeError(f"Unsupported protocol version from the server: {result.protocol_version}")
|
||||
|
||||
self._server_capabilities = result.capabilities
|
||||
self._initialize_result = result
|
||||
|
||||
await self.send_notification(types.InitializedNotification())
|
||||
|
||||
return result
|
||||
|
||||
def get_server_capabilities(self) -> types.ServerCapabilities | None:
|
||||
"""Return the server capabilities received during initialization.
|
||||
@property
|
||||
def initialize_result(self) -> types.InitializeResult | None:
|
||||
"""The server's InitializeResult. None until initialize() has been called.
|
||||
|
||||
Returns None if the session has not been initialized yet.
|
||||
Contains server_info, capabilities, instructions, and the negotiated protocol_version.
|
||||
"""
|
||||
return self._server_capabilities
|
||||
return self._initialize_result
|
||||
|
||||
@property
|
||||
def experimental(self) -> ExperimentalClientFeatures:
|
||||
|
|
|
|||
Loading…
Reference in a new issue