Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/improvement/mcp-serve-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ export function mcpServeVerifier(spec: McpServeSpec): Verifier {
const failCandidate = (msg: string) =>
settle(() => resolve({ ok: false, feedback: withStderr(msg) }))
const setupFault = (err: Error) => settle(() => reject(err))
const failStdin = (err: Error) =>
failCandidate(`writing to MCP server stdin failed: ${err.message}`)

const send = (msg: Record<string, unknown>): boolean => {
try {
child.stdin.write(`${JSON.stringify(msg)}\n`)
return true
} catch (err) {
// EPIPE: the server died mid-handshake — a failed candidate, not a fault.
failCandidate(`writing to MCP server stdin failed: ${(err as Error).message}`)
failStdin(err as Error)
return false
}
}
Expand All @@ -100,6 +102,8 @@ export function mcpServeVerifier(spec: McpServeSpec): Verifier {
// server crashed on boot); after we settle, our own SIGKILL fires here.
failCandidate(`MCP server exited (code ${code}, signal ${signal}) before serving`)
})
// Pipe failures normally arrive asynchronously, outside send()'s try/catch.
child.stdin.on('error', failStdin)
child.stderr.on('data', (d) => stderr.push(String(d)))

const rl = createInterface({ input: child.stdout })
Expand Down
27 changes: 25 additions & 2 deletions tests/mcp-serve-verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@ import { mcpServeVerifier } from '../src/improvement/mcp-serve-verifier'

// A minimal stdio MCP server (newline-delimited JSON-RPC 2.0) used as the
// "built server" the verifier boots. Variants exercise each outcome.
function serverScript(variant: 'ok' | 'no-tools' | 'crash' | 'hang'): string {
function serverScript(variant: 'ok' | 'no-tools' | 'crash' | 'hang' | 'closed-stdin'): string {
if (variant === 'crash') return 'process.exit(1)\n'
if (variant === 'hang') return 'setInterval(() => {}, 1000)\n' // never answers
if (variant === 'closed-stdin') {
return [
'import { closeSync } from "node:fs"',
'process.stdin.on("error", () => {})',
'process.stdin.once("data", (chunk) => {',
' const request = JSON.parse(String(chunk).trim())',
' closeSync(0)',
' process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: request.id, result: { protocolVersion: "2024-11-05", capabilities: {}, serverInfo: { name: "fake", version: "0" } } }) + "\\n")',
'})',
'setInterval(() => {}, 1000)',
].join('\n')
}
const tools =
variant === 'no-tools'
? '[]'
Expand All @@ -31,7 +43,7 @@ beforeEach(() => {
})
afterEach(() => rmSync(dir, { recursive: true, force: true }))

function write(variant: 'ok' | 'no-tools' | 'crash' | 'hang'): string {
function write(variant: 'ok' | 'no-tools' | 'crash' | 'hang' | 'closed-stdin'): string {
const path = join(dir, `server-${variant}.mjs`)
writeFileSync(path, serverScript(variant))
return path
Expand All @@ -57,6 +69,17 @@ describe('mcpServeVerifier — boot-and-probe', () => {
expect(res.feedback).toMatch(/exited|serving/)
})

it('fails when the server closes stdin during the handshake', async () => {
const verify = mcpServeVerifier({
command: 'node',
args: [write('closed-stdin')],
timeoutMs: 800,
})
const res = await verify(dir)
expect(res.ok).toBe(false)
expect(res.feedback).toContain('writing to MCP server stdin failed')
})

it('fails (does not hang) a server that never answers, via timeout', async () => {
const verify = mcpServeVerifier({ command: 'node', args: [write('hang')], timeoutMs: 800 })
const res = await verify(dir)
Expand Down
Loading