What is Regex Validation (Policy)?

2 min read Updated

Using regular expressions in YAML policies to validate tool call argument values before they reach the MCP server. Regex constraints can enforce email formats, branch naming conventions, file path restrictions, SQL injection prevention patterns, and more.

WHY IT MATTERS

Regular expressions are the most flexible argument validation primitive. While enums work for closed sets and ranges work for numbers, regex handles everything in between — email formats, file paths, URL patterns, naming conventions, and forbidden string patterns.

In MCP tool call governance, regex validation is critical for catching dangerous inputs. A file system server's write_file tool might accept any path — but a regex constraint like ^/home/agent/workspace/ ensures the agent can only write to its designated directory. A database tool might accept any SQL — but a regex blocking DROP|TRUNCATE|DELETE prevents destructive operations.

The power of regex comes with responsibility. Overly complex patterns can cause performance issues (catastrophic backtracking) and overly strict patterns can break legitimate use cases. Policies should use the simplest effective pattern and document what the regex intends to match.

HOW POLICYLAYER USES THIS

Intercept supports regex patterns as argument constraints in YAML policies. The pattern field accepts standard regular expressions that are evaluated against argument values at policy evaluation time. Patterns are compiled once at policy load and evaluated efficiently on each tool call. Failed regex matches produce clear denial messages indicating which argument violated which pattern.

FREQUENTLY ASKED QUESTIONS

What regex flavour does Intercept use?
Intercept uses Go's RE2 regular expression engine, which guarantees linear-time matching and prevents catastrophic backtracking — important for a security-critical proxy.
Can I use regex to block dangerous inputs?
Yes. Negative patterns (denying calls where an argument matches a dangerous pattern) are common — for example, blocking SQL arguments containing DROP or TRUNCATE keywords.
Is regex validation case-sensitive?
By default, yes. You can use the (?i) flag in the regex pattern to make matching case-insensitive where needed.

FURTHER READING

Enforce policies on every tool call

Intercept is the open-source MCP proxy that enforces YAML policies on AI agent tool calls. No code changes needed.

npx -y @policylayer/intercept
github.com/policylayer/intercept →
// GET IN TOUCH

Have a question or want to learn more? Send us a message.

Message sent.

We'll get back to you soon.