UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

62 lines (61 loc) • 3.77 kB
import { MCP_NAMESPACE, createToolNameClaims, findNamespacePrefixCollisions } from "@sema-agent/primitives"; import { inlineUntrusted } from "./untrusted-text.js"; import { foreignLine } from "./protocol-http-auth.js"; function refuseArgument(problem) { throw Object.assign(new Error(problem), { code: "config.invalid_argument" }); } function readNameSet(servers) { if (!Array.isArray(servers)) refuseArgument("judgeMcpNames takes a list of declared servers; what was passed is not a list."); return Array.from(servers, (server, index) => { if (typeof server !== "object" || server === null) refuseArgument(`judgeMcpNames: entry ${index} is not a server declaration (an object with a "name").`); const { name, tools } = server; if (typeof name !== "string") refuseArgument(`judgeMcpNames: entry ${index} has no string "name".`); if (tools === undefined) return { name }; const listed = Array.isArray(tools) ? Array.from(tools) : [undefined]; if (listed.some((tool) => typeof tool !== "string")) { refuseArgument(`judgeMcpNames: entry ${index} ("${inlineUntrusted(name, 120)}") names its tools as something other than a list of strings.`); } return { name, tools: listed }; }); } export function judgeMcpNames(servers) { const set = readNameSet(servers); const conflicts = findNamespacePrefixCollisions(MCP_NAMESPACE, set.map((s) => s.name)).map((c) => ({ kind: "server_name", names: c.peers, shared: c.prefix })); for (const server of set) { const claim = createToolNameClaims(MCP_NAMESPACE, server.name); for (const tool of server.tools ?? []) { const { minted, heldBy } = claim(tool); if (heldBy !== undefined) conflicts.push({ kind: "tool_name", server: server.name, names: [heldBy, tool], shared: minted }); } } const [first, ...rest] = conflicts; return first === undefined ? { ok: true } : { ok: false, conflicts: [first, ...rest] }; } export function mcpToolDropReason(minted, heldBy, tool) { return heldBy === tool ? `this server listed this tool name more than once in one listing; the first entry is mounted as "${minted}" and the repeat is dropped (one name cannot denote two tools)` : `name collides with an earlier tool of this server: both mount as "${minted}" after namespacing (only [a-zA-Z0-9_-] survives), and one name cannot denote two tools`; } export function describeMcpNameConflict(conflict) { const [a, b] = conflict.names; if (conflict.kind === "tool_name") { return `MCP server "${inlineUntrusted(conflict.server, 120)}", tool "${foreignLine(b, 120)}": ${mcpToolDropReason(conflict.shared, a, b)}`; } return a === b ? `MCP server "${inlineUntrusted(a, 120)}" is declared twice — each server needs its own name (both mount under "${conflict.shared}", so their tools would shadow each other and a refresh of one would unmount the other's).` : `MCP server names "${inlineUntrusted(a, 120)}" and "${inlineUntrusted(b, 120)}" both mount under "${conflict.shared}" — the namespaced tool name keeps only [a-zA-Z0-9_-], so they are the same domain to this engine (their tools would shadow each other and a refresh of one would unmount the other's). Rename one.`; } export function refuseCollidingMcpServerNames(serverNames) { const verdict = judgeMcpNames(serverNames.map((name) => ({ name }))); if (verdict.ok) return; const row = verdict.conflicts.find((c) => c.kind === "server_name"); if (row === undefined) return; throw Object.assign(new Error(describeMcpNameConflict(row)), { code: "config.mcp_server_name_collision" }); }