Skip to content

Commit 443e023

Browse files
committed
diag(mcp): length-gate initialize check to skip parsing large tool payloads
isInitializeRequest now rejects bodies over 4096 chars before any JSON.parse. The initialize message is a small fixed structure, so this keeps large tools/call payloads (potentially multi-MB) off the parse hot path while still detecting initialize.
1 parent d8467f4 commit 443e023

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

apps/sim/lib/mcp/http-diagnostics.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ function rawUrl(input: string | URL | Request): string {
3333
return typeof input === 'string' ? input : input instanceof URL ? input.href : input.url
3434
}
3535

36+
/**
37+
* The `initialize` request is a small fixed structure (protocolVersion, capabilities,
38+
* clientInfo). This length gate lets us skip parsing large `tools/call` payloads —
39+
* which can be multi-MB — entirely, keeping the check off the hot path.
40+
*/
41+
const MAX_INIT_BODY_CHARS = 4096
42+
3643
/**
3744
* True only when the request body is an MCP `initialize` JSON-RPC message. Used to
3845
* scope response-body logging to the initialize handshake and nothing else — token
3946
* requests (form-encoded) and tool calls (`method: 'tools/call'`) both fail this.
47+
* Large bodies are rejected by length before any parse (see {@link MAX_INIT_BODY_CHARS}).
4048
*/
4149
function isInitializeRequest(body: unknown): boolean {
42-
if (typeof body !== 'string') return false
50+
if (typeof body !== 'string' || body.length > MAX_INIT_BODY_CHARS) return false
4351
try {
4452
return (JSON.parse(body) as { method?: unknown })?.method === 'initialize'
4553
} catch {

0 commit comments

Comments
 (0)