From 6e3442bd127659e887cc3b056ec988974d06bbf4 Mon Sep 17 00:00:00 2001 From: Vivecccccc Date: Thu, 25 Sep 2025 16:45:24 +0800 Subject: [PATCH] set conditional capabilities for SamplingCapability and RootsCapability upon MCP client session initialization to prevent servers from attempting callbacks when the client does not actually support them --- api/core/mcp/session/client_session.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/api/core/mcp/session/client_session.py b/api/core/mcp/session/client_session.py index 5817416ba4..85c677d3cb 100644 --- a/api/core/mcp/session/client_session.py +++ b/api/core/mcp/session/client_session.py @@ -109,12 +109,20 @@ class ClientSession( self._message_handler = message_handler or _default_message_handler def initialize(self) -> types.InitializeResult: - sampling = types.SamplingCapability() - roots = types.RootsCapability( - # TODO: Should this be based on whether we - # _will_ send notifications, or only whether - # they're supported? - listChanged=True, + # Only set capabilities if non-default callbacks are provided + # This prevents servers from attempting callbacks when we don't actually support them + sampling = ( + types.SamplingCapability() + if self._sampling_callback is not _default_sampling_callback + else None + ) + roots = ( + types.RootsCapability( + # Only enable listChanged if we have a custom callback + listChanged=True, + ) + if self._list_roots_callback is not _default_list_roots_callback + else None ) result = self.send_request(