* Execute a tool by name. * * `sessionState` is the CALLER's per-session explore history (CG-17). The * daemon shares one ToolHandler across every connected session, so this state * cannot live on the handler — each session owns one and hands it in, which is * what keeps two sessions
(
toolName: string,
args: Record<string, unknown>,
sessionState?: ExploreSessionState,
indexVersionWarnings?: IndexVersionWarningState,
opts: ExecuteOptions = {},
)
| 3100 | * Omit it (the CLI does) and explore behaves exactly as before, untracked. |
| 3101 | */ |
| 3102 | async execute( |
| 3103 | toolName: string, |
| 3104 | args: Record<string, unknown>, |
| 3105 | sessionState?: ExploreSessionState, |
| 3106 | indexVersionWarnings?: IndexVersionWarningState, |
| 3107 | opts: ExecuteOptions = {}, |
| 3108 | ): Promise<ToolResult> { |
| 3109 | if (this.closing) { |
| 3110 | return opts.startNow ? NOT_STARTED : this.textResult('This MCP session is closing; retry with a connected session.'); |
| 3111 | } |
| 3112 | this.activeCalls++; |
| 3113 | try { |
| 3114 | // Block the first tool call on the engine's post-open reconcile so we |
| 3115 | // never serve rows for files deleted/edited while no MCP server was |
| 3116 | // running. The wait is time-boxed (#905): a huge-repo reconcile takes |
| 3117 | // minutes, and blocking the first call on all of it reads as a hang, so |
| 3118 | // we wait briefly then serve and let it finish in the background. The |
| 3119 | // gate stays installed until reconciliation settles, including on timeout. |
| 3120 | // Catch-up failures are logged by the engine; we proceed regardless so a |
| 3121 | // transient sync error never breaks tools. |
| 3122 | if (this.catchUpGate) { |
| 3123 | if (opts.startNow) return NOT_STARTED; |
| 3124 | const gate = this.catchUpGate; |
| 3125 | await this.awaitCatchUpGate(gate); |
| 3126 | } |
| 3127 | // Honor the optional tool allowlist (CODEGRAPH_MCP_TOOLS): a trimmed |
| 3128 | // surface rejects ablated tools defensively even if a client cached them. |
| 3129 | if (!this.isToolAllowed(toolName)) { |
| 3130 | return this.errorResult(`Tool ${toolName} is disabled via CODEGRAPH_MCP_TOOLS`); |
| 3131 | } |
| 3132 | // Cross-cutting input validation. All tools accept an optional |
| 3133 | // `projectPath` and most accept either `query`, `task`, or |
| 3134 | // `symbol` — bound their lengths centrally so individual handlers |
| 3135 | // can stay focused on tool-specific logic. |
| 3136 | const pathCheck = this.validateOptionalPath(args.projectPath, 'projectPath'); |
| 3137 | if (typeof pathCheck === 'object' && pathCheck !== undefined) { |
| 3138 | return pathCheck; |
| 3139 | } |
| 3140 | // An explicit project gets the same first-call guarantee as the default |
| 3141 | // (#1835): its post-open catch-up sync finishes (time-boxed) before we |
| 3142 | // serve it. Resolved on the main thread so the watcher lives here even |
| 3143 | // when dispatch is off-loaded to a worker. |
| 3144 | if (typeof pathCheck === 'string') { |
| 3145 | // A file inside a project (an agent passing the file it is looking at) |
| 3146 | // stands for its directory, like any other path inside it (#2982). |
| 3147 | // Rewritten here so the gate, the worker, the worktree notice and |
| 3148 | // getCodeGraph all see the same directory. |
| 3149 | let projectPath = pathCheck; |
| 3150 | try { |
| 3151 | if (statSync(pathCheck).isFile()) projectPath = dirname(pathCheck); |
| 3152 | } catch { /* a missing path walks up to its .codegraph/ as before */ } |
| 3153 | if (projectPath !== pathCheck) args = { ...args, projectPath }; |
| 3154 | await this.awaitProjectGate(projectPath); |
| 3155 | } |
| 3156 | // The `path` and `pattern` properties used by codegraph_files are |
| 3157 | // also path-shaped — apply the same cap. |
| 3158 | if (args.path !== undefined) { |
| 3159 | const check = this.validateOptionalPath(args.path, 'path'); |