# Configure a WAF for MCP traffic

> **Scope of this page**
>
> This page documents the Zuplo **gateway** runtime (API, AI, and MCP gateways), not the Developer Portal.
>
> It applies to every Zuplo hosting option: Managed Edge, Managed Dedicated, and Self-Hosted. See [Hosting options](https://zuplo.com/docs/articles/hosting-options).

A web application firewall (WAF) tuned for browser traffic often blocks
legitimate Model Context Protocol (MCP) requests. MCP clients aren't browsers.
They send JSON-RPC messages in `POST` bodies that carry whatever text a user or
model puts in a tool call, they can't solve a JavaScript challenge or a CAPTCHA,
and AI platforms send their requests from shared cloud addresses. Default WAF
rules read each of those traits as a sign of an attack.

This page explains where a WAF can break MCP traffic, how to recognize a WAF
block, and how to add exceptions that keep the WAF on for everything else. The
guidance applies to routes that use the MCP Gateway's
[`McpProxyHandler`](./code-config/mcp-proxy-handler.mdx) and to routes that use
the [MCP Server handler](../handlers/mcp-server.mdx).

## Where a WAF sits

A WAF can block MCP traffic in two places:

<Diagram>
  <DiagramNode id="client">MCP client (Claude, ChatGPT, Cursor)</DiagramNode>
  <DiagramNode id="waf1" variant="orange">
    WAF in front of the gateway
  </DiagramNode>
  <DiagramNode id="zuplo" variant="zuplo">
    Zuplo MCP route
  </DiagramNode>
  <DiagramNode id="waf2" variant="orange">
    WAF in front of the upstream
  </DiagramNode>
  <DiagramNode id="upstream">Upstream MCP server</DiagramNode>
  <DiagramEdge from="client" to="waf1" />
  <DiagramEdge from="waf1" to="zuplo" />
  <DiagramEdge from="zuplo" to="waf2" />
  <DiagramEdge from="waf2" to="upstream" />
</Diagram>

- **In front of your gateway.** You proxy your custom domain through a WAF such
  as Cloudflare, Akamai, or AWS WAF with CloudFront. Requests from MCP clients
  pass through the WAF before they reach Zuplo. See
  [Zuplo + WAF/DDoS Services](../articles/waf-ddos.mdx) for how each WAF
  connects to Zuplo.
- **In front of an upstream MCP server.** The MCP server that a gateway route
  proxies to sits behind its own WAF, for example an Application Load Balancer
  with AWS WAF. Requests from Zuplo pass through that WAF before they reach the
  server.

The same rules cause trouble in both places, but the fix for the second one also
involves proving that a request came from your gateway. See
[Protect an upstream MCP server behind a WAF](#protect-an-upstream-mcp-server-behind-a-waf).

## Recognize a WAF block

A WAF block rarely looks like a WAF block from inside an MCP client. Look for
these signs:

- The client reports a connection failure, an unexpected content type, or a JSON
  parse error, and the raw response is an HTML page with a `403` or `503` status
  instead of a JSON-RPC message.
- The server works when you call it with `curl` from your own machine, but fails
  from a hosted AI platform such as ChatGPT or Claude.
- Sign-in fails before the client ever calls a tool, because the client can't
  read the OAuth metadata documents.
- Some tool calls fail and others succeed, depending on what the arguments
  contain. For example, a call that passes a SQL query or a code snippet fails,
  and a call that passes a plain search term works.
- The request never appears in your Zuplo [logs](./observability/logging.mdx).
  When the WAF in front of the gateway blocks a request, the gateway never sees
  it.

To confirm, search the WAF's event log (Security Events in Cloudflare, sampled
requests or WAF logs in AWS) for the MCP route's path. The log entry names the
rule that matched.

## What blocks MCP traffic

| WAF feature                                       | Why it blocks MCP traffic                                                                                                                                                                                        |
| ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Bot management and challenges                     | Challenges, CAPTCHAs, and bot scores assume a browser. An MCP client can't run a JavaScript challenge, so it receives an HTML challenge page. Settings that block AI bots can also block AI platforms by design. |
| Managed rulesets (OWASP, SQL injection, XSS, RCE) | Tool arguments and results often contain SQL, shell commands, HTML, file paths, or source code. Signature rules match that text even though the request is legitimate.                                           |
| Request body size limits                          | A `tools/call` request with a long prompt, a document, or a large argument object exceeds limits such as the 8 KB body rule in the AWS managed core rule set.                                                    |
| User-Agent rules                                  | Server-to-server requests often carry a library's User-Agent or none at all. Rules that block a missing or non-browser User-Agent, such as `NoUserAgent_HEADER` in AWS, match them.                              |
| Per-IP rate limits and IP reputation              | A hosted AI platform sends requests for many users from a small set of cloud addresses, and so does your gateway when it calls an upstream. One busy tenant can push everyone over a per-IP limit.               |
| Geographic or hosting-provider blocks             | Requests from AI platforms come from cloud data centers, which some rules treat as suspicious or place in an unexpected country.                                                                                 |
| Method and content-type rules                     | MCP requests are `POST` with `Content-Type: application/json` and `Accept: application/json, text/event-stream`. Responses can be `text/event-stream`. Rules that allow only typical form or page traffic fail.  |
| Proxy timeouts and response buffering             | A slow tool call can exceed a proxy's response timeout, and a proxy that buffers responses delays or breaks streamed results.                                                                                    |
| Rules that require credentials on every request   | OAuth discovery and sign-in requests carry no bearer token. A rule that requires an `Authorization` header blocks sign-in before the client gets a token.                                                        |

## Add narrow exceptions

Turning the WAF off for your whole domain fixes MCP traffic, but it also removes
protection from everything else. Instead, follow these principles:

1. **Scope each exception to MCP paths.** Match the hostname and the path prefix
   of your MCP routes, such as `/mcp/`, and the `POST` method. Leave every other
   path under the full rule set.
1. **Skip individual rules, not whole rulesets.** Use the WAF event log to find
   the specific rules that match MCP requests, and skip or relax only those.
1. **Log before you block, and keep logging.** Switch a rule to log-only (Log in
   Cloudflare, Count in AWS WAF) on MCP paths, watch the results, and keep
   logging turned on for your exceptions so you can still see what they let
   through.
1. **Replace challenges with authentication.** MCP routes already authenticate
   every request with an [MCP OAuth policy](./auth/overview.mdx), so a browser
   challenge adds nothing there. Turn challenges off on MCP paths and rely on
   the token instead.
1. **Rate limit by user, not by IP address.** Apply Zuplo
   [rate limiting](../policies/rate-limit-inbound.mdx) per authenticated user so
   that users who share an AI platform's addresses don't share a limit. Keep a
   WAF rate limit only as a high ceiling against floods.
1. **Keep OAuth discovery and sign-in reachable.** MCP clients fetch these paths
   without a token, and some of them from a browser:
   - `/.well-known/oauth-protected-resource/*`
   - `/.well-known/oauth-authorization-server` and
     `/.well-known/oauth-authorization-server/*`
   - `/__zuplo/oauth/*` (or your configured `basePath`)
   - `/__zuplo/auth/connections/*`, if a route uses
     [upstream OAuth](./auth/upstream-oauth.mdx)

   See the [MCP Gateway reference](./reference.mdx#oauth-endpoints) for the full
   list of endpoints the gateway serves.

:::caution

An exception exempts requests from the rules it skips, so anyone can send a
request that matches it. Keep the exception as narrow as the traffic allows, and
make sure the route itself enforces authentication.

:::

If your WAF or CDN sits in front of a custom domain, it must also forward the
original `Host` header or set `X-Forwarded-Host`, or the gateway advertises the
wrong issuer. See
[Custom domain → wrong issuer in AS metadata](./troubleshooting.mdx#custom-domain--wrong-issuer-in-as-metadata).

## Cloudflare

Cloudflare evaluates WAF custom rules before managed rules, so a custom rule
with the **Skip** action can exempt MCP traffic from specific managed rules,
rate limiting rules, or Super Bot Fight Mode.

1. In the Cloudflare dashboard, open **Security → Events** for your zone and
   filter by your MCP path. Note the service and the rule ID of each blocked
   request.
1. Open **Security → Security rules** (**Security → WAF** on older dashboards)
   and create a custom rule. Use an expression that matches only your MCP
   routes:

   ```txt
   (http.host eq "api.example.com"
    and starts_with(http.request.uri.path, "/mcp/")
    and http.request.method eq "POST")
   ```

1. Set the action to **Skip** and select only what the events showed: specific
   managed rules, rate limiting rules, or Super Bot Fight Mode. Turn on **Log
   matching requests**.
1. Add a second rule with the same steps for the OAuth discovery and sign-in
   paths, if any rule blocks them:

   ```txt
   (http.host eq "api.example.com"
    and (starts_with(http.request.uri.path, "/.well-known/oauth-")
     or starts_with(http.request.uri.path, "/__zuplo/")))
   ```

1. Deploy the rules and repeat the failing request. Confirm in **Security →
   Events** that the request now matches your skip rule.

A few Cloudflare settings need attention outside custom rules:

- **Bot Fight Mode** (Free plan) applies to the whole zone and a skip rule can't
  bypass it. If it challenges MCP clients, turn it off or upgrade to a plan with
  Super Bot Fight Mode, which a skip rule can bypass.
- **AI bot blocking** blocks requests from AI services. If you turned it on,
  confirm that it doesn't block the AI platforms your users connect from.
- **Challenge rules** (**Managed Challenge**, **JS Challenge**, or **Interactive
  Challenge**) never succeed for an MCP client. Exclude MCP paths from them.
- **Proxy read timeout.** Cloudflare closes a proxied connection if the origin
  sends nothing for 100 seconds. Keep slow tool calls under that limit, or
  stream progress from the server.

## AWS WAF

The AWS managed core rule set (`AWSManagedRulesCommonRuleSet`) contains the two
rules that most often block MCP traffic: `SizeRestrictions_BODY`, which blocks
request bodies larger than 8 KB, and `NoUserAgent_HEADER`, which blocks requests
without a User-Agent.

1. Open the web ACL's sampled requests or WAF logs and filter by your MCP path
   to find the matching rules.
1. In the managed rule group, override the action of each matching rule to
   **Count**. Count keeps the rule visible in metrics and logs without blocking.
1. To keep blocking on other paths, add a custom rule after the managed rule
   group that blocks requests carrying the rule's label (for example
   `awswaf:managed:aws:core-rule-set:SizeRestrictions_Body`) unless the path
   starts with your MCP prefix.

Apply the same approach to other managed rule groups, such as the SQL database
or known bad inputs rule sets, if their rules match tool arguments. Set the
limit of any rate-based rule with shared AI platform addresses in mind.

## Other WAFs

Akamai, Fastly, Azure Front Door, and other WAFs use the same building blocks
under different names: a match condition on hostname and path, a per-rule
exception or log-only mode, and bot management settings. Apply the
[principles above](#add-narrow-exceptions) with your provider's tools. For setup
details, see [Akamai](../articles/waf-ddos-akamai.md),
[Fastly](../articles/waf-ddos-fastly.mdx), and
[AWS WAF and Shield](../articles/waf-ddos-aws-waf-shield.mdx).

## Protect an upstream MCP server behind a WAF

When the upstream MCP server sits behind a WAF, the WAF sees requests from your
gateway, not from the original MCP client. Apply the same exceptions to the
upstream's MCP path, and then lock that path down so that only your gateway can
use it.

On Managed Edge, requests to the upstream come from many edge locations, so an
IP allowlist isn't practical. Use a shared secret header instead:

1. Store a random value in an
   [environment variable](../articles/environment-variables.mdx) marked as a
   secret, for example `UPSTREAM_ORIGIN_SECRET`.
1. Add a [`set-headers-inbound`](../policies/set-headers-inbound.mdx) policy to
   the MCP route that sets the secret on every request to the upstream:

   ```json title="config/policies.json"
   {
     "name": "set-upstream-origin-header",
     "policyType": "set-headers-inbound",
     "handler": {
       "export": "SetHeadersInboundPolicy",
       "module": "$import(@zuplo/runtime)",
       "options": {
         "headers": [
           {
             "name": "x-origin-secret",
             "value": "$env(UPSTREAM_ORIGIN_SECRET)"
           }
         ]
       }
     }
   }
   ```

1. In the upstream's WAF, block requests to the MCP path unless the header
   matches the secret. Redact the header from the WAF's logs.

Use a header other than `Authorization` for the secret. When a route uses
[upstream OAuth](./auth/upstream-oauth.mdx), the gateway sends the upstream
access token in `Authorization`. If the upstream runs its own OAuth server,
leave its discovery and sign-in paths public, because users' browsers reach them
directly.

For other ways to restrict an upstream to your gateway, such as mTLS or a secure
tunnel, see [Securing your backend](../articles/securing-your-backend.mdx).
Managed Dedicated deployments can also use
[custom networking](../dedicated/networking.mdx) for private connectivity.

## Verify the fix

Send an `initialize` request through the WAF with the headers an MCP client
sends:

```sh
curl -i -X POST https://api.example.com/mcp/my-server \
  -H 'Accept: application/json, text/event-stream' \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"waf-test","version":"1.0.0"}}}'
```

A route protected by an MCP OAuth policy answers with `401` and a
`WWW-Authenticate: Bearer` header. That response came from Zuplo, so the WAF let
the request through. A `403` or `503` with an HTML body means the WAF still
blocks the request. Repeat the test with a large or code-heavy request body to
check the managed rules, and then connect a real client from the AI platform
your users rely on.
