Protect an MCP server
Put the gateway in front of an MCP server and allow or refuse each tool, resource and prompt by name, for each agent or user.
APIModel Context Protocol
Your MCP server offers get_weather and delete_records at one address, and agents should have the first but not
the second. With the gateway in front of it, each MCP call is decided as the tool it names
(see MCP tool calls), and an MCP client that has never met your server finds where
to sign in from the gateway’s metadata. This guide is for teams exposing MCP servers to agents and
assistants.
Before you start: set up the gateway as in Get started with Gateway, with your MCP server
as its BACKEND. Its image isn’t published yet: ask your Cvyūh contact for it. Here the server’s audience is
https://gw.example.com/mcp. You’re a realm administrator in a group holding RealmOwner, with your session in
$CVYUH_SESSION (Call the API).
Write the policies
Section titled “Write the policies”-
Create the resource types MCP calls are decided as. For tools:
Terminal window curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/resourceType?id=tool" \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "patterns": ["https://gw.example.com/mcp/tool/*"], "actions": { "tools/call": true } }'Create
mcp_serverthe same way, with the patternhttps://gw.example.com/mcpand the actiontools/list. Addresource,promptandtaskif your server offers them: the patterns and actions are on MCP tool calls. -
Create a policy set for the server. Use the
regexcomparator, which matches tool names whole and respects case:Terminal window curl -i "https://foobar.cvyuh.com/api/v0/am/objects/v1/policySet?id=mcp-weather" \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "comparator": "regex", "subjectTypes": ["JwtClaim"], "conditionTypes": ["OAuth2Scope"] }'Then link each type to it, with the revision the last answer’s
ETaggave:Terminal window curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policySet/mcp-weather/relationships/resourceTypes \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-H 'If-Match: "<rev>"' \-d '{ "targetId": "tool" }' -
Allow agents one tool:
Terminal window curl -i "https://foobar.cvyuh.com/api/v0/am/objects/v1/policy?id=weather-call" \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "active": true,"resources": ["https://gw.example.com/mcp/tool/get_weather"],"actionValues": { "tools/call": true },"subject": { "type": "JwtClaim", "claimName": "principal_type", "claimValue": "agent" } }'Link it to the
mcp-weatherset and thetooltype, as in Get started with Gateway. -
Allow agents to list the tools, with a policy on
mcp_server:resources: ["https://gw.example.com/mcp"]andactionValues: { "tools/list": true }, linked to the set and themcp_servertype.
Tell the gateway
Section titled “Tell the gateway”-
Restart the gateway with
POLICY_SET=mcp-weather,MCP_PATHS=/mcpandJWT_AUDIENCE=https://gw.example.com/mcp: see Gateway configuration. -
Optionally, set
SCOPESto the scopes MCP clients should ask for; the metadata names them.
Check it worked
Section titled “Check it worked”- An MCP client’s first call gets
401, with a challenge pointing athttps://gw.example.com/.well-known/oauth-protected-resource/mcp. That document names the realm, where the client signs the user in. - With an agent’s token,
tools/listandtools/callforget_weatherreach the server.ping,notifications/*andinitializepass on any valid token. tools/callfordelete_recordsis refused403, with a JSON-RPC error MCP clients read natively.tools/liststill listsdelete_records: a listing shows every tool the server has, including ones the caller will be refused.
Undo it
Section titled “Undo it”Unset MCP_PATHS to decide the server’s requests as plain HTTP again, or delete the weather-call policy to refuse
the tool.
Related
Section titled “Related”- MCP tool calls: every method, and how it’s decided.
- Accept apps identified by a URL: MCP clients that identify themselves by a metadata document.
- Token swap: give the server a token meant for it alone.