Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/mcp/client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/mcp/client/sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
10 changes: 10 additions & 0 deletions src/mcp/server/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading