diff --git a/src/mcp/client/session.py b/src/mcp/client/session.py index 86113874b..c6a8e8def 100644 --- a/src/mcp/client/session.py +++ b/src/mcp/client/session.py @@ -146,6 +146,20 @@ def _receive_notification_adapter(self) -> TypeAdapter[types.ServerNotification] return types.server_notification_adapter async def initialize(self) -> types.InitializeResult: + """Perform the MCP initialization handshake with the server. + + Sends an InitializeRequest with the client's capabilities (sampling, + elicitation, roots, tasks) and client_info, waits for the server's + InitializeResult, validates the negotiated protocol version, stores + the result, and sends an InitializedNotification. + + Returns: + The server's InitializeResult containing server_info, + capabilities, instructions, and the negotiated protocol_version. + + Raises: + RuntimeError: If the server returns an unsupported protocol version. + """ sampling = ( (self._sampling_capabilities or types.SamplingCapability()) if self._sampling_callback is not _default_sampling_callback diff --git a/src/mcp/client/sse.py b/src/mcp/client/sse.py index 74e5ba806..5bb4d86a1 100644 --- a/src/mcp/client/sse.py +++ b/src/mcp/client/sse.py @@ -18,6 +18,11 @@ def remove_request_params(url: str) -> str: + """Strip query parameters from a URL, returning only scheme + host + path. + + Used to derive the base endpoint URL from an SSE connection URL that may + include session identifiers or other transient query parameters. + """ return urljoin(url, urlparse(url).path) diff --git a/src/mcp/server/session.py b/src/mcp/server/session.py index fc2f97a9c..ea78e93b1 100644 --- a/src/mcp/server/session.py +++ b/src/mcp/server/session.py @@ -107,6 +107,11 @@ def _receive_notification_adapter(self) -> TypeAdapter[types.ClientNotification] @property def client_params(self) -> types.InitializeRequestParams | None: + """The client's InitializeRequestParams received during handshake. + + Contains client_info, capabilities, and the requested protocol_version. + Returns None if the session has not yet been initialized. + """ return self._client_params @property @@ -689,4 +694,9 @@ async def _handle_incoming(self, req: ServerRequestResponder) -> None: @property def incoming_messages(self) -> MemoryObjectReceiveStream[ServerRequestResponder]: + """Stream of incoming client requests wrapped as ServerRequestResponder objects. + + Each item in the stream pairs the original client request with a + responder that the server handler uses to send back a result or error. + """ return self._incoming_message_stream_reader