Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
1.9 KB
Raw Blame
import { operations } from '../core/operations.ts';
import { buildToolDefs } from '../mcp/tool-defs.ts';
import { writeStdoutFinal } from '../core/cli-force-exit.ts';

/**
 * `gbrain --tools-json` — the public CLI tool-catalog contract.
 *
 * The legacy keys (`name`, `description`, `parameters` with the compact
 * `"type"` / `"type?"` strings) are preserved VERBATIM — downstream scripts
 * parse them. WP3 adds the full JSON Schema (param descriptions, enums,
 * defaults, required array) additively under a new `schema` key per tool,
 * generated by the same buildToolDefs mapper the MCP transports serve.
 * Each tool also carries its access contract, additively (#5953 + agent
 * contract A2): `scope` and `required_scopes` (what an OAuth token needs to
 * call it), `mutating` (whether it writes), `idempotent` (whether repeating
 * it has the same effect), and `local_only` (reachable only from the local
 * CLI and stdio, never over HTTP) — so a client can derive which ops are
 * read-only from the catalog instead of keeping its own list.
 */
export async function printToolsJson(): Promise<void> {
  const defsByName = new Map(buildToolDefs(operations).map(def => [def.name, def]));
  const tools = operations.map(op => ({
    name: op.name,
    description: op.description,
    parameters: Object.fromEntries(
      Object.entries(op.params).map(([k, v]) => [
        k,
        `${v.type}${v.required ? '' : '?'}`,
      ]),
    ),
    schema: defsByName.get(op.name)!.inputSchema,
    scope: op.scope ?? 'read',
    required_scopes: op.requiredScopes ?? [],
    mutating: op.mutating === true,
    idempotent: op.idempotent === true,
    local_only: op.localOnly === true,
  }));

  // Awaited delivery (#3423): the catalog is well past 64KiB, the exact size
  // class that lost its tail to a slow pipe reader under queued stdout writes.
  await writeStdoutFinal(JSON.stringify(tools, null, 2) + '\n');
}