Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

17.3 KB
Raw Blame
// Dynamic Tool Registry — MCP-shaped, replaces hardcoded AGENT_TOOL_DEFS

import { turnCaps, capCategory } from '../../lib/remote-turn-caps'
import type { MCPToolDefinition, PermissionMap, PermissionLevel, ToolArgs } from './types'
import type { OllamaTool } from '../../types/agent-mode'
import type { ToolDefinition } from '../providers/types'
import { MUTATING_TOOLS } from '../../lib/mutating-tools'
import { RETIRED_TOOL_NAMES, retiredPermissionLevel } from '../../lib/retired-tools'
import { retiredToolRunsOnThisDevice } from '../../lib/mobile-tools'
import { toolOfferedToChatModel, SUB_AGENTS_NEED_CLOUD_FOR_MODEL } from '../../lib/phone-sub-agents'
import type { AgentRunContext } from '../agent-context'
import { externalSecretAccessOf, secretAccessOf, secretRefusal, type SecretHit } from '../../lib/secret-paths'

/**
 * The optional second argument is the run this call belongs to (plan 2.6.6 C1).
 * Tools whose gate depends on the run (read-only turn, todo target
 * conversation, artifact capture, workspace jail) read it from there instead of
 * the process-wide singleton, so a second interleaving run cannot move their
 * goalposts mid-call. Tools that do not care simply ignore it.
 */
type ToolExecutor = (
  args: ToolArgs,
  run?: AgentRunContext,
  signal?: AbortSignal,
) => Promise<string>
/** The pre-2.6.6 shape, still accepted from external servers and tests. */
type LegacyToolExecutor = (args: ToolArgs) => Promise<string>
/**
 * External-tool executor gets the tool name too, because one MCP server
 * owns many tools and routes by name. The registry wraps it into a
 * per-tool ToolExecutor closure so the Map lookup stays {name → executor}.
 */
type ExternalToolExecutor = (toolName: string, args: ToolArgs) => Promise<string>

/**
 * The flat shape the Hermes/XML tool prompt wants: the definition's own
 * JSON-Schema under `parameters`, no wrapper. `any` here meant the prompt
 * builder could be handed anything at all — including `undefined` — and the
 * `<tools>` block would have silently advertised a tool with no parameters.
 */
export interface HermesToolDef {
  name: string
  description: string
  parameters: MCPToolDefinition['inputSchema']
}

interface RegisteredTool {
  definition: MCPToolDefinition
  executor: ToolExecutor
}

export class ToolRegistry {
  private tools = new Map<string, RegisteredTool>()
  /**
   * Names the app itself owns. A third-party MCP server — the settings panel
   * advertises those as "community tools" — could register under `file_write`
   * or `shell_execute` and the Map.set simply replaced the builtin: every later
   * call went to the foreign server, transparently, with the builtin's own
   * description still selling it to the model. Disconnecting the server then
   * ran unregisterServer, which deleted the entry by serverId and took the
   * BUILTIN with it — permanently, for the rest of the session.
   *
   * Builtins are therefore untouchable: a colliding external name is refused,
   * not merged, not suffixed. Suffixing would be worse than refusing, because
   * the model would then see two tools that both claim to be the terminal.
   */
  private builtinNames = new Set<string>()
  /** Collisions refused since startup, for the MCP settings panel / report. */
  private rejectedExternal: { serverId: string; toolName: string }[] = []
  /**
   * Fallback für zurückgezogene Tool-Namen (2.6.6 tool merge), gesetzt von
   * registerBuiltinTools.
   *
   * M7 / Audit W-T2: hier stand vorher `await import('./builtin-tools')`
   * mitten in execute(). Der Kommentar daneben nannte den echten Grund —
   * „keeps the module graph acyclic" —, aber gesplittet hat dieser import()
   * nie: mcp/index.ts zieht builtin-tools ohnehin statisch herein, also lag das
   * Modul immer schon im selben Chunk. Rolldown hat das als
   * INEFFECTIVE_DYNAMIC_IMPORT gemeldet, und zu Recht.
   *
   * Ein statischer Import wäre die falsche Auflösung gewesen: die generische
   * Registry darf die konkreten Builtins nicht kennen (dieselbe Regel, wegen
   * der RETIRED_MUTATING_NAMES schon in lib/retired-tools.ts ausgelagert ist).
   * Also wird die Abhängigkeit umgedreht — builtin-tools *meldet* seinen
   * Redirect an, statt dass die Registry ihn sich holt. Kante weg, Zyklus weg,
   * Warnung weg.
   */
  private retiredRunner:
    | ((name: string, args: ToolArgs, run?: AgentRunContext, signal?: AbortSignal) => Promise<string | null>)
    | null = null

