* 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 = {},
)
| 3217 | * Omit it (the CLI does) and explore behaves exactly as before, untracked. |
| 3218 | */ |
| 3219 | async execute( |
| 3220 | toolName: string, |
| 3221 | args: Record<string, unknown>, |
| 3222 | sessionState?: ExploreSessionState, |
| 3223 | indexVersionWarnings?: IndexVersionWarningState, |
| 3224 | opts: ExecuteOptions = {}, |
| 3225 | ): Promise<ToolResult> { |
| 3226 | if (this.closing) { |
| 3227 | return opts.startNow ? NOT_STARTED : this.textResult('This MCP session is closing; retry with a connected session.'); |
| 3228 | } |
| 3229 | this.activeCalls++; |
| 3230 | try { |
| 3231 | // Block the first tool call on the engine's post-open reconcile so we |
| 3232 | // never serve rows for files deleted/edited while no MCP server was |
| 3233 | // running. The wait is time-boxed (#905): a huge-repo reconcile takes |
| 3234 | // minutes, and blocking the first call on all of it reads as a hang, so |
| 3235 | // we wait briefly then serve and let it finish in the background. The |
| 3236 | // gate stays installed until reconciliation settles, including on timeout. |
| 3237 | // Catch-up failures are logged by the engine; we proceed regardless so a |
| 3238 | // transient sync error never breaks tools. |
| 3239 | if (this.catchUpGate) { |
| 3240 | if (opts.startNow) return NOT_STARTED; |
| 3241 | const gate = this.catchUpGate; |
| 3242 | await this.awaitCatchUpGate(gate); |
| 3243 | } |
| 3244 | // Honor the optional tool allowlist (CODEGRAPH_MCP_TOOLS): a trimmed |
| 3245 | // surface rejects ablated tools defensively even if a client cached them. |
| 3246 | if (!this.isToolAllowed(toolName)) { |
| 3247 | return this.errorResult(`Tool ${toolName} is disabled via CODEGRAPH_MCP_TOOLS`); |
| 3248 | } |
| 3249 | // Cross-cutting input validation. All tools accept an optional |
| 3250 | // `projectPath` and most accept either `query`, `task`, or |
| 3251 | // `symbol` — bound their lengths centrally so individual handlers |
| 3252 | // can stay focused on tool-specific logic. |
| 3253 | const pathCheck = this.validateOptionalPath(args.projectPath, 'projectPath'); |
| 3254 | if (typeof pathCheck === 'object' && pathCheck !== undefined) { |
| 3255 | return pathCheck; |
| 3256 | } |
| 3257 | // An explicit project gets the same first-call guarantee as the default |
| 3258 | // (#1835): its post-open catch-up sync finishes (time-boxed) before we |
| 3259 | // serve it. Resolved on the main thread so the watcher lives here even |
| 3260 | // when dispatch is off-loaded to a worker. |
| 3261 | if (typeof pathCheck === 'string') { |
| 3262 | // A file inside a project (an agent passing the file it is looking at) |
| 3263 | // stands for its directory, like any other path inside it (#2982). |
| 3264 | // Rewritten here so the gate, the worker, the worktree notice and |
| 3265 | // getCodeGraph all see the same directory. |
| 3266 | let projectPath = pathCheck; |
| 3267 | try { |
| 3268 | if (statSync(pathCheck).isFile()) projectPath = dirname(pathCheck); |
| 3269 | } catch { /* a missing path walks up to its .codegraph/ as before */ } |
| 3270 | if (projectPath !== pathCheck) args = { ...args, projectPath }; |
| 3271 | await this.awaitProjectGate(projectPath); |
| 3272 | } |
| 3273 | // The `path` and `pattern` properties used by codegraph_files are |
| 3274 | // also path-shaped — apply the same cap. |
| 3275 | if (args.path !== undefined) { |
| 3276 | const check = this.validateOptionalPath(args.path, 'path'); |