openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
89 lines (88 loc) • 3.18 kB
JavaScript
import { c as normalizeOptionalLowercaseString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
//#region src/agents/agent-bundle-mcp-names.ts
const TOOL_NAME_SAFE_RE = /[^A-Za-z0-9_-]/g;
const TOOL_NAME_SEPARATOR = "__";
const TOOL_NAME_MAX_PREFIX = 30;
const TOOL_NAME_MAX_TOTAL = 64;
function compareMcpCatalogTools(e, t) {
return e.safeServerName.localeCompare(t.safeServerName) || e.toolName.localeCompare(t.toolName) || e.serverName.localeCompare(t.serverName);
}
function mcpCatalogToolsSignature(e) {
return JSON.stringify(e.map((e) => [
e.serverName,
e.safeServerName,
e.toolName,
e.title ?? "",
e.description ?? "",
e.fallbackDescription,
e.inputSchema,
e.uiResourceUri ?? "",
e.uiVisibility ?? null
]));
}
function sanitizeNodeIdFragment(e) {
const t = e.trim().toLowerCase().replace(/[^a-z0-9_]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 32);
if (!t) return "node";
return /^[a-z]/.test(t) ? t : `node_${t}`.slice(0, 32);
}
function sanitizeToolFragment(e, t, n) {
const r = e.trim().replace(TOOL_NAME_SAFE_RE, "-") || t;
const i = /^[A-Za-z]/.test(r) ? r : `${t}-${r}`;
if (!n) return i;
return i.length > n ? i.slice(0, n) : i;
}
function sanitizeServerName(e, t) {
const n = sanitizeToolFragment(e, "mcp", TOOL_NAME_MAX_PREFIX);
let r = n;
let i = 2;
while (t.has(normalizeLowercaseStringOrEmpty(r))) {
const e = `-${i}`;
r = `${n.slice(0, Math.max(1, TOOL_NAME_MAX_PREFIX - e.length))}${e}`;
i += 1;
}
t.add(normalizeLowercaseStringOrEmpty(r));
return r;
}
/**
* Assign safe server names from the full declared set in declaration order,
* independent of which servers resolve for a requester. Declaration order
* preserves legacy collision-suffix ownership for existing static configs;
* sorting here would silently swap safe names between colliding servers.
*/
function assignSafeServerNames(e) {
const t = /* @__PURE__ */ new Set();
const n = /* @__PURE__ */ new Map();
for (const r of e) n.set(r, sanitizeServerName(r, t));
return n;
}
function normalizeReservedToolNames(e) {
return new Set(Array.from(e ?? [], (e) => normalizeOptionalLowercaseString(e)).filter((e) => Boolean(e)));
}
/**
* The name buildSafeToolName tries on a given attempt: attempt 1 is the
* unsuffixed name, and attempt n >= 2 is the `-n` collision-suffixed name.
*/
function safeToolNameCandidate(e) {
const t = Math.max(1, TOOL_NAME_MAX_TOTAL - e.serverName.length - 2);
const n = sanitizeToolFragment(e.toolName, "tool").slice(0, t) || "tool";
const r = e.attempt > 1 ? `-${e.attempt}` : "";
const i = r ? `${n.slice(0, Math.max(1, t - r.length))}${r}` : n;
return `${e.serverName}${"__"}${i}`;
}
function buildSafeToolName(e) {
let t = 1;
let n = safeToolNameCandidate({
...e,
attempt: t
});
while (e.reservedNames.has(normalizeLowercaseStringOrEmpty(n))) {
t += 1;
n = safeToolNameCandidate({
...e,
attempt: t
});
}
return n;
}
//#endregion
export { normalizeReservedToolNames as a, sanitizeServerName as c, mcpCatalogToolsSignature as i, buildSafeToolName as n, safeToolNameCandidate as o, compareMcpCatalogTools as r, sanitizeNodeIdFragment as s, assignSafeServerNames as t };