1
0

mcp-initialize.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * MCP `initialize` handshake regression tests.
  3. *
  4. * Issue #172: on slow filesystems (Docker Desktop VirtioFS on macOS, WSL2),
  5. * the MCP server was blocking the initialize response on CodeGraph.open() and
  6. * Parser.init() (web-tree-sitter WASM bootstrap), which could take longer than
  7. * Claude Code's ~30s handshake timeout. The child process stayed alive and
  8. * had received the request, but never sent a response, so tools never
  9. * appeared in the client. The fix sends the initialize response before
  10. * kicking off the heavy init in the background. These tests guard the
  11. * contract that initialize is fast regardless of how much work init does.
  12. */
  13. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  14. import { spawn, ChildProcessWithoutNullStreams } from 'child_process';
  15. import * as fs from 'fs';
  16. import * as path from 'path';
  17. import * as os from 'os';
  18. import { CodeGraph } from '../src';
  19. const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
  20. function spawnServer(cwd: string): ChildProcessWithoutNullStreams {
  21. return spawn(process.execPath, [BIN, 'serve', '--mcp'], {
  22. cwd,
  23. stdio: ['pipe', 'pipe', 'pipe'],
  24. // Pin to direct (in-process) mode. #172 is a contract about the in-process
  25. // server's init ordering — the "File watcher active" log this test observes
  26. // is emitted in-process. In daemon mode the watcher runs in the detached
  27. // daemon (logging to .codegraph/daemon.log, not the child's stderr); the
  28. // same response-before-init guarantee lives in the shared session code and
  29. // is covered by mcp-daemon.test.ts. Direct mode also avoids leaking a
  30. // detached daemon from this suite.
  31. env: { ...process.env, CODEGRAPH_NO_DAEMON: '1' },
  32. }) as ChildProcessWithoutNullStreams;
  33. }
  34. function sendInitialize(child: ChildProcessWithoutNullStreams, projectPath: string) {
  35. const msg = JSON.stringify({
  36. jsonrpc: '2.0',
  37. id: 0,
  38. method: 'initialize',
  39. params: {
  40. protocolVersion: '2025-11-25',
  41. capabilities: {},
  42. clientInfo: { name: 'test', version: '0.0.0' },
  43. rootUri: `file://${projectPath}`,
  44. },
  45. });
  46. child.stdin.write(msg + '\n');
  47. }
  48. /**
  49. * Collect stdout lines and stderr text from the child, tagging each piece
  50. * with a monotonic sequence number. Lets us assert ordering between the
  51. * JSON-RPC response (stdout) and side-effect logs (stderr).
  52. */
  53. function tagStreams(child: ChildProcessWithoutNullStreams) {
  54. const events: Array<{ seq: number; stream: 'stdout' | 'stderr'; text: string }> = [];
  55. let seq = 0;
  56. let stdoutBuf = '';
  57. let stderrBuf = '';
  58. child.stdout.on('data', (chunk) => {
  59. stdoutBuf += chunk.toString('utf8');
  60. let idx;
  61. while ((idx = stdoutBuf.indexOf('\n')) !== -1) {
  62. const line = stdoutBuf.slice(0, idx);
  63. stdoutBuf = stdoutBuf.slice(idx + 1);
  64. events.push({ seq: seq++, stream: 'stdout', text: line });
  65. }
  66. });
  67. child.stderr.on('data', (chunk) => {
  68. stderrBuf += chunk.toString('utf8');
  69. let idx;
  70. while ((idx = stderrBuf.indexOf('\n')) !== -1) {
  71. const line = stderrBuf.slice(0, idx);
  72. stderrBuf = stderrBuf.slice(idx + 1);
  73. events.push({ seq: seq++, stream: 'stderr', text: line });
  74. }
  75. });
  76. return events;
  77. }
  78. function waitFor<T>(
  79. events: ReadonlyArray<{ seq: number; stream: string; text: string }>,
  80. predicate: (e: { seq: number; stream: string; text: string }) => boolean,
  81. timeoutMs: number,
  82. ): Promise<{ seq: number; stream: string; text: string }> {
  83. return new Promise((resolve, reject) => {
  84. const started = Date.now();
  85. const tick = () => {
  86. const hit = events.find(predicate);
  87. if (hit) return resolve(hit);
  88. if (Date.now() - started > timeoutMs) {
  89. return reject(new Error(`Timed out waiting for predicate. Events: ${JSON.stringify(events)}`));
  90. }
  91. setTimeout(tick, 20);
  92. };
  93. tick();
  94. });
  95. }
  96. describe('MCP initialize handshake (issue #172)', () => {
  97. let tempDir: string;
  98. let child: ChildProcessWithoutNullStreams | null = null;
  99. beforeEach(() => {
  100. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-mcp-init-'));
  101. });
  102. afterEach(() => {
  103. if (child && !child.killed) {
  104. child.kill('SIGKILL');
  105. child = null;
  106. }
  107. fs.rmSync(tempDir, { recursive: true, force: true });
  108. });
  109. it('responds to initialize quickly when no .codegraph exists in cwd', async () => {
  110. child = spawnServer(tempDir);
  111. const events = tagStreams(child);
  112. sendInitialize(child, tempDir);
  113. const response = await waitFor(events, (e) => e.stream === 'stdout', 5000);
  114. const json = JSON.parse(response.text);
  115. expect(json.jsonrpc).toBe('2.0');
  116. expect(json.id).toBe(0);
  117. expect(json.result.protocolVersion).toBeDefined();
  118. expect(json.result.capabilities.tools).toBeDefined();
  119. }, 10000);
  120. it('sends initialize response BEFORE tryInitializeDefault finishes', async () => {
  121. // Seed a real .codegraph so the server's tryInitializeDefault path runs
  122. // its full body: CodeGraph.open() (which awaits initGrammars()) and then
  123. // startWatching() (which logs "File watcher active" to stderr). On any
  124. // platform, that stderr log is observable evidence that tryInitializeDefault
  125. // has completed. The contract we're protecting: the JSON-RPC response on
  126. // stdout must arrive BEFORE that stderr log. If a future change re-awaits
  127. // tryInitializeDefault before sendResult, this ordering inverts and the
  128. // test fails — regardless of how fast the local filesystem is.
  129. const cg = await CodeGraph.init(tempDir);
  130. cg.close();
  131. child = spawnServer(tempDir);
  132. const events = tagStreams(child);
  133. sendInitialize(child, tempDir);
  134. const response = await waitFor(events, (e) => e.stream === 'stdout', 10000);
  135. const watcherLog = await waitFor(
  136. events,
  137. (e) => e.stream === 'stderr' && e.text.includes('File watcher active'),
  138. 10000,
  139. );
  140. expect(response.seq).toBeLessThan(watcherLog.seq);
  141. const json = JSON.parse(response.text);
  142. expect(json.id).toBe(0);
  143. expect(json.result.serverInfo.name).toBe('codegraph');
  144. }, 20000);
  145. it('answers resources/list and prompts/list with empty lists, not -32601 (issue #621)', async () => {
  146. child = spawnServer(tempDir);
  147. const events = tagStreams(child);
  148. sendInitialize(child, tempDir);
  149. await waitFor(events, (e) => e.stream === 'stdout', 5000); // initialize reply
  150. child.stdin.write(JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'resources/list', params: {} }) + '\n');
  151. child.stdin.write(JSON.stringify({ jsonrpc: '2.0', id: 2, method: 'prompts/list', params: {} }) + '\n');
  152. const replyFor = async (id: number) => {
  153. const ev = await waitFor(events, (e) => {
  154. if (e.stream !== 'stdout') return false;
  155. try { return JSON.parse(e.text).id === id; } catch { return false; }
  156. }, 5000);
  157. return JSON.parse(ev.text);
  158. };
  159. const resources = await replyFor(1);
  160. expect(resources.error).toBeUndefined();
  161. expect(resources.result.resources).toEqual([]);
  162. const prompts = await replyFor(2);
  163. expect(prompts.error).toBeUndefined();
  164. expect(prompts.result.prompts).toEqual([]);
  165. }, 15000);
  166. });