Skip to main content

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.

  1. Add the guardrail to config.yaml and restart, or in the Admin UI open Guardrails, click Add Guardrail, choose the mcp_security type, set the mode to pre-call and turn Default on on:

    guardrails:
    - guardrail_name: mcp-registered-only
    spanway_params:
    guardrail: mcp_security
    mode: pre_call
    default_on: true
    on_violation: block
  2. 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."
    }
    }
    }
  3. The same request with spanway_proxy/mcp/jira, a registered server, goes through. On the Logs page the rejected request shows mcp_security under 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.

  1. Give the customer its servers with object_permission.mcp_servers, on the Customers page or with /customer/new or /customer/update, see customers.

  2. Add the guardrail to config.yaml and restart:

    guardrails:
    - guardrail_name: customer-mcp-tools
    spanway_params:
    guardrail: mcp_end_user_permission
    mode: pre_call
  3. 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.

  1. Set MCP_JWT_SIGNING_KEY to a PEM string or file:///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.

  2. Add the guardrail:

    guardrails:
    - guardrail_name: mcp-jwt-signer
    spanway_params:
    guardrail: mcp_jwt_signer
    mode: pre_mcp_call
    default_on: true
    issuer: https://gateway.example.com
    audience: mcp
    ttl_seconds: 300
    end_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"]
  3. Point your MCP servers at the gateway's keys. They fetch the public key from GET /.well-known/openid-configuration and GET /.well-known/jwks.json:

    curl http://localhost:4000/.well-known/jwks.json

    Expected result:

    {"keys": [{"kty": "RSA", "use": "sig", "alg": "RS256", "kid": "spanway-mcp-2026-11", "n": "...", "e": "AQAB"}]}
  4. Call a tool through the gateway. The upstream server receives an Authorization: Bearer <jwt> whose payload carries iss, aud, sub, exp and scope. With debug_headers: true the gateway also returns an x-spanway-mcp-debug header describing the signed claims so you can check them from the client side.

OptionMeaning
issuer, audience, ttl_secondsClaims of the minted token.
access_token_discovery_uri, token_introspection_endpoint, verify_issuer, verify_audienceVerify the caller's incoming bearer token against the identity provider's JWKS, or introspect an opaque token, before signing.
end_user_claim_sourcesOrdered 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_claimsIncoming claims that must be present, and claims copied through into the outbound token.
add_claims, set_claims, remove_claimsAdd a claim when missing, always set it, or strip it from the final token.
channel_token_audience, channel_token_ttlIssue a second token for the transport channel with its own audience.
allowed_scopesExplicit scope list instead of the generated one.
debug_headersEmit an x-spanway-mcp-debug header describing the signed claims.

Fetched JWKS documents are cached for an hour.

Errors​

StatusWhereMeaningFix
400 Violated guardrail policy with unregistered_serversChat and Responses requestsA 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 missingMCP tool callsThe 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 restartMCP tool callsMCP_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 expiredMCP tool callsttl_seconds is shorter than the server's clock skew tolerance or the call took longer.Raise ttl_seconds modestly.
Guardrail missing from the Logs entryLogs pageThe 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​

LevelLineWhat it meansAction
INFOMCPJWTSigner: 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.
WARNINGMCP guardrail: removing tool '<tool>' (server: '<server>'), not in end user's allowed serversThe 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.
INFOMCPJWTSigner: loaded RSA key from env var MCP_JWT_SIGNING_KEYThe signer uses your key.None.
  • Guardrails for modes, attaching guardrails to keys and teams and the Guardrails page.
  • MCP gateway for how tools names a registered server.
  • MCP OAuth for the gateway's JWKS and OpenID configuration routes.
  • MCP access control for restricting which client applications may connect.