UNPKG

@capgo/cli

Version:
20 lines (19 loc) 1.07 kB
import type { Writable } from 'node:stream'; /** * Harden a stdio MCP server against stdout pollution. * * The MCP stdio transport frames JSON-RPC over `process.stdout`. Any *other* write to * stdout — a clack `intro`/`log`, a stray `console.log`, a chatty dependency — injects * non-JSON bytes into that stream, and a strict client (e.g. Codex) then drops the * connection with "Transport closed". Per-call `silent` flags (see sdk.ts) are easy to * miss one-by-one, so this is the backstop: it routes EVERY ambient `process.stdout` * write to stderr (still visible for debugging, harmless to the protocol) and returns a * dedicated Writable — bound to the real stdout — for the transport to send JSON-RPC on. * * Call once, before constructing StdioServerTransport, and hand it the returned stream: * const out = installMcpStdoutGuard() * const transport = new StdioServerTransport(process.stdin, out) * * @returns a Writable wired to the real fd-1 stdout, for the transport only. */ export declare function installMcpStdoutGuard(): Writable;