  // ── Registration ──────────────────────────────────────────────

  /** Siehe `retiredRunner`. Wird von registerBuiltinTools() verdrahtet. */
  setRetiredRunner(
    run: (name: string, args: ToolArgs, run?: AgentRunContext, signal?: AbortSignal) => Promise<string | null>,
  ) {
    this.retiredRunner = run
  }

  registerBuiltin(tool: MCPToolDefinition, executor: ToolExecutor) {
    this.builtinNames.add(tool.name)
    this.tools.set(tool.name, { definition: tool, executor })
  }

  /**
   * Take an app tool out of the catalog while a setting has it switched off
   * (computer control). The name stays reserved: an MCP server still cannot
   * register under it, on or off.
   */
  withdrawBuiltin(name: string) {
    if (this.builtinNames.has(name)) this.tools.delete(name)
  }

  /** Is this name owned by the app (and thus off-limits to MCP servers)? */
  isBuiltinName(name: string): boolean {
    return this.builtinNames.has(name)
  }

  /** External registrations refused because they collided with a builtin. */
  getRejectedExternalTools(): readonly { serverId: string; toolName: string }[] {
    return this.rejectedExternal
  }

  /**
   * Register all tools from an external MCP server. The executor receives
   * both the tool name and args, letting one MCP client back many tools
   * (which is how MCP servers actually work — one `tools/call` endpoint,
   * dispatched by name).
   *
   * Also accepts the legacy single-arg executor shape for backward compat
   * — tests and older callers can still pass `(args) => ...` and the
   * registry will call it unchanged. Prefer the two-arg shape in new code.
   */
  registerExternal(
    serverId: string,
    tools: MCPToolDefinition[],
    executor: ExternalToolExecutor | LegacyToolExecutor,
    /** The name the user gave the server, shown as `<server>: <tool>`. */
    serverName?: string,
  ) {
    const isTwoArg = executor.length >= 2
    for (const tool of tools) {
      const name = tool.name
      // Name collision with an app tool: refuse the registration and say so.
      // The alternative (overwrite) hands the app's own file and terminal tools
      // to a third-party process without a word to the user, and the eventual
      // disconnect deletes the builtin for good. See `builtinNames`.
      if (this.builtinNames.has(name)) {
        this.rejectedExternal.push({ serverId, toolName: name })
        console.warn(
          `[ToolRegistry] MCP server "${serverId}" tried to register "${name}", `
          + 'which is a built-in tool. Registration refused; the built-in stays in place.',
        )
        continue
      }
      const call: LegacyToolExecutor = isTwoArg
        ? (args: ToolArgs) =>
            (executor as ExternalToolExecutor)(name, args)
        : (executor as LegacyToolExecutor)
      // The secret floor of the built-in tools (builtin-tools guardSecrets),
      // for a third-party server too (M1): a call that names a secret path
      // anywhere in its arguments is refused before it leaves the app, in
      // every mode, Bypass included.
      const bound: ToolExecutor = async (args: ToolArgs) => {
        const hit = externalSecretAccessOf(args)
        if (hit) return secretRefusal(hit)
        return call(args)
      }
      const shownServer = serverName?.trim() || tool.serverName
      this.tools.set(name, {
        // Whatever category a definition claims, an external tool sits in
        // 'mcp' (B12): a server must not file its own shell tool under a row
        // the user set to Auto.
        definition: { ...tool, category: 'mcp', source: 'external', serverId, ...(shownServer ? { serverName: shownServer } : {}) },
        executor: bound,
      })
    }
  }

