π The fast, Pythonic way to build MCP servers and clients.
Forward list-changed notifications through ProxyClient (#6426)
nate nowackf33a0991000c
diff --git a/fastmcp_slim/fastmcp/server/providers/proxy.py b/fastmcp_slim/fastmcp/server/providers/proxy.pyindex 7f9c7c9f7..5b934e7f8 100644--- a/fastmcp_slim/fastmcp/server/providers/proxy.py+++ b/fastmcp_slim/fastmcp/server/providers/proxy.py@@ -21,6 +21,7 @@ import anyioimport httpx2import mcp_typesfrom mcp import ClientSession+from mcp.client.client import _evicting_message_handlerfrom mcp.server.connection import Connectionfrom mcp.server.context import ServerRequestContextfrom mcp.shared.exceptions import MCPError@@ -1775,6 +1776,26 @@ async def default_proxy_progress_handler(await ctx.report_progress(progress, total, message)+_FORWARDED_LIST_CHANGED_NOTIFICATIONS = (+ mcp_types.ToolListChangedNotification,+ mcp_types.ResourceListChangedNotification,+ mcp_types.PromptListChangedNotification,+)+++async def default_proxy_message_handler(+ message: mcp_types.ServerNotification | Exception,+) -> None:+ """Forward list-changed notifications from remote server to proxy's connected clients.++ Logging and progress have dedicated handlers; the tool, resource, and prompt+ list-changed notifications only arrive through the message handler.+ """+ if isinstance(message, _FORWARDED_LIST_CHANGED_NOTIFICATIONS):+ ctx = get_context()+ await ctx.send_notification(message)++def _restore_request_context(rc_ref: list[Any],) -> None:@@ -1839,7 +1860,8 @@ def _make_restoring_handler(handler: Callable, rc_ref: list[Any]) -> Callable:class ProxyClient(Client[ClientTransportT]):"""A proxy client that forwards advanced interactions between a remote MCP server and the proxy's connected clients.- Supports forwarding roots, sampling, elicitation, logging, and progress.+ Supports forwarding roots, sampling, elicitation, logging, progress, and+ tool, resource, and prompt list-changed notifications.Eligible inbound HTTP headers are forwarded by default; set`forward_incoming_headers=False` to use only the backend transport'sconfigured headers and authentication. Applies to HTTP and SSE backends,@@ -1914,6 +1936,7 @@ class ProxyClient(Client[ClientTransportT]):("elicitation_handler", default_proxy_elicitation_handler),("log_handler", default_proxy_log_handler),("progress_handler", default_proxy_progress_handler),+ ("message_handler", default_proxy_message_handler),):if key not in kwargs:kwargs[key] = _make_restoring_handler(default_fn, self._proxy_rc_ref)@@ -1950,6 +1973,16 @@ class ProxyClient(Client[ClientTransportT]):self._progress_handler = _make_restoring_handler(default_proxy_progress_handler, self._proxy_rc_ref)+ if "message_handler" in self._proxy_restoring_handler_keys:+ message_handler = _make_restoring_handler(+ default_proxy_message_handler, self._proxy_rc_ref+ )+ self._base_message_handler = message_handler+ self._session_kwargs["message_handler"] = (+ _evicting_message_handler(self._response_cache, message_handler)+ if self._response_cache is not None+ else message_handler+ )def new(self) -> ProxyClient[ClientTransportT]:new_client = cast(ProxyClient[ClientTransportT], super().new())diff --git a/tests/server/providers/proxy/test_proxy_client.py b/tests/server/providers/proxy/test_proxy_client.pyindex 3a39a3590..f34a93bb8 100644--- a/tests/server/providers/proxy/test_proxy_client.py+++ b/tests/server/providers/proxy/test_proxy_client.py@@ -187,6 +187,10 @@ def fastmcp_server():)return 100+ @mcp.tool+ async def hide_echo(context: Context) -> None:+ await context.disable_components(names={"echo"})+return mcp@@ -506,6 +510,24 @@ class TestProxyClient:assert PROGRESS_MESSAGES == EXPECTED_PROGRESS_MESSAGES+ async def test_forward_list_changed_notifications(self, proxy_server: FastMCP):+ received: list[str] = []++ async def message_handler(message: Any) -> None:+ if not isinstance(message, Exception):+ received.append(message.method)++ async with Client(+ proxy_server, mode="legacy", message_handler=message_handler+ ) as client:+ await client.call_tool("hide_echo", {})++ assert received == [+ "notifications/tools/list_changed",+ "notifications/resources/list_changed",+ "notifications/prompts/list_changed",+ ]+async def test_concurrent_log_requests_no_mixing(self, proxy_server: FastMCP):"""Test that concurrent log requests don't mix handlers (fixes #1068)."""results: dict[str, LogMessage] = {}