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
27 changes: 27 additions & 0 deletions src/term-commands/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ async function driveMcp(cwd: string, requests: Record<string, unknown>[]): Promi
.map((l) => JSON.parse(l) as RpcResponse);
}

/** Like driveMcp but sends raw (already-serialized) lines — for malformed input. */
async function driveMcpRaw(cwd: string, rawLines: string[]): Promise<RpcResponse[]> {
const proc = Bun.spawn(['bun', GENIE, 'mcp'], {
cwd,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
env: { ...process.env, NO_COLOR: '1', GENIE_TEST_SKIP_PGSERVE: '1' },
});
proc.stdin.write(`${rawLines.join('\n')}\n`);
await proc.stdin.end();
const stdout = await new Response(proc.stdout).text();
await proc.exited;
return stdout
.split('\n')
.filter((l) => l.trim().length > 0)
.map((l) => JSON.parse(l) as RpcResponse);
}

const INIT = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -108,6 +127,14 @@ function seed(cwd: string): { taskId: string } {
// ============================================================================

describe('mcp handshake', () => {
test('a bare `null` / primitive line is dropped and does not crash the server', async () => {
// `JSON.parse('null')` is valid JSON but not a JSON-RPC object; without the
// non-object guard, dispatch(null) throws on null.id and the server crashes.
const res = await driveMcpRaw(repo, ['null', '5', 'true', '"str"', JSON.stringify(INIT)]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add an empty array [] to the list of raw malformed lines to verify that the server also safely drops arrays without crashing or processing them.

Suggested change
const res = await driveMcpRaw(repo, ['null', '5', 'true', '"str"', JSON.stringify(INIT)]);
const res = await driveMcpRaw(repo, ['null', '5', 'true', '"str"', '[]', JSON.stringify(INIT)]);

// The malformed lines are silently dropped; the server survives + answers initialize.
expect(res.some((r) => r.id === 1 && (r.result as { serverInfo?: unknown })?.serverInfo)).toBe(true);
});

test('initialize replies with protocolVersion, tools capability, and serverInfo', async () => {
const [res] = await driveMcp(repo, [INIT]);
expect(res.id).toBe(1);
Expand Down
5 changes: 5 additions & 0 deletions src/term-commands/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export async function runMcpServer(): Promise<void> {
// Unparseable line → cannot attribute an id; drop it (no id to reply to).
return;
}
// A JSON-RPC request must be a non-null object. `JSON.parse('null')` and bare
// primitives are valid JSON but carry no id to attribute — drop them like an
// unparseable line. Without this, `dispatch(null)` (and the catch below) throw
// on `null.id`, and the uncaught error crashes the server on a `null` line.
if (req === null || typeof req !== 'object') return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While typeof req === 'object' correctly filters out primitives and null, it does not filter out arrays (e.g., [] or a JSON-RPC batch request). In JavaScript, typeof [] is 'object'. If an array is passed, it will proceed to dispatch(req), where req.id and req.method will be undefined, causing it to be treated as a notification and silently ignored. Since this server does not support batch requests, we should explicitly reject arrays using Array.isArray(req) to ensure only actual non-null objects are processed.

Suggested change
if (req === null || typeof req !== 'object') return;
if (req === null || typeof req !== 'object' || Array.isArray(req)) return;

try {
dispatch(req);
} catch (e) {
Expand Down