Repository navigation
Check MCP allowed_tools in one way on HTTP, on a stream and in the SDKs - #964
Merged
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three hardening changes to the check of
operation.allowed_toolsfor MCP. Asecurity review of the tool scope found them after #961. None of them lets a
caller run a tool outside
allowed_toolson a path that a release ships, sothey are not advisories. Each commit is one change with its tests, and each
commit message describes the defect, the fix and its limits.
api, envoy: check the MCP tool name as the backend receives itscheme://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_prpassed asget_profmcp://github./mesh/mcpstream.api, node: allow the same MCP session requests on HTTP and on a streamresources/listandprompts/listpass as session requests. The stream check denied them.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 wordauthorizeCallerandauthorize_callertook a request with no tool on/mesh/mcp/1.0.0for 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 everytools/call.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 noallowed_toolsdoes notchange.
resources/listandprompts/listget403. An MCP client that lists resources or promptswhen it connects sees an error for those two requests.
initialize,server/discover,ping,tools/listand thetools/callof a listedtool 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.scheme://service/prefix, or with spacesaround it, is denied.
allowed_toolscannot hold such a name(
ValidateTaskAuthorizationRulerefuses a slash). The node and the SDKssplit
mcp://service/toolbefore they send a call, so no client of thisrepository sends the prefixed name.
authorizer is the
/libp2p-httphandler, which always gives an HTTPmethod. An application that calls the exported authorizer for an MCP stream
must now set the new field for a session request.
/mesh/mcpstream: no change for a caller. The pass-through answersserver/discoveritself before the task check.Tests
Each new test fails on the code before its commit:
TestTaskRuleChecksTheToolNameAsTheBodyStatesIt(api): four names thatonly resemble the allowed tool were checked as that tool.
TestMCPService_TaskCheckDecidesARequestAsTheHTTPInspection(
internal/node):server/discover,resources/listandprompts/listhad 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 flagwas allowed.
Run locally on this branch:
make lint,make verify,make test(racedetector, 29 packages, integration included),
make sdk-js,make sdk-python.make e2e-testwas not run locally; thebatscheck of this pull requestruns it.
A flaky test, fixed in the fourth commit
The first run of the
testcheck on this pull request failed inTestIngressDatalogPolicyEvaluation(api), which this change does nottouch. 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 detectorread as a denial. It reproduces on one CPU (
taskset -c 0 stress -p 8: 1failure in 150 runs).
TestBuildPolicyRulesWithStructuredBindingClaimshadthe 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.