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

Method execute

src/mcp/tools.ts:3102–3295  ·  view source on GitHub ↗

* 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 = {},
  )

Source from the content-addressed store, hash-verified

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');

Callers 15

mainFunction · 0.95
askFunction · 0.95
serveLocallyFunction · 0.45
handleToolsCallMethod · 0.45
handlePromptExploreMethod · 0.45
exploreHereFunction · 0.45
probe-sweep.mjsFile · 0.45
probe-trace.mjsFile · 0.45
probe-explore.mjsFile · 0.45
runCaseFunction · 0.45
runExploreFunction · 0.45
probe-node.mjsFile · 0.45

Calls 15

textResultMethod · 0.95
awaitCatchUpGateMethod · 0.95
isToolAllowedMethod · 0.95
errorResultMethod · 0.95
validateOptionalPathMethod · 0.95
awaitProjectGateMethod · 0.95
getCodeGraphMethod · 0.95
awaitIndexRecoveryMethod · 0.95
handleStatusMethod · 0.95
withSessionViewMethod · 0.95
executeReadToolMethod · 0.95

Tested by 1

askFunction · 0.76