MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / proxyHandshake

Function proxyHandshake

__tests__/mcp-opencode-tool-names.test.ts:197–243  ·  view source on GitHub ↗

The real proxy, with no daemon: it answers initialize and tools/list itself.

(clientInfo: object)

Source from the content-addressed store, hash-verified

195describe('the proxy\'s local handshake says what the session says (#1267)', () => {
196 /** The real proxy, with no daemon: it answers initialize and tools/list itself. */
197 async function proxyHandshake(clientInfo: object): Promise<{ init: any; list: any }> {
198 const proxyModule = path.resolve(__dirname, '../dist/mcp/proxy.js');
199 const script = `
200 const { runLocalHandshakeProxy } = require(${JSON.stringify(proxyModule)});
201 runLocalHandshakeProxy({
202 root: process.cwd(),
203 getDaemonSocket: async () => null,
204 makeEngine: () => { throw new Error('no tool call is made'); },
205 });
206 `;
207 const child = spawn(process.execPath, ['-e', script], {
208 cwd: indexedDir,
209 env: {
210 ...process.env,
211 CODEGRAPH_TELEMETRY: '0', DO_NOT_TRACK: '1', CODEGRAPH_NO_UPDATE_CHECK: '1', CODEGRAPH_NO_PROMPT_HOOK: '1',
212 CODEGRAPH_NO_WATCHDOG: '1', CODEGRAPH_PPID_POLL_MS: '0',
213 CODEGRAPH_STARTUP_HANDSHAKE_TIMEOUT_MS: '0', CODEGRAPH_DAEMON_RETRY_MS: '60000',
214 CODEGRAPH_MCP_TOOLS: ALL_TOOLS,
215 },
216 stdio: ['pipe', 'pipe', 'pipe'],
217 });
218 let out = '';
219 let err = '';
220 child.stdout!.on('data', (d) => { out += d; });
221 child.stderr!.on('data', (d) => { err += d; });
222 const exited = new Promise<void>((resolve) => child.once('exit', () => resolve()));
223 const send = (msg: object) => child.stdin!.write(JSON.stringify({ jsonrpc: '2.0', ...msg }) + '\n');
224 const reply = (id: number) =>
225 out.split('\n').filter((l) => l.startsWith('{')).map((l) => JSON.parse(l)).find((m) => m.id === id);
226 const until = async (check: () => boolean) => {
227 const deadline = Date.now() + 15_000;
228 while (!check()) {
229 if (Date.now() > deadline) throw new Error(`no reply: ${out}\n${err}`);
230 await new Promise((r) => setTimeout(r, 25));
231 }
232 };
233 try {
234 send({ id: 1, method: 'initialize', params: initializeParams(clientInfo, indexedDir) });
235 send({ id: 2, method: 'tools/list', params: {} });
236 await until(() => !!reply(1) && !!reply(2));
237 return { init: reply(1).result, list: reply(2).result };
238 } finally {
239 child.stdin!.end();
240 child.kill('SIGKILL');
241 await exited;
242 }
243 }
244
245 it('gives an OpenCode client, and any other, the same initialize and tools/list answers as the session', async () => {
246 process.env.CODEGRAPH_MCP_TOOLS = ALL_TOOLS;

Callers 1

Calls 8

spawnFunction · 0.85
resolveFunction · 0.70
sendFunction · 0.70
initializeParamsFunction · 0.70
untilFunction · 0.70
replyFunction · 0.70
resolveMethod · 0.65
onMethod · 0.65

Tested by

no test coverage detected