Support custom client info throughout client APIs (#474)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Jerome
2025-04-10 14:52:01 +01:00
committed by GitHub
parent da54ea003e
commit c4beb3e8ef
4 changed files with 142 additions and 3 deletions

View File

@@ -38,9 +38,13 @@ async def message_handler(
async def run_session(
read_stream: MemoryObjectReceiveStream[JSONRPCMessage | Exception],
write_stream: MemoryObjectSendStream[JSONRPCMessage],
client_info: types.Implementation | None = None,
):
async with ClientSession(
read_stream, write_stream, message_handler=message_handler
read_stream,
write_stream,
message_handler=message_handler,
client_info=client_info,
) as session:
logger.info("Initializing session")
await session.initialize()

View File

@@ -10,6 +10,8 @@ from mcp.shared.context import RequestContext
from mcp.shared.session import BaseSession, RequestResponder
from mcp.shared.version import SUPPORTED_PROTOCOL_VERSIONS
DEFAULT_CLIENT_INFO = types.Implementation(name="mcp", version="0.1.0")
class SamplingFnT(Protocol):
async def __call__(
@@ -97,6 +99,7 @@ class ClientSession(
list_roots_callback: ListRootsFnT | None = None,
logging_callback: LoggingFnT | None = None,
message_handler: MessageHandlerFnT | None = None,
client_info: types.Implementation | None = None,
) -> None:
super().__init__(
read_stream,
@@ -105,6 +108,7 @@ class ClientSession(
types.ServerNotification,
read_timeout_seconds=read_timeout_seconds,
)
self._client_info = client_info or DEFAULT_CLIENT_INFO
self._sampling_callback = sampling_callback or _default_sampling_callback
self._list_roots_callback = list_roots_callback or _default_list_roots_callback
self._logging_callback = logging_callback or _default_logging_callback
@@ -130,7 +134,7 @@ class ClientSession(
experimental=None,
roots=roots,
),
clientInfo=types.Implementation(name="mcp", version="0.1.0"),
clientInfo=self._client_info,
),
)
),

View File

@@ -10,6 +10,7 @@ from typing import Any
import anyio
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
import mcp.types as types
from mcp.client.session import (
ClientSession,
ListRootsFnT,
@@ -65,6 +66,7 @@ async def create_connected_server_and_client_session(
list_roots_callback: ListRootsFnT | None = None,
logging_callback: LoggingFnT | None = None,
message_handler: MessageHandlerFnT | None = None,
client_info: types.Implementation | None = None,
raise_exceptions: bool = False,
) -> AsyncGenerator[ClientSession, None]:
"""Creates a ClientSession that is connected to a running MCP server."""
@@ -95,6 +97,7 @@ async def create_connected_server_and_client_session(
list_roots_callback=list_roots_callback,
logging_callback=logging_callback,
message_handler=message_handler,
client_info=client_info,
) as client_session:
await client_session.initialize()
yield client_session