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

Method execute

src/mcp/tools.ts:3060–3253  ·  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

3058 * Omit it (the CLI does) and explore behaves exactly as before, untracked.
3059 */
3060 async execute(
3061 toolName: string,
3062 args: Record<string, unknown>,
3063 sessionState?: ExploreSessionState,
3064 indexVersionWarnings?: IndexVersionWarningState,
3065 opts: ExecuteOptions = {},
3066 ): Promise<ToolResult> {
3067 if (this.closing) {
3068 return opts.startNow ? NOT_STARTED : this.textResult('This MCP session is closing; retry with a connected session.');
3069 }
3070 this.activeCalls++;
3071 try {
3072 // Block the first tool call on the engine's post-open reconcile so we
3073 // never serve rows for files deleted/edited while no MCP server was
3074 // running. The wait is time-boxed (#905): a huge-repo reconcile takes
3075 // minutes, and blocking the first call on all of it reads as a hang, so
3076 // we wait briefly then serve and let it finish in the background. The
3077 // gate stays installed until reconciliation settles, including on timeout.
3078 // Catch-up failures are logged by the engine; we proceed regardless so a
3079 // transient sync error never breaks tools.
3080 if (this.catchUpGate) {
3081 if (opts.startNow) return NOT_STARTED;
3082 const gate = this.catchUpGate;
3083 await this.awaitCatchUpGate(gate);
3084 }
3085 // Honor the optional tool allowlist (CODEGRAPH_MCP_TOOLS): a trimmed
3086 // surface rejects ablated tools defensively even if a client cached them.
3087 if (!this.isToolAllowed(toolName)) {
3088 return this.errorResult(`Tool ${toolName} is disabled via CODEGRAPH_MCP_TOOLS`);
3089 }
3090 // Cross-cutting input validation. All tools accept an optional
3091 // `projectPath` and most accept either `query`, `task`, or
3092 // `symbol` — bound their lengths centrally so individual handlers
3093 // can stay focused on tool-specific logic.
3094 const pathCheck = this.validateOptionalPath(args.projectPath, 'projectPath');
3095 if (typeof pathCheck === 'object' && pathCheck !== undefined) {
3096 return pathCheck;
3097 }
3098 // An explicit project gets the same first-call guarantee as the default
3099 // (#1835): its post-open catch-up sync finishes (time-boxed) before we
3100 // serve it. Resolved on the main thread so the watcher lives here even
3101 // when dispatch is off-loaded to a worker.
3102 if (typeof pathCheck === 'string') {
3103 // A file inside a project (an agent passing the file it is looking at)
3104 // stands for its directory, like any other path inside it (#2982).
3105 // Rewritten here so the gate, the worker, the worktree notice and
3106 // getCodeGraph all see the same directory.
3107 let projectPath = pathCheck;
3108 try {
3109 if (statSync(pathCheck).isFile()) projectPath = dirname(pathCheck);
3110 } catch { /* a missing path walks up to its .codegraph/ as before */ }
3111 if (projectPath !== pathCheck) args = { ...args, projectPath };
3112 await this.awaitProjectGate(projectPath);
3113 }
3114 // The `path` and `pattern` properties used by codegraph_files are
3115 // also path-shaped — apply the same cap.
3116 if (args.path !== undefined) {
3117 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