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

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](/gateway/) in front of it, each MCP call is decided as the tool it names
(see [MCP tool calls](/gateway/mcp-servers/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](/gateway/get-started/), 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](/develop/api/)).

## Write the policies

<Steps>

1. **Create the resource types MCP calls are decided as.** For tools:

   ```sh
   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](/gateway/mcp-servers/mcp-tool-calls/#set-it-up).

2. **Create a policy set for the server.** Use the `regex` comparator, which matches tool names whole and respects
   case:

   ```sh
   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:

   ```sh
   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:**

   ```sh
   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](/gateway/get-started/).

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.

</Steps>

## Tell the gateway

<Steps>

1. Restart the gateway with `POLICY_SET=mcp-weather`, `MCP_PATHS=/mcp` and `JWT_AUDIENCE=https://gw.example.com/mcp`: see [Gateway configuration](/gateway/configuration/#mcp-servers).

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

</Steps>

<Aside type="tip" title="One tool name, taken whole">
  A call's tool name is one name of its own: a tool called `get_*` is never read as a pattern, so it can't match
  more than itself.
</Aside>

## Check it worked

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.

## 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

- [MCP tool calls](/gateway/mcp-servers/mcp-tool-calls/): every method, and how it's decided.
- [Accept apps identified by a URL](/workforce-identity/guides/partners/apps-identified-by-url/): MCP clients that identify themselves by a metadata document.
- [Token swap](/gateway/protect-apis/token-swap/): give the server a token meant for it alone.