  unregisterServer(serverId: string) {
    for (const [name, entry] of this.tools) {
      // `source === 'external'` as well as the serverId: a builtin can never be
      // collateral of a disconnect, whatever a definition claims to carry.
      if (entry.definition.serverId === serverId && !this.builtinNames.has(name)) {
        this.tools.delete(name)
      }
    }
    this.rejectedExternal = this.rejectedExternal.filter((r) => r.serverId !== serverId)
  }

  // ── Query ─────────────────────────────────────────────────────

  /**
   * Every tool a run may be offered. On a phone whose chat model runs on the
   * phone itself the sub agent tools are left out (lib/phone-sub-agents.ts);
   * every other device and model gets the whole registry, as before.
   */
  getAll(): MCPToolDefinition[] {
    return Array.from(this.tools.values())
      .map(t => t.definition)
      .filter(t => toolOfferedToChatModel(t.name))
  }

  getAvailableTools(permissions: PermissionMap): MCPToolDefinition[] {
    return this.getAll().filter(t => permissions[t.category] !== 'blocked')
  }

  getToolByName(name: string): MCPToolDefinition | undefined {
    return this.tools.get(name)?.definition
  }

  /**
   * The name a person reads for this tool: `<server>: <tool>` for a tool of
   * an external server, so an approval says whose tool asks (M1). The
   * registered name the model calls stays the bare tool name.
   */
  displayName(name: string): string {
    const def = this.tools.get(name)?.definition
    if (def?.source === 'external') return `${def.serverName || def.serverId || 'MCP server'}: ${name}`
    return name
  }

  /**
   * The secret path a call names, with the checker its tool uses: every
   * string that reads like a path for an external tool, the known keys for
   * an app tool. For the notice in the run; the refusal itself sits in the
   * executor.
   */
  secretHitOf(name: string, args: Record<string, unknown> | undefined): SecretHit | null {
    if (this.tools.get(name)?.definition.source === 'external') return externalSecretAccessOf(args)
    return secretAccessOf(name, args)
  }

  /**
   * Resolve a name to something the step executor may run: registered tools
   * by their definition, retired names (2.6.6 merge) as a schema-less stub so
   * the executor reaches execute(), where runRetiredTool redirects them.
   * Without this, the executor's own getTool miss fails the call as
   * "Unknown tool" before the redirect ever runs.
   */
  resolveExecutable(name: string): { name: string; inputSchema?: MCPToolDefinition['inputSchema'] } | undefined {
    const td = this.tools.get(name)?.definition
    if (td) return { name: td.name, inputSchema: td.inputSchema }
    // Not on a phone, apart from the clock: a stub here would carry the old
    // shell names all the way to an approval dialog for a tool that cannot run.
    if (RETIRED_TOOL_NAMES.has(name) && retiredToolRunsOnThisDevice(name)) return { name }
    return undefined
  }

  getPermissionLevel(toolName: string, permissions: PermissionMap): PermissionLevel {
    const tool = this.tools.get(toolName)?.definition
    // A map from before a category existed has no key for it: ask.
    if (tool) return permissions[tool.category] ?? 'confirm'
    // A9: a retired name has no definition to read a category off, and the
    // 'confirm' below meant Agent mode opened an approval dialog for
    // `git_status`, whose executor is one fixed read. Read-only retired names
    // run unattended, mutating ones keep confirm, blocked stays blocked.
    // Anything else is genuinely unknown and confirm is the honest answer.
    return retiredPermissionLevel(toolName, permissions) ?? 'confirm'
  }

  /**
   * Phase 12 — resolve the effective permission for a tool with a per-tool
   * override map layered on top of category defaults. The override map is
   * typically sourced from permissionStore.perToolOverrides and takes
   * precedence over the category permission; when no override exists we
   * fall back to getPermissionLevel() semantics.
   */
  getPermissionLevelWithOverrides(
    toolName: string,
    permissions: PermissionMap,
    perToolOverrides: Record<string, PermissionLevel>
  ): PermissionLevel {
    const override = perToolOverrides[toolName]
    if (override) return override
    return this.getPermissionLevel(toolName, permissions)
  }

  // ── Execution ─────────────────────────────────────────────────

