Heuristic:1mcp app Agent Schema Validation Sandbox Limits
| Knowledge Sources | |
|---|---|
| Domains | Security, Optimization |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
MCP tool schemas are validated in isolated worker processes with strict byte/node/depth budgets to prevent hostile schemas from exhausting the gateway's resources.
Description
The `SchemaBoundary` class runs JSON Schema compilation and evaluation in separate Node.js worker threads with hard resource limits. The `SCHEMA_LIMITS` object defines frozen budgets: schema size (256KB), tool schema size (512KB), input payload (1MB), output payload (4MB), maximum depth (64), node count (10K for schemas, 100K for instances), and worker heap (128MB). If a schema or payload exceeds any budget, a `SchemaBoundaryError` with code `schema_budget_exceeded` is thrown immediately, without attempting compilation. Worker evaluation has time limits: 250ms for compilation, 50ms for evaluation, and 5000ms for shutdown. A queue limit of 128 pending jobs prevents unbounded backlog.
Usage
Use this heuristic when:
- Diagnosing schema_budget_exceeded errors from MCP servers with large or deeply nested tool schemas
- Understanding why a tool schema is rejected even though it is valid JSON Schema
- Tuning for servers with complex schemas (may need server-side schema simplification)
- Debugging schema_evaluation_timeout or schema_evaluation_unavailable errors
The Insight (Rule of Thumb)
- Action: No direct configuration — these limits are frozen constants in `SCHEMA_LIMITS`.
- Key limits:
- `schemaBytes`: 256 KB (general schema)
- `toolSchemaBytes`: 512 KB (tool-specific schema)
- `inputBytes`: 1 MB (input payload)
- `outputBytes`: 4 MB (output payload)
- `depth`: 64
- `schemaNodes`: 10,000
- `instanceNodes`: 100,000
- `arrayEntries`: 10,000
- `compileMs`: 250 (schema compilation timeout)
- `evaluateMs`: 50 (schema evaluation timeout)
- `queue`: 128 (max pending validation jobs)
- `heapMb`: 128 (worker heap limit)
- `shutdownMs`: 5000 (worker shutdown deadline)
- Trade-off: Strict limits protect the gateway from resource exhaustion but may reject legitimate large schemas. Servers with complex tool schemas should simplify or split them.
- Error codes: `schema_budget_exceeded`, `schema_compile_failed`, `schema_evaluation_timeout`, `schema_evaluation_unavailable`, `schema_invalid`, `schema_input_invalid`, `schema_output_invalid`
Reasoning
MCP servers are external processes that may be compromised, buggy, or adversarial. Their tool schemas are untrusted input to the gateway. Without sandboxed validation:
- A deeply nested schema could cause stack overflow
- A huge schema could exhaust memory
- A pathological regex could cause ReDoS (regular expression denial of service)
- A schema with circular references could create infinite loops during compilation
The worker-thread isolation ensures that even if a schema causes a crash or hang, the main gateway process is unaffected. The time limits (250ms compile, 50ms evaluate) ensure that validation latency stays bounded. The queue limit (128) prevents a flood of validation requests from consuming all worker slots.
Code Evidence
Frozen limits from `src/core/validation/schemaPolicy.ts:4-27`:
export const SCHEMA_LIMITS = Object.freeze({
schemaBytes: 256 * 1024,
toolSchemaBytes: 512 * 1024,
depth: 64,
schemaNodes: 10_000,
properties: 1024,
required: 1024,
references: 256,
branches: 1024,
regexes: 128,
regexBytes: 1024,
enumValues: 1024,
inputBytes: 1024 * 1024,
outputBytes: 4 * 1024 * 1024,
instanceNodes: 100_000,
arrayEntries: 10_000,
instanceProperties: 10_000,
compileMs: 250,
evaluateMs: 50,
queue: 128,
heapMb: 128,
stackMb: 4,
shutdownMs: 5000,
});
Queue rejection from `src/core/validation/schemaBoundary.ts:138-139`:
if (this.closing || signal?.aborted || this.queue.length >= SCHEMA_LIMITS.queue)
return Promise.reject(new SchemaBoundaryError('schema_evaluation_unavailable', true));
Worker resource limits from `src/core/validation/schemaBoundary.ts:206`:
resourceLimits: { maxOldGenerationSizeMb: SCHEMA_LIMITS.heapMb, stackSizeMb: SCHEMA_LIMITS.stackMb },
Budget check in capture from `src/core/validation/schemaPolicy.ts:67-68`:
if (++nodes > (schema ? SCHEMA_LIMITS.schemaNodes : SCHEMA_LIMITS.instanceNodes) || depth > SCHEMA_LIMITS.depth)
fail();