openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
71 lines (70 loc) • 2.8 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as filterStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import { o as safeToolNameCandidate } from "./agent-bundle-mcp-names-856aMFJA.mjs";
import { t as matchesStarSegments } from "./star-pattern-_Z4SIE-5.mjs";
//#region src/agents/mcp-tool-filter.ts
/** Match the documented MCP tool-filter glob syntax: exact text plus `*`. */
function matchesMcpToolFilterPattern(e, t) {
const n = e.trim();
if (!n) return false;
if (!n.includes("*")) return n === t;
const r = n.split("*");
return matchesStarSegments({
first: r[0] ?? "",
last: r.at(-1) ?? "",
interior: r.slice(1, -1).filter(Boolean)
}, t);
}
/** Normalizes open-world MCP tool filters into the runtime policy shape. */
function normalizeMcpToolFilter(e) {
if (!isRecord(e)) return;
const t = filterStringEntries(e.include);
const n = filterStringEntries(e.exclude);
if (t.length === 0 && n.length === 0) return;
return {
...t.length > 0 ? { include: t } : {},
...n.length > 0 ? { exclude: n } : {}
};
}
/** Reads a server's full advertised inventory, including filtered tools, from a catalog. */
function mcpCatalogToolFilterScope(e, t) {
const n = e.policyTools ?? [...e.tools, ...e.sessionDeniedTools ?? []];
const r = e.servers[t];
return {
safeServerName: r?.safeServerName ?? t,
toolNames: n.filter((e) => e.serverName === t).map((e) => e.toolName),
resources: r?.resources,
prompts: r?.prompts
};
}
/**
* Resolve probe aliases once against the full inventory. Raw names and globs
* retain their meaning. Includes accept only a unique unsuffixed owner;
* excludes hide every possible owner, including collision-suffixed owners.
* Without a scope, matching is raw-only.
*/
function createMcpToolFilter(e, t) {
const n = /* @__PURE__ */ new Set([
...t?.toolNames ?? [],
...t?.resources ? ["resources_list", "resources_read"] : [],
...t?.prompts ? ["prompts_list", "prompts_get"] : []
]);
const compileEntries = (e, r) => (e ?? []).map((e) => {
const i = e.trim();
if (!t || i.includes("*") || n.has(i)) return (e) => matchesMcpToolFilterPattern(i, e);
const candidate = (e, n = 1) => safeToolNameCandidate({
serverName: t.safeServerName,
toolName: e,
attempt: n
});
const a = Number(/-(\d+)$/.exec(i)?.[1] ?? 1);
const o = [...n].filter((e) => candidate(e) === i || candidate(e, a) === i);
const s = r ? o : o.filter((e) => o.length === 1 && candidate(e) === i);
return (e) => s.includes(e);
});
const r = compileEntries(e?.include, false);
const i = compileEntries(e?.exclude, true);
return (t) => (!e?.include?.length || r.some((e) => e(t))) && !i.some((e) => e(t));
}
//#endregion
export { mcpCatalogToolFilterScope as n, normalizeMcpToolFilter as r, createMcpToolFilter as t };