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

Method handleInitialize

src/mcp/session.ts:293–356  ·  view source on GitHub ↗
(request: JsonRpcRequest)

Source from the content-addressed store, hash-verified

291 }
292
293 private async handleInitialize(request: JsonRpcRequest): Promise<void> {
294 const params = request.params as {
295 rootUri?: string;
296 workspaceFolders?: Array<{ uri: string; name: string }>;
297 capabilities?: { roots?: unknown };
298 clientInfo?: { name?: unknown; version?: unknown };
299 } | undefined;
300
301 this.clientSupportsRoots = !!params?.capabilities?.roots;
302 if (params?.clientInfo) {
303 this.clientInfo = {
304 name: typeof params.clientInfo.name === 'string' ? params.clientInfo.name : undefined,
305 version: typeof params.clientInfo.version === 'string' ? params.clientInfo.version : undefined,
306 };
307 }
308
309 // Explicit project signal, strongest first: client-provided rootUri /
310 // workspaceFolders (LSP-style), else the --path the server was launched
311 // with. cwd is NOT used here — we defer it so a roots/list answer can
312 // win over it. See issue #196.
313 let explicitPath: string | null = null;
314 if (params?.rootUri) {
315 explicitPath = fileUriToPath(params.rootUri);
316 } else if (params?.workspaceFolders?.[0]?.uri) {
317 explicitPath = fileUriToPath(params.workspaceFolders[0].uri);
318 } else if (this.explicitProjectPath) {
319 explicitPath = this.explicitProjectPath;
320 }
321
322 // Pick the instructions variant by the root's index state — synchronous
323 // and bounded (an existsSync walk-up plus, when that misses, the depth- and
324 // count-bounded workspace down-scan; no DB open, so the #172 respond-fast
325 // contract holds). This is the SAME resolution the engine's doInitialize
326 // runs (#1606), so the variant matches what the engine will actually adopt
327 // — a workspace whose single indexed sub-project becomes the default gets
328 // the full single-project playbook, race-free by construction (both sides
329 // compute it independently; no ordering between handshake and engine init
330 // is assumed). When the root ISN'T indexed (and nothing was adopted), send
331 // the per-project variant (tools are still exposed — see handleToolsList):
332 // it tells the agent there is no default project and to pass `projectPath`
333 // to any project that has a `.codegraph/`. Gating tool AVAILABILITY on
334 // whether `./` is indexed was the #964 bug — it broke monorepos (only
335 // sub-projects indexed) and never surfaced the tools after a mid-session
336 // `codegraph init`. When no explicit path is known yet (roots/list dance
337 // pending), cwd is the best predictor of where the default will resolve.
338 // A root in a sensitive folder resolves to null here as in the engine, so
339 // that session gets the no-default-project variant too.
340 const indexed = resolveServerRoot(explicitPath ?? process.cwd()).root !== null;
341
342 // Respond to the handshake BEFORE doing any heavy init — see issue #172.
343 this.transport.sendResult(request.id, {
344 protocolVersion: PROTOCOL_VERSION,
345 capabilities: { tools: {} },
346 serverInfo: SERVER_INFO,
347 instructions: instructionsForClient(indexed ? SERVER_INSTRUCTIONS : SERVER_INSTRUCTIONS_NO_ROOT_INDEX, this.clientInfo?.name),
348 });
349
350 if (explicitPath) {

Callers 1

handleMessageMethod · 0.95

Calls 5

resolveServerRootFunction · 0.90
fileUriToPathFunction · 0.85
instructionsForClientFunction · 0.85
sendResultMethod · 0.80
ensureInitializedMethod · 0.80

Tested by

no test coverage detected