Skip to content

fix(gateway): apply the tool rules to child MCP calls through /svc - #669

Merged
earakely-scale merged 5 commits into
mainfrom
edgararakelyan/svc-mcp-tool-gate
Nov 13, 2026
Merged

earakely-scale merged 5 commits into
mainfrom
edgararakelyan/svc-mcp-tool-gate

Conversation

@earakely-scale

@earakely-scale earakely-scale commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

The gateway applies its per-role tool rules on its own /mcp endpoint. A POST to a backing server's own MCP endpoint through /svc/{service}/... was forwarded without them. So a tool disabled for a role on /mcp, or one hidden by default, could still be called there.

Now, before forwarding, the proxy reads each tools/call in such a POST and checks it against the same rules /mcp uses (_is_disabled).

  • A single call to a disabled tool gets the tool error /mcp gives (isError: true, "Tool 'X' is disabled for role 'r'"). It never reaches the server.
  • A batch containing such a call is refused whole with a JSON-RPC invalid-request error (400). So is a body the gateway can't parse as JSON, such as a gzip-encoded one, which a more lenient server might still unpack and run.
  • Path matching. The endpoint path is matched after dot-segment normalisation and ignoring case, since some servers (Express, by default) route /MCP to /mcp.
  • Hidden tools. The gateway learns which tools are hidden by default from tool discovery, so the check runs discovery first. While discovery can't complete, the call gets the same 503 /step returns. That check is now one helper, _discovery_unavailable, shared by both.

These are unchanged:

  • other /svc routes (REST, export-state, cards);
  • GETs;
  • MCP requests that carry no tools/call.

tools/list through /svc isn't filtered, because listing a tool's name grants nothing. A client that calls a hidden tool through /svc now needs a rule naming that tool, as it already did on /mcp.

How it was tested

  • Unit (tst/unit/env/gateway/rest_proxy_stream_test.py, gateway ASGI harness). The new tests fail with the check removed. All 1,047 gateway unit tests pass. They cover:

    • a disabled tool is refused with /mcp's exact text, at /mcp or /MCP/, and nothing reaches the server;
    • an enabled tool, and the same tool for another role, are forwarded;
    • a tool hidden by default is refused under a "*" rule and forwarded once a rule names it;
    • a batch with a disabled call is refused whole;
    • a non-JSON or gzip body is refused;
    • a site's own MCP endpoint is checked the same way;
    • while discovery can't complete, the call gets a 503;
    • with a rule disabling the tool, paths other than the child MCP endpoint are still forwarded untouched.
  • Integration (tst/integration/env/gateway/gateway_test.py, local Docker). In the new test_a_tool_disabled_for_a_role_is_refused_through_svc_as_on_the_gateway_s_mcp, a real MCP client calls through /mcp and /svc/mcp-email/mcp. The disabled role gets the same refusal on both paths and writes nothing; another role's calls succeed on both. It passed, together with the existing /svc write test (2 passed).

  • Manual, at 6c0cb51. Two local gateways, one built from main and one from this branch, each in front of a file-storage server and a mail server on Postgres, loaded with their QC fixtures. A file read is disabled for one user.

    Through /svc/<server>/mcp main this branch
    the disabled read reached the server refused, with /mcp's error
    the same read by a user without access the server's own "not found" same
    an enabled tool works works
    17 bypass spellings, sent byte for byte with http.client 10 reached the server all refused
    • The 17 spellings: trailing slash, dot segments, public%2Fx/../mcp, %2E%2E, x%2F..%2Fmcp, %6dcp, MCP, //mcp, ?x=1, an escaped name, a duplicate name key, UTF-8 BOM, UTF-16, gzip, batch, text/plain, and the plain call.
    • Regression. Both servers' whole generated QC suites ran through /svc, with every tool enabled, on main and on this branch: 1,264 tests. They had the same outcome test by test (0 differ).
    • Faults.
      • 200 concurrent calls: every disabled call was refused and every enabled call was answered.
      • 300 calls while the rule flipped every 20 ms: each got either the refusal or the server's answer; none got a 5xx or hung.
      • The server killed with 200 calls in flight: disabled calls were still refused, and enabled ones got a fast 502. After a restart, both work again.
      • Another server down: no effect on this one.
      • A gateway whose discovery can't complete (a tool-name clash): /svc tool calls get 503 here, where main forwarded them.
    • A hidden tool. A mail server's generated suite calls a tool hidden by default. On main it passes (16 passed). On this branch it is refused under a "*" rule, and passes once a rule names the tool (16 passed).
    • Integration on local Docker. The 19 proxy-, role- and website-related tests in gateway_test.py gave 9 passed and 10 skipped (the remote-sandbox variants).

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no outstanding blocking issue was found.

Summary

The PR applies role-based tool rules to child MCP calls through /svc.

  • The gateway checks child MCP tool calls against each caller’s role rules.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Child MCP POST through /svc] --> B[Check routed and forwarded paths]
  B --> C[Read tool names]
  C --> D[Discover tools and check role rules]
  D -->|Disabled or unavailable| E[Return refusal]
  D -->|Allowed| F[Stamp role and forward to child]
Loading

Reviews (6) · Last reviewed commit: "fix(gateway): judge the child MCP path b..." · Reviewed by Greptile

earakely-scale and others added 3 commits November 13, 2026 10:02
A POST to a backing server's own MCP endpoint through /svc reached the server
without the gateway's per-role tool rules, so a tool disabled (or hidden by
default) for a role on /mcp stayed callable there. The proxy now checks each
tools/call against the same rules before forwarding: a single call gets the
tool error /mcp gives, a batch with such a call is refused whole, and neither
reaches the server. Other /svc routes are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A body the gateway can't parse as JSON (a gzip-encoded one, say) was
forwarded unchecked, and a server that inflates or otherwise parses it more
leniently would run the call. Such a POST is now refused with an invalid
request error and never forwarded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale requested a review from a team as a code owner November 13, 2026 18:27
Some servers route paths case-insensitively (Express does by default), so
/svc/<child>/MCP could reach the MCP endpoint without the tool check or the
role stamp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/agent_env/env/gateway/gateway.py Resolved
… too

/svc/<child>/public%2Fx/../mcp routes here as public/x/../mcp, but forward()
keeps the encoded slash and httpx then removes 'public%2Fx/..', so the child
received /mcp without the tool check or the role stamp. Both now check the
path as routed and the path sent, as reserved_site_request does for sites.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale merged commit 7171e25 into main Nov 13, 2026
37 checks passed
@earakely-scale
earakely-scale deleted the edgararakelyan/svc-mcp-tool-gate branch November 13, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant