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.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_SENDin 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 theagentgateway 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.
Create a
config.yamlfile 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"]Run agentgateway with the configuration file.
agentgateway -f config.yamlOpen http://localhost:15000/ui/mcp/policies (MCP > Policies), and click the MCP guardrails tile.
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.- Click Add processor.
- From the Type list, select Expression.
- 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') - Keep the Reject action, and in Reject message, enter
Messages containing DO_NOT_SEND may not be sent upstream. - In Method phases, keep the
tools/callmethod with the Request phase.


Add the processor that masks email addresses in the request.
- Click Add processor, and from the Type list, select Expression.
- In the Condition editor, enter the following expression.
mcp.params.name == 'echo' && has(mcp.params.arguments) && has(mcp.params.arguments.message) - From the Action list, select Transform.
- 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]' ) }) })) - In Method phases, keep the
tools/callmethod with the Request phase.
Add the processor that masks demo secrets in the response.
- Click Add processor, and from the Type list, select Expression.
- In the Condition editor, enter
has(mcp.result.content). - From the Action list, select Transform.
- 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 ) })) - In Method phases, enter
tools/call, select the Response phase, and click Add. Thetools/callmethod now runs in the Response phase.


Optional: To review the generated YAML before you save, click View diff.
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
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_IDSend
notifications/initializedto 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"}'Call the
echotool 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]"}]}}Call the
echotool with a message that containsDO_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: trueand 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.- Stop the agentgateway process with
Ctrl+C. - Delete the configuration file.
rm config.yaml