π The fast, Pythonic way to build MCP servers and clients.
fix(client): keep ClientGroup tool routes after the group context exits (#6268)
nate nowack8ac57b33de92
diff --git a/docs/clients/client-groups.mdx b/docs/clients/client-groups.mdxindex 357ac254c..78eacac08 100644--- a/docs/clients/client-groups.mdx+++ b/docs/clients/client-groups.mdx@@ -70,6 +70,8 @@ async with group:The group aggregates names and detects collisions; it never stands between the adapter and the client, so everything a single `Client` supports keeps working per tool.+Discovered routes outlive the group's context. After the last exit, `resolve_tool()` and `call_tool()` for a known tool need only that tool's own client to be connected, so an adapter that bound its tools inside the group can keep calling them under the owning client's context. The next unknown name after the last exit reloads the catalog, which requires every client. Call `list_tools()` to drop routes for tools a server has since removed.+## List skills across servers`list_skills()` collects [Skills extension](/servers/skills) entries from every client that opted into it, keyed by server name, since two servers can publish the same skill URI. Clients created without `SkillsClientExtension()`, including those from `from_config`, and servers that don't advertise the extension are left out.diff --git a/fastmcp_slim/fastmcp/client/group.py b/fastmcp_slim/fastmcp/client/group.pyindex 31e20901f..d4762f4c7 100644--- a/fastmcp_slim/fastmcp/client/group.py+++ b/fastmcp_slim/fastmcp/client/group.py@@ -207,7 +207,9 @@ class ClientGroup:# Closed already, or another context entered while we waited.return Noneself._exit_stack = None- self._tool_routes.clear()+ # Keep the known routes: a route needs only its own client, which a+ # caller may connect without the group. Mark the catalog for lazy+ # rediscovery, so the next unknown name re-reads every server.self._catalog_loaded = Falsereturn await held.__aexit__(exc_type, exc_value, traceback)diff --git a/tests/client/group/test_client_group.py b/tests/client/group/test_client_group.pyindex 43195dcbc..bb94ed331 100644--- a/tests/client/group/test_client_group.py+++ b/tests/client/group/test_client_group.py@@ -321,6 +321,67 @@ async def test_known_route_survives_an_unrelated_dead_client():await group.call_tool("doomed_echo", {"value": "hi"})+async def test_known_route_survives_the_group_context():+ """An adapter that resolved a route inside the group can call it later+ with only the owning client connected."""+ calc = Client(make_server("calc"))+ greet = Client(make_server("greet"))+ group = ClientGroup({"calc": calc, "greet": greet})++ async with group:+ await group.list_tools()+ route = await group.resolve_tool("calc_echo")+ assert not calc.is_connected()++ with (+ patch.object(calc, "list_tools", wraps=calc.list_tools) as calc_list,+ patch.object(greet, "list_tools", wraps=greet.list_tools) as greet_list,+ ):+ async with route.client:+ assert await group.resolve_tool("calc_echo") is route+ result = await group.call_tool("calc_echo", {"value": "hi"})+ assert result.data == "calc: hi"++ with pytest.raises(RuntimeError, match="'greet' is not connected"):+ await group.call_tool("greet_echo", {"value": "hi"})++ assert calc_list.call_count == 0+ assert greet_list.call_count == 0+++async def test_unknown_tool_after_reconnect_rediscovers_lazily():+ server = make_server("server")+ client = Client(server)+ group = ClientGroup({"server": client})++ async with group:+ await group.list_tools()++ @server.tool+ def added() -> str:+ return "new"++ async with group:+ result = await group.call_tool("server_added")+ assert result.data == "new"+ assert (await group.call_tool("server_echo", {"value": "x"})).data == (+ "server: x"+ )+++async def test_unknown_tool_after_exit_needs_every_client_for_discovery():+ calc = Client(make_server("calc"))+ greet = Client(make_server("greet"))+ group = ClientGroup({"calc": calc, "greet": greet})++ async with group:+ await group.list_tools()++ async with calc:+ with pytest.raises(RuntimeError, match="not connected: 'greet'"):+ await group.call_tool("calc_missing")++async def test_partial_connect_failure_unwinds_connected_clients():ok = Client(FastMCPTransport(make_server("ok")))bad = Client(FastMCPTransport(make_server("bad")))