Skip to content
GatewayMarkdown

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).

  1. 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_server the same way, with the pattern https://gw.example.com/mcp and the action tools/list. Add resource, prompt and task if your server offers them: the patterns and actions are on MCP tool calls.

  2. Create a policy set for the server. Use the regex comparator, 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 ETag gave:

    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" }'
  3. 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-weather set and the tool type, as in Get started with Gateway.

  4. Allow agents to list the tools, with a policy on mcp_server: resources: ["https://gw.example.com/mcp"] and actionValues: { "tools/list": true }, linked to the set and the mcp_server type.

  1. Restart the gateway with POLICY_SET=mcp-weather, MCP_PATHS=/mcp and JWT_AUDIENCE=https://gw.example.com/mcp: see Gateway configuration.

  2. Optionally, set SCOPES to the scopes MCP clients should ask for; the metadata names them.

  1. An MCP client’s first call gets 401, with a challenge pointing at https://gw.example.com/.well-known/oauth-protected-resource/mcp. That document names the realm, where the client signs the user in.
  2. With an agent’s token, tools/list and tools/call for get_weather reach the server. ping, notifications/* and initialize pass on any valid token.
  3. tools/call for delete_records is refused 403, with a JSON-RPC error MCP clients read natively.
  4. tools/list still lists delete_records: a listing shows every tool the server has, including ones the caller will be refused.

Unset MCP_PATHS to decide the server’s requests as plain HTTP again, or delete the weather-call policy to refuse the tool.