MCP guardrails
Every guardrail can run before, during and after an MCP tool call to inspect arguments and results. Two guardrails ship specifically for the MCP gateway. mcp_security stops a request from pointing a model at an MCP server nobody registered, which is how unapproved tools sneak in. mcp_jwt_signer gives your MCP servers one signing authority to trust, so a server verifies who is calling from the gateway's keys instead of from every identity provider your users sign in with.
A real-world scenario
A developer pastes a snippet that adds "server_url": "https://mcp.some-startup.example/mcp" to the tools of a Responses API request. With mcp_security on, the gateway rejects the request with a message naming the unregistered server, and the security team sees the attempt in the logs. Meanwhile the company's internal MCP servers want to know which employee is behind each tool call. With mcp_jwt_signer on, every tools/call carries a short lived RS256 token with the employee's email, signed by the gateway, and the servers verify it against the gateway's /.well-known/jwks.json.
MCP security
mcp_security checks the tools array of a chat completion or Responses API request. Any tool of type: mcp whose server_url points at the gateway (spanway_proxy/mcp/{server_name}) must name a server that is registered. Requests that reference an unknown server are rejected with status 400. That is the default: when on_violation is left out, or set to anything other than alert or warn, the guardrail blocks. Set on_violation: alert only while you find out which requests would be refused, because then the request passes and the violation is only logged.
-
Add the guardrail to
config.yamland restart, or in the Admin UI open Guardrails, click Add Guardrail, choose themcp_securitytype, set the mode to pre-call and turn Default on on:guardrails:- guardrail_name: mcp-registered-onlyspanway_params:guardrail: mcp_securitymode: pre_calldefault_on: trueon_violation: block -
Send a request that names an unregistered server:
curl -X POST http://localhost:4000/v1/responses \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"model": "claude-sonnet-5-5", "input": "hi", "tools": [{"type": "mcp", "server_label": "shadow", "server_url": "spanway_proxy/mcp/shadow-it-server"}]}'Expected result, status 400, in the OpenAI error shape. The guardrail details sit under
error.provider_specific_fields:{"error": {"message": "Violated guardrail policy","type": "invalid_request_error","param": null,"code": "400","provider_specific_fields": {"error": "Violated guardrail policy","guardrail": "mcp_security","unregistered_servers": ["shadow-it-server"],"detection_message": "MCP Security: request references unregistered MCP server(s): shadow-it-server. Only servers registered on this gateway are allowed."}}} -
The same request with
spanway_proxy/mcp/jira, a registered server, goes through. On the Logs page the rejected request showsmcp_securityunder the guardrails that ran.
Limit a customer's MCP tools
When your application sends a customer id in the user field, the mcp_end_user_permission guardrail removes the MCP tools that customer may not use before the request reaches the model. Use it when one key serves many customers and each customer should only see the MCP servers you gave them, for example a free plan with a docs server and a paid plan that also gets a CRM server.
-
Give the customer its servers with
object_permission.mcp_servers, on the Customers page or with/customer/newor/customer/update, see customers. -
Add the guardrail to
config.yamland restart:guardrails:- guardrail_name: customer-mcp-toolsspanway_params:guardrail: mcp_end_user_permissionmode: pre_call -
Send a request with
"user": "acct-48213"and tools from two servers. Tools named<server>-<tool>from a server outside the customer's list are dropped, and the model only sees the rest.
The guardrail is on for every request unless you set default_on: false. A request without a customer id, or a customer with no mcp_servers list, keeps every tool, so key and team permissions still decide. Tools whose names have no <server>- prefix are never removed. Each removed tool writes a WARNING line MCP guardrail: removing tool '<tool>' (server: '<server>') ... with the tool and server names, never the prompt.
MCP JWT signer
On every tools/list and tools/call the guardrail mints an RS256 JWT carrying the end user's identity and the scopes of the call and sends it to the upstream server.
-
Set
MCP_JWT_SIGNING_KEYto a PEM string orfile:///path/to/key.pem. When it is unset an RSA 2048 key pair is generated at startup, which means tokens signed before a restart stop verifying, so set the variable in production. -
Add the guardrail:
guardrails:- guardrail_name: mcp-jwt-signerspanway_params:guardrail: mcp_jwt_signermode: pre_mcp_calldefault_on: trueissuer: https://gateway.example.comaudience: mcpttl_seconds: 300end_user_claim_sources: ["token:sub", "token:email", "spanway:user_id"]required_claims: ["sub"]optional_claims: ["groups"]add_claims: {deployment_id: prod-001}allowed_scopes: ["mcp:tools/call", "mcp:tools/list"] -
Point your MCP servers at the gateway's keys. They fetch the public key from
GET /.well-known/openid-configurationandGET /.well-known/jwks.json:curl http://localhost:4000/.well-known/jwks.jsonExpected result:
{"keys": [{"kty": "RSA", "use": "sig", "alg": "RS256", "kid": "spanway-mcp-2026-11", "n": "...", "e": "AQAB"}]} -
Call a tool through the gateway. The upstream server receives an
Authorization: Bearer <jwt>whose payload carriesiss,aud,sub,expandscope. Withdebug_headers: truethe gateway also returns anx-spanway-mcp-debugheader describing the signed claims so you can check them from the client side.
| Option | Meaning |
|---|---|
issuer, audience, ttl_seconds | Claims of the minted token. |
access_token_discovery_uri, token_introspection_endpoint, verify_issuer, verify_audience | Verify the caller's incoming bearer token against the identity provider's JWKS, or introspect an opaque token, before signing. |
end_user_claim_sources | Ordered list of where the subject comes from. token:<claim> reads the verified incoming token, spanway:user_id, spanway:email, spanway:end_user_id and spanway:team_id read the gateway's own view of the caller. |
required_claims, optional_claims | Incoming claims that must be present, and claims copied through into the outbound token. |
add_claims, set_claims, remove_claims | Add a claim when missing, always set it, or strip it from the final token. |
channel_token_audience, channel_token_ttl | Issue a second token for the transport channel with its own audience. |
allowed_scopes | Explicit scope list instead of the generated one. |
debug_headers | Emit an x-spanway-mcp-debug header describing the signed claims. |
Fetched JWKS documents are cached for an hour.
Errors
| Status | Where | Meaning | Fix |
|---|---|---|---|
400 Violated guardrail policy with unregistered_servers | Chat and Responses requests | A type: mcp tool names a server the gateway does not know. | Register the server on MCP Servers, or remove it from tools. With on_violation: alert the request passes and the violation is only logged. |
Guardrail refuses to sign, required_claims missing | MCP tool calls | The incoming token lacks a claim listed in required_claims, or no claim source produced a subject. | Add a spanway: source as a fallback, or fix the identity provider's token. |
| Upstream server rejects the token after a restart | MCP tool calls | MCP_JWT_SIGNING_KEY is unset, so the key pair changed on restart. | Set the variable to a stable key. |
| Upstream server rejects the token as expired | MCP tool calls | ttl_seconds is shorter than the server's clock skew tolerance or the call took longer. | Raise ttl_seconds modestly. |
| Guardrail missing from the Logs entry | Logs page | The guardrail is not attached to the key or team and default_on is false. | Set default_on: true or attach it to the key, see guardrails. |
Logs
| Level | Line | What it means | Action |
|---|---|---|---|
| INFO | MCPJWTSigner: auto-generated RSA-2048 keypair (set MCP_JWT_SIGNING_KEY to use your own key) | The JWT signer made a new key pair at start, so tokens it signed before a restart no longer verify. | Set MCP_JWT_SIGNING_KEY to a stable key in production. |
| WARNING | MCP guardrail: removing tool '<tool>' (server: '<server>'), not in end user's allowed servers | The end user permission guardrail dropped a tool the request's customer may not use. | None, unless the customer should have that server. Then add it to the customer's object_permission.mcp_servers. |
| INFO | MCPJWTSigner: loaded RSA key from env var MCP_JWT_SIGNING_KEY | The signer uses your key. | None. |
Related
- Guardrails for modes, attaching guardrails to keys and teams and the Guardrails page.
- MCP gateway for how
toolsnames a registered server. - MCP OAuth for the gateway's JWKS and OpenID configuration routes.
- MCP access control for restricting which client applications may connect.