JuliusBrussee/caveman · error

tool search response contained an unknown or duplicate tool

Error message

tool search response contained an unknown or duplicate tool

What it means

After mapping the returned tools back to the local catalog, any returned name that is not in the catalog or appears more than once throws this error. It guarantees the server only selected tools the client offered and never duplicates.

Solutions

  1. Drop the stale toolSessionId and start a fresh search session
  2. Ensure the catalog passed to searchTools is identical across calls in a session
  3. Check for SDK/server version mismatch and upgrade both
  4. Log data.tools to identify the unknown/duplicate name and fix the catalog or report the server bug

Example fix

// before
await searchTools(cave, newCatalog, query, { toolSessionId: oldSessionId }); // session knows old tool names
// after
await searchTools(cave, newCatalog, query); // fresh session matching this catalog
Defensive patterns

Strategy: try-catch

Validate before calling

if (toolSessionId) {
  // only reuse sessions whose catalog matches exactly
  if (!sessions.get(toolSessionId)?.catalogEquals(catalog)) toolSessionId = undefined;
}

Type guard

null

Try / catch

try {
  return await searchTools(cave, catalog, query, { toolSessionId });
} catch (e) {
  if (/unknown or duplicate tool/.test(e.message)) {
    // stale session: retry once without the session id
    return await searchTools(cave, catalog, query);
  }
  throw e;
}

Prevention

When it happens

Trigger: Server returns a tool name absent from the request catalog, an empty/malformed name entry, or the same tool name twice in data.tools.

Common situations: Server/session state referencing tools from a previous catalog (stale toolSessionId); server bug or version drift; cached session returning older tool set.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/c45f8499972d3c1f. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/typescript/src/index.ts:1637

  const fullSchemaTokens = validCounts ? rawFull : 0;
  const savedTokens = fullSchemaTokens - sentSchemaTokens;
  const reductionPct = fullSchemaTokens === 0 ? 0 : Math.round((savedTokens / fullSchemaTokens) * 1000) / 10;

  const catalogByName = new Map<string, CaveTool>();
  for (const tool of catalog) {
    if (typeof tool.name !== "string" || tool.name.trim() === "" || catalogByName.has(tool.name)) {
      throw new Error("tool catalog names must be non-empty and unique");
    }
    catalogByName.set(tool.name, tool);
  }
  if (!Array.isArray(data.tools)) throw new Error("tool search response tools must be an array");
  const returnedNames = new Set<string>();
  const selectedTools = data.tools.map((wire: unknown) => {
    const name = wire !== null && typeof wire === "object" && typeof (wire as Record<string, unknown>)["name"] === "string"
      ? (wire as Record<string, unknown>)["name"] as string
      : "";
    const local = catalogByName.get(name);
    if (!local || returnedNames.has(name)) throw new Error("tool search response contained an unknown or duplicate tool");
    returnedNames.add(name);
    return local;
  });

  return {
    sessionId: typeof data.session_id === "string" ? data.session_id : undefined,
    tools: selectedTools,
    sentSchemaTokens,
    fullSchemaTokens,
    deferredCount: strictNonNegativeInt(data.deferred_count) ?? 0,
    method: data.method ?? "unknown",
    tokenBasis: typeof data.token_basis === "string" && data.token_basis.trim() ? data.token_basis : "unavailable",
    basis: "inferred",
    savedTokens,
    reductionPct
  };
}

View on GitHub (pinned to 3ee70a1026)