  async execute(
    name: string,
    args: ToolArgs,
    maxRetries = 1,
    run?: AgentRunContext,
    signal?: AbortSignal,
  ): Promise<string> {
    // The run's Stop, in the tool's own hands. Callers that predate the fourth
    // argument still reach it through the run they already thread.
    const abort = signal ?? run?.abortSignal
    // Stop is not "finish the batch quietly": a call that has not started yet
    // must not start. The executor checks this too, but the registry is also
    // reached directly (sub-agents, retired-name redirects).
    if (abort?.aborted) return `Error: cancelled by the user before "${name}" started.`
    // Not offered means not runnable either: a restored chat or a model that
    // writes the name from memory must not start what the list left out.
    if (!toolOfferedToChatModel(name)) return SUB_AGENTS_NEED_CLOUD_FOR_MODEL
    const entry = this.tools.get(name)
    if (!entry) {
      // Retired names (2.6.6 tool merge) still run: a restored session or a
      // model that knows git_status from its context must not burn the step
      // on "Unknown tool". Der Redirect kommt über setRetiredRunner herein
      // (siehe `retiredRunner`) statt über einen Rück-Import auf builtin-tools.
      const redirected = this.retiredRunner ? await this.retiredRunner(name, args, run, abort) : null
      if (redirected !== null) return redirected
      return `Error: Unknown tool "${name}"`
    }
    // A turn from the paired phone never uses more than the device may
    // (lib/remote-turn-caps), whatever the permissions or the Code preset say.
    if (turnCaps(run?.conversationId ?? undefined)?.[capCategory(entry.definition)] === 'blocked') {
      return `Error: ${name} is not allowed for the phone that started this turn. Its rights on this computer leave it out. Continue without it.`
    }

    // Audit B2: a retry re-RUNS the tool. For a side-effecting tool that is a
    // second commit, a second push, a second shell command — never acceptable
    // on spec. Reads may retry, and only on transient failures.
    const retriable = !MUTATING_TOOLS.has(name)
    const isTransientText = (s: string) =>
      s.includes('timed out') || s.includes('ECONNREFUSED') || s.includes('fetch failed')

    for (let attempt = 0; attempt <= maxRetries; attempt++) {
      try {
        const result = await entry.executor(args, run, abort)
        // If result is an error and we have retries left, retry
        if (result.startsWith('Error:') && attempt < maxRetries) {
          // A retry after Stop would run the command the user just cancelled a
          // second time — the one case where "transient" is the wrong read.
          if (retriable && isTransientText(result) && !abort?.aborted) continue
        }
        return result
      } catch (err) {
        const message = err instanceof Error ? err.message : String(err)
        // The throw path used to retry EVERYTHING once, blind — including a
        // git_commit whose invoke threw after the commit landed, and aborted
        // calls. Same rule as the string path: transient + non-mutating only,
        // and an abort is the user speaking, not a network hiccup.
        const aborted =
          (err as { name?: string })?.name === 'AbortError'
          || /abort/i.test(message)
          || abort?.aborted === true
        if (attempt < maxRetries && retriable && !aborted && isTransientText(message)) continue
        return `Error: ${message}`
      }
    }
    return `Error: Max retries exceeded for "${name}"`
  }

  // ── Format Conversion ─────────────────────────────────────────

  toOllamaTools(permissions: PermissionMap): OllamaTool[] {
    return this.getAvailableTools(permissions).map(t => ({
      type: 'function' as const,
      function: {
        name: t.name,
        description: t.description,
        parameters: t.inputSchema,
      },
    }))
  }

  toOpenAITools(permissions: PermissionMap): ToolDefinition[] {
    return this.getAvailableTools(permissions).map(t => ({
      type: 'function' as const,
      function: {
        name: t.name,
        description: t.description,
        parameters: t.inputSchema,
      },
    }))
  }

  toHermesToolDefs(permissions: PermissionMap): HermesToolDef[] {
    return this.getAvailableTools(permissions).map(t => ({
      name: t.name,
      description: t.description,
      parameters: t.inputSchema,
    }))
  }
}

// ── Singleton ────────────────────────────────────────────────────

export const toolRegistry = new ToolRegistry()