Skip to content
Help guide the future of agentgateway — Take the survey

Set up CEL guardrails

Verified : Automated tests passed for: Configuration files: The configuration files on this page were checked against their schema. End-to-end run: Every step ran in a fresh test environment and the results were checked.
Page as Markdown

Reject and mask MCP tool calls in-process with CEL expression guardrails.

Reject and mask Model Context Protocol (MCP) tool calls with Common Expression Language (CEL) expressions inside agentgateway. CEL guardrails require no external policy server.

For an overview, see Guardrail processors. To use an external policy server, see Set up remote guardrails.

In this guide, you configure three CEL guardrails for the echo tool of a sample MCP server:

  • Reject messages containing DO_NOT_SEND in the request phase.
  • Mask email addresses before sending the request to the MCP server.
  • Mask demo secrets in the response before returning it to the client.

Before you begin

Install the agentgateway binary.

Set up CEL guardrails

You can configure CEL guardrails in the UI or in your configuration file.

The following steps add the same three processors as the Config file tab. The UI saves them in the top-level mcp.policies.mcpGuardrails policy, which applies to all MCP traffic.

  1. Create a config.yaml file with a local stdio MCP server, but no guardrails. You add the guardrails in the UI.

    # yaml-language-server: $schema=https://agentgateway.dev/schema/config
    gateways:
      default:
        port: 3000
    mcp:
      targets:
      - name: everything
        stdio:
          cmd: npx
          args: ["@modelcontextprotocol/server-everything"]
  2. Run agentgateway with the configuration file.

    agentgateway -f config.yaml
  3. Open http://localhost:15000/ui/mcp/policies (MCP > Policies), and click the MCP guardrails tile.

  4. Add the processor that rejects messages with DO_NOT_SEND. Add it first, so that it sees the original message before the masking processor changes it.

    1. Click Add processor.
    2. From the Type list, select Expression.
    3. In the Condition editor, enter the following expression.
      mcp.params.name == 'echo' && has(mcp.params.arguments) && has(mcp.params.arguments.message) && mcp.params.arguments.message.contains('DO_NOT_SEND')
    4. Keep the Reject action, and in Reject message, enter Messages containing DO_NOT_SEND may not be sent upstream.
    5. In Method phases, keep the tools/call method with the Request phase.
    MCP guardrails drawer with processor 1, an Expression processor with the DO_NOT_SEND condition, the Reject action, and the reject message
    MCP guardrails drawer with processor 1, an Expression processor with the DO_NOT_SEND condition, the Reject action, and the reject message
  5. Add the processor that masks email addresses in the request.

    1. Click Add processor, and from the Type list, select Expression.
    2. In the Condition editor, enter the following expression.
      mcp.params.name == 'echo' && has(mcp.params.arguments) && has(mcp.params.arguments.message)
    3. From the Action list, select Transform.
    4. In the Transform editor, enter the following expression.
      mcp.params.with(p, p.merge({
        'arguments': p.arguments.merge({
          'message': p.arguments.message.regexReplaceAll(
            '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+[.][A-Za-z]{2,}',
            '[EMAIL]'
          )
        })
      }))
    5. In Method phases, keep the tools/call method with the Request phase.
  6. Add the processor that masks demo secrets in the response.

    1. Click Add processor, and from the Type list, select Expression.
    2. In the Condition editor, enter has(mcp.result.content).
    3. From the Action list, select Transform.
    4. In the Transform editor, enter the following expression.
      mcp.result.with(r, r.merge({
        'content': r.content.map(item,
          item.type == 'text'
            ? item.merge({'text': item.text.regexReplaceAll(
                'demo-secret-[A-Za-z0-9]+', '[SECRET]'
              )})
            : item
        )
      }))
    5. In Method phases, enter tools/call, select the Response phase, and click Add. The tools/call method now runs in the Response phase.
    Processor 3 in the MCP guardrails drawer with the Transform action, the expression that masks demo secrets, and the tools/call method in the Response phase
    Processor 3 in the MCP guardrails drawer with the Transform action, the expression that masks demo secrets, and the tools/call method in the Response phase
  7. Optional: To review the generated YAML before you save, click View diff.

  8. Click Save policy.

Whether the UI can save depends on your storage mode. For more information, see Save changes from the UI.

Verify the guardrails

  1. Initialize an MCP session and save the session ID.

    export MCP_SESSION_ID=$(curl -s -D - http://localhost:3000/mcp \
      -H "Content-Type: application/json" \
      -H "Accept: application/json, text/event-stream" \
      -H "MCP-Protocol-Version: 2025-03-26" \
      -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"curl","version":"1.0.0"}}}' \
      | grep -i "mcp-session-id:" | sed 's/.*: //' | tr -d '\r')
    echo $MCP_SESSION_ID
  2. Send notifications/initialized to complete the MCP handshake before calling tools.

    curl -s http://localhost:3000/mcp \
      -H "Content-Type: application/json" \
      -H "Accept: application/json, text/event-stream" \
      -H "MCP-Protocol-Version: 2025-03-26" \
      -H "mcp-session-id: $MCP_SESSION_ID" \
      -d '{"jsonrpc":"2.0","method":"notifications/initialized"}'
  3. Call the echo tool with a message that contains two email addresses and two demo secrets.

    curl -s http://localhost:3000/mcp \
      -H "Content-Type: application/json" \
      -H "Accept: application/json, text/event-stream" \
      -H "MCP-Protocol-Version: 2025-03-26" \
      -H "mcp-session-id: $MCP_SESSION_ID" \
      -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo","arguments":{"message":"Contact alice@example.com and bob@example.org; tokens demo-secret-abc and demo-secret-xyz"}}}'

    Verify that the result masks both email addresses and both secrets. The request guardrail masks the addresses; the response guardrail masks the secrets. This response uses server-sent events (Content-Type: text/event-stream).

    event: message
    data: {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"Echo: Contact [EMAIL] and [EMAIL]; tokens [SECRET] and [SECRET]"}]}}
    
  4. Call the echo tool with a message that contains DO_NOT_SEND.

    curl -s http://localhost:3000/mcp \
      -H "Content-Type: application/json" \
      -H "Accept: application/json, text/event-stream" \
      -H "MCP-Protocol-Version: 2025-03-26" \
      -H "mcp-session-id: $MCP_SESSION_ID" \
      -d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{"message":"DO_NOT_SEND alice@example.com"}}}'

    Verify that the call is rejected before it reaches the MCP server. The tool result contains isError: true and the configured rejection message. Agentgateway returns this request-phase rejection as plain JSON (Content-Type: application/json) with HTTP 200.

    {"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"Messages containing DO_NOT_SEND may not be sent upstream"}],"isError":true}}
    

Cleanup

You can remove the resources that you created in this guide.
  1. Stop the agentgateway process with Ctrl+C.
  2. Delete the configuration file.
    rm config.yaml
Was this page helpful?
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate ↵ select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.