Skip to content

Check MCP allowed_tools in one way on HTTP, on a stream and in the SDKs - #964

Merged
aojea merged 4 commits into
google:mainfrom
aojea:mcp-tool-scope
Nov 27, 2026
Merged

aojea merged 4 commits into
google:mainfrom
aojea:mcp-tool-scope

Conversation

@aojea

@aojea aojea commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Three hardening changes to the check of operation.allowed_tools for MCP. A
security review of the tool scope found them after #961. None of them lets a
caller run a tool outside allowed_tools on a path that a release ships, so
they are not advisories. Each commit is one change with its tests, and each
commit message describes the defect, the fix and its limits.

Commit Problem Change
api, envoy: check the MCP tool name as the backend receives it The HTTP inspection removed spaces and a scheme://service/ prefix from the tool name before the check. The backend got the name unchanged, and the service of the prefix was ignored: mcp://gitlab/get_pr passed as get_pr of mcp://github. The name is checked as the body states it, as on a /mesh/mcp stream.
api, node: allow the same MCP session requests on HTTP and on a stream The HTTP inspection let resources/list and prompts/list pass as session requests. The stream check denied them. One list, api.MCPSessionMethod: initialize, server/discover, ping, tools/list. Both enforcement points use it.
sdk/js, sdk/python: allow an MCP request with no tool only on the provider's word authorizeCaller and authorize_caller took a request with no tool on /mesh/mcp/1.0.0 for the open of a stream and allowed it. The SDKs have no check of each message, so an application that serves MCP on the exported authorizer passed every tools/call. New field allowMCPStreamInit (allow_mcp_stream_init), false by default. Nothing is inferred from the protocol.

Changes that a user sees

All three apply only to a credential that has a task rule with
operation.allowed_tools. A credential with no allowed_tools does not
change.

  • HTTP (router ingress, the node, the Envoy filters): resources/list and
    prompts/list get 403.
    An MCP client that lists resources or prompts
    when it connects sees an error for those two requests. initialize,
    server/discover, ping, tools/list and the tools/call of a listed
    tool work as before. The two list methods were added to the HTTP list with
    external ingress in v0.2.0. A credential that must list resources or
    prompts needs a rule for the service with no allowed_tools.
  • HTTP: a tool name with a scheme://service/ prefix, or with spaces
    around it, is denied.
    allowed_tools cannot hold such a name
    (ValidateTaskAuthorizationRule refuses a slash). The node and the SDKs
    split mcp://service/tool before they send a call, so no client of this
    repository sends the prefixed name.
  • SDKs: no change on a path that the SDKs ship. Their one caller of the
    authorizer is the /libp2p-http handler, which always gives an HTTP
    method. An application that calls the exported authorizer for an MCP stream
    must now set the new field for a session request.
  • /mesh/mcp stream: no change for a caller. The pass-through answers
    server/discover itself before the task check.

Tests

Each new test fails on the code before its commit:

  • TestTaskRuleChecksTheToolNameAsTheBodyStatesIt (api): four names that
    only resemble the allowed tool were checked as that tool.
  • TestMCPService_TaskCheckDecidesARequestAsTheHTTPInspection
    (internal/node): server/discover, resources/list and prompts/list
    had different decisions on the two paths.
  • a credential narrowed to tools passes with no tool only as a session request (sdk/js) and its Python twin: a request with no tool and no flag
    was allowed.

Run locally on this branch: make lint, make verify, make test (race
detector, 29 packages, integration included), make sdk-js, make sdk-python.
make e2e-test was not run locally; the bats check of this pull request
runs it.

A flaky test, fixed in the fourth commit

The first run of the test check on this pull request failed in
TestIngressDatalogPolicyEvaluation (api), which this change does not
touch. The test used the 2 ms default time limit of the Biscuit authorizer
and compared Authorize() with nil, so a slow run under the race detector
read as a denial. It reproduces on one CPU (taskset -c 0 stress -p 8: 1
failure in 150 runs). TestBuildPolicyRulesWithStructuredBindingClaims had
the same defect. Both now use the 5 s limit of the other tests of the
package and stop with the authorizer's error on a time limit. The same loop
is clean for 308 runs.

