diff --git a/plugins/lookup/mcp_servers.py b/plugins/lookup/mcp_servers.py index bda568a1a7..8a67782b17 100644 --- a/plugins/lookup/mcp_servers.py +++ b/plugins/lookup/mcp_servers.py @@ -10,14 +10,23 @@ credential actually resolves, reaches ``selected``. Everything else lands in ``rejected`` with a stable code: ``consumer_not_allowed``, ``transport_unsupported``, ``auth_unsupported``, -``credential_missing`` and ``endpoint_unreachable``. +``credential_missing``, ``endpoint_unreachable`` and ``tools_unenforceable``. ``consumer_not_allowed`` is a decision and simply narrows the result. -``transport_unsupported``, ``auth_unsupported`` and ``endpoint_unreachable`` -mean the provider did authorize this consumer and the connection still cannot -be rendered, so they abort the run instead of disappearing quietly: a client -that silently ends up with fewer tools than the deployment declared is -indistinguishable from one that works. +``transport_unsupported``, ``auth_unsupported``, ``endpoint_unreachable`` and +``tools_unenforceable`` mean the provider did authorize this consumer and the +connection still cannot be rendered, so they abort the run instead of +disappearing quietly: a client that silently ends up with fewer tools than the +deployment declared is indistinguishable from one that works. + +``tools_unenforceable`` is that same rule applied to the tool policy rather +than the connection. A client filters by tool name, so a provider that resolves +no callable name gives it nothing to pin and the rendered filter would be +empty. That happens two ways, and neither is a working pairing: a surface +composed at runtime keeps its contract in ``tools.categories``, which no +per-tool filter can express, and a surface that withholds every tool it serves +as mutating has nothing left to offer. Advertising either as a configured +server would leave an agent wired to a provider it can never call. Providers are narrowed to ``application_closure(deployment.whitelist)``, the same set ``sys-service-loader`` preloads from. Inventory membership, ``group_names`` @@ -56,8 +65,11 @@ REJECT_TRANSPORT = "transport_unsupported" REJECT_AUTH = "auth_unsupported" REJECT_CREDENTIAL = "credential_missing" REJECT_ENDPOINT = "endpoint_unreachable" +REJECT_TOOLS = "tools_unenforceable" -FATAL_REJECTIONS = frozenset({REJECT_TRANSPORT, REJECT_AUTH, REJECT_ENDPOINT}) +FATAL_REJECTIONS = frozenset( + {REJECT_TRANSPORT, REJECT_AUTH, REJECT_ENDPOINT, REJECT_TOOLS} +) RECONCILE_STRICT_VAR = "MCP_RECONCILE_STRICT" @@ -199,6 +211,19 @@ def build_mcp_discovery( ) continue + tools = list(server.get("tools") or []) + mutating = list(server.get("mutating") or []) + if not [tool for tool in tools if tool not in mutating]: + reject( + server_id, + REJECT_TOOLS, + f"{server_id} serves {len(tools)} tool(s) and withholds " + f"{len(mutating)}, leaving no name a client could pin; " + f"{consumer_id} filters by tool name and would render an " + f"empty filter", + ) + continue + selected.append( { "id": server_id, @@ -207,8 +232,8 @@ def build_mcp_discovery( "auth": auth, "auth_subject": server.get("auth_subject"), "owner": owner, - "tools": list(server.get("tools") or []), - "mutating": list(server.get("mutating") or []), + "tools": tools, + "mutating": mutating, "transport": transport.replace("_", "-"), } ) diff --git a/roles/web-app-hermes/templates/config.yaml.j2 b/roles/web-app-hermes/templates/config.yaml.j2 index 2f559dc7c3..b70d7a5d63 100644 --- a/roles/web-app-hermes/templates/config.yaml.j2 +++ b/roles/web-app-hermes/templates/config.yaml.j2 @@ -13,7 +13,5 @@ mcp_servers:{{ ' {}' if renderable | length == 0 else '' }} resources: true prompts: false {% set callable_tools = server.tools | reject('in', server.mutating | default([])) | list %} -{% if callable_tools %} include: {{ callable_tools | to_json }} -{% endif %} {% endfor %} diff --git a/roles/web-app-openclaw/templates/openclaw.json.j2 b/roles/web-app-openclaw/templates/openclaw.json.j2 index c765205b9b..0b96f2c4ef 100644 --- a/roles/web-app-openclaw/templates/openclaw.json.j2 +++ b/roles/web-app-openclaw/templates/openclaw.json.j2 @@ -27,9 +27,8 @@ {{ server.id | to_json }}: { "url": {{ server.url | to_json }}, "transport": {{ server.transport | to_json }}, - "headers": {"Authorization": {{ server | mcp_authorization | to_json }}}{% if callable_tools %}, - "toolFilter": {"include": {{ callable_tools | to_json }}}{% endif %} - + "headers": {"Authorization": {{ server | mcp_authorization | to_json }}}, + "toolFilter": {"include": {{ callable_tools | to_json }}} }{{ "," if not loop.last }} {% endfor %} } diff --git a/tests/unit/python/plugins/lookup/test_mcp_servers.py b/tests/unit/python/plugins/lookup/test_mcp_servers.py index 93377772fb..6ef82cbe62 100644 --- a/tests/unit/python/plugins/lookup/test_mcp_servers.py +++ b/tests/unit/python/plugins/lookup/test_mcp_servers.py @@ -36,6 +36,8 @@ HOMEASSISTANT = { "key": "web-app-homeassistant", }, "endpoint": {"service_key": "homeassistant", "port": 8123, "path": "/api/mcp"}, + "tools": ["GetDateTime", "HassTurnOn"], + "mutating": ["HassTurnOn"], } CREDENTIALS = {"web-app-homeassistant": ("ha-token", "mcp-web-app-homeassistant")} @@ -176,6 +178,35 @@ class TestBuildMcpDiscovery(unittest.TestCase): reasons(result), {"web-app-homeassistant": "endpoint_unreachable"} ) + def test_a_provider_naming_no_tool_is_rejected(self): + """A runtime-composed surface keeps its contract in ``tools.categories``. + + A client filters by tool name, so that contract reaches it as no + contract at all: the rendered filter would be empty and the agent + would carry a server it can never call. + """ + dynamic = dict(HOMEASSISTANT, tools=[], mutating=[]) + result = build_mcp_discovery([dynamic], CONSUMER, CLIENT, CREDENTIALS) + self.assertEqual(result["selected"], []) + self.assertEqual( + reasons(result), {"web-app-homeassistant": "tools_unenforceable"} + ) + + def test_a_provider_withholding_every_tool_it_serves_is_rejected(self): + withheld = dict(HOMEASSISTANT, tools=["HassTurnOn"], mutating=["HassTurnOn"]) + result = build_mcp_discovery([withheld], CONSUMER, CLIENT, CREDENTIALS) + self.assertEqual(result["selected"], []) + self.assertEqual( + reasons(result), {"web-app-homeassistant": "tools_unenforceable"} + ) + + def test_a_provider_keeping_one_callable_tool_is_selected(self): + entry = build_mcp_discovery([HOMEASSISTANT], CONSUMER, CLIENT, CREDENTIALS)[ + "selected" + ][0] + self.assertEqual(["GetDateTime", "HassTurnOn"], entry["tools"]) + self.assertEqual(["HassTurnOn"], entry["mutating"]) + def test_order_of_the_discovered_servers_is_kept(self): jenkins = dict(HOMEASSISTANT, id="web-app-jenkins") credentials = dict(CREDENTIALS, **{"web-app-jenkins": ("jk", "mcp-jenkins")}) @@ -242,6 +273,7 @@ class TestAssertAuthorizedAreRenderable(unittest.TestCase): "transport_unsupported", "auth_unsupported", "endpoint_unreachable", + "tools_unenforceable", ): with self.subTest(reason=reason), self.assertRaises(AnsibleError): self.assert_renderable( diff --git a/tests/unit/python/roles/agents/test_mcp_tool_filter.py b/tests/unit/python/roles/agents/test_mcp_tool_filter.py index aa19a0ea66..4ec2df064a 100644 --- a/tests/unit/python/roles/agents/test_mcp_tool_filter.py +++ b/tests/unit/python/roles/agents/test_mcp_tool_filter.py @@ -6,6 +6,11 @@ tools (backward-compatible default)". OpenClaw's ``toolFilter`` is absent unless written. A provider that grows a mutating tool would therefore reach both agents without anyone declaring it, so the rendered configuration has to carry the allowlist rather than rely on the default. + +That holds for the empty allowlist too: a provider that declares none is the +case where the default is most dangerous, so the filter is rendered +unconditionally, the way ``web-app-n8n``'s client node pins ``include`` to +``selected`` whatever ``includeTools`` resolves to. """ from __future__ import annotations @@ -94,14 +99,15 @@ class TestAgentToolFilter(unittest.TestCase): entry["tools"]["include"], ) - def test_hermes_omits_the_key_when_no_allowlist_is_declared(self) -> None: + def test_hermes_pins_an_empty_allowlist_rather_than_omitting_it(self) -> None: rendered = _yaml(_render(HERMES, "config.yaml.j2", HERMES_MCP_SERVERS=SERVERS)) entry = rendered["mcp_servers"]["web_app_prometheus"] - self.assertNotIn( - "include", - entry["tools"], - "an empty include list would register nothing at all, which reads " - "as a working client serving no tools", + self.assertEqual( + [], + entry["tools"]["include"], + "omitting the key means 'register all tools' upstream, so a " + "provider that declares no allowlist would reach the agent with " + "every tool it happens to serve", ) def test_openclaw_pins_the_declared_allowlist(self) -> None: @@ -120,7 +126,7 @@ class TestAgentToolFilter(unittest.TestCase): entry["toolFilter"]["include"], ) - def test_openclaw_stays_valid_json_with_and_without_the_filter(self) -> None: + def test_openclaw_stays_valid_json_with_an_empty_filter(self) -> None: rendered = json.loads( _render( OPENCLAW, @@ -130,7 +136,10 @@ class TestAgentToolFilter(unittest.TestCase): OPENCLAW_MCP_SERVERS=SERVERS, ) ) - self.assertNotIn("toolFilter", rendered["mcp"]["servers"]["web-app-prometheus"]) + self.assertEqual( + [], + rendered["mcp"]["servers"]["web-app-prometheus"]["toolFilter"]["include"], + ) self.assertEqual( {"svc-db-qdrant", "web-app-prometheus", "web-app-baserow"}, set(rendered["mcp"]["servers"]),