The HTTP inspection of an MCP tools/call removed the spaces around the tool name and a "scheme://service/" prefix before a task rule checked the name against allowed_tools. The backend receives the body unchanged, so the name that was checked was not the name that the backend got. The service of the prefix was also ignored: with allowed_tools [get_pr] on mcp://github, a call of "mcp://gitlab/get_pr" passed as get_pr of github. A backend that serves tools of more than one service under such names would run a tool that the rule does not allow.

InspectJSONRPCMCPBody now returns the name as the body states it, as the check on a /mesh/mcp stream already does. A name with a prefix or with spaces is not in allowed_tools, so a task rule with allowed_tools denies it. ValidateTaskAuthorizationRule refuses an allowed_tools entry with a slash, so no rule can allow the prefixed name. A request under a credential with no allowed_tools does not change.

No client of this repository puts a prefixed name in params.name: the node and the SDKs split "mcp://service/tool" before they send the call.
A task rule with allowed_tools allows the requests that open or keep a session, and a tools/call of a listed tool. The two enforcement points had their own list of session requests. The check on a /mesh/mcp stream allowed initialize, ping and tools/list. The HTTP inspection also allowed server/discover, resources/list and prompts/list. A credential that grants one tool could thus read the list of the resources and of the prompts of the service on HTTP (router ingress, the node, the Envoy filters), and not on a stream.

api.MCPSessionMethod is now the one list: initialize, server/discover, ping and tools/list. Both enforcement points use it. On HTTP, resources/list and prompts/list are denied under allowed_tools, as every other request that is not a tools/call of a listed tool. On a stream nothing changes for a caller: the pass-through answers server/discover itself before the task check.

The notifications do not change: HTTP allows notifications/initialized and notifications/cancelled, and a stream allows a notifications/ method with no id.

An MCP client that lists resources or prompts when it connects through an ingress or a gateway, with a credential that has allowed_tools, now gets 403 for those two requests. A credential with no allowed_tools does not change.
…vider's word

Under a task rule with allowed_tools, authorizeCaller and authorize_caller allowed a request that names no tool when it had no HTTP method and its protocol was /mesh/mcp/1.0.0. The authorizers took such a request for the open of an MCP stream. agentmesh-node makes the same allowance, and then checks each message of the stream. The SDKs serve no MCP and have no such check, so an application that serves MCP on the exported authorizer authorized the stream one time and then passed every tools/call. A request for a method that is not a tools/call, authorized with an empty tool, also passed.

AuthorizeRequest has a new field, allowMCPStreamInit (allow_mcp_stream_init), false by default. A request with no tool passes a rule with allowed_tools only with this field. The authorizers infer nothing from the protocol, so the application states which request opens or keeps the session, and a request with no tool and no flag is denied.

No path that the SDKs ship changes: their one caller of the authorizer is the /libp2p-http handler, which always gives an HTTP method.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Model Context Protocol (MCP) authorization logic to enforce tool name checks exactly as they are stated in the JSON-RPC request body, removing previous trimming and namespace-splitting behavior. It introduces the MCPSessionMethod helper to identify session-initiating or session-keeping requests and updates the Go, TypeScript, and Python SDKs to support an explicit allowMCPStreamInit flag. This ensures consistent enforcement of task rules with allowed_tools across both stream and HTTP endpoints. I have reviewed the changes and have no feedback to provide as there are no review comments to assess.

TestIngressDatalogPolicyEvaluation and TestBuildPolicyRulesWithStructuredBindingClaims built their authorizer with the default time limit of biscuit-go, 2 ms, and compared the result of Authorize with nil. Under the race detector on a loaded machine a run takes longer than 2 ms. The error of the limit then read as a denial, and the test failed with "expected ... to be allowed" and no cause.

The test check of a pull request failed in this way. The failure reproduces on one CPU: taskset -c 0 stress -p 8 on the race build of the api tests gave 1 failure in 150 runs of TestIngressDatalogPolicyEvaluation, each time at a different assertion.

The two tests now give the authorizer the 5 s limit that the other tests of the package use, and a time limit stops the test with the error of the authorizer. The same stress loop is clean for 308 runs.
@aojea
aojea merged commit 631a7ff into google:main Nov 27, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant