openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
52 lines (51 loc) • 1.55 kB
JavaScript
import { ProtocolError, ProtocolErrorCode } from "@modelcontextprotocol/client";
//#region src/agents/mcp-tool-metadata.ts
/** Canonicalizes one server catalog before policy, publication, and call metadata diverge. */
function normalizeMcpToolCatalog(e, t, n = () => "include") {
const r = e.map((e) => e.name.trim());
const i = /* @__PURE__ */ new Map();
for (const e of r) if (e) i.set(e, (i.get(e) ?? 0) + 1);
const a = [];
const o = [];
const s = [];
const c = /* @__PURE__ */ new Map();
for (const [l, u] of e.entries()) {
const e = r[l] ?? "";
if (!e) continue;
const d = {
...u,
name: e
};
if (i.get(e) !== 1 || u.execution?.taskSupport === "required") {
s.push(d);
continue;
}
const f = n(e);
if (f === "exclude") {
s.push(d);
continue;
}
if (f === "include") {
a.push(d);
if (d.outputSchema) {
const n = t.getValidator(d.outputSchema);
c.set(e, (t) => {
if (t.isError === true) return;
if (t.structuredContent === void 0) throw new ProtocolError(ProtocolErrorCode.InvalidRequest, `Tool ${e} has an output schema but did not return structured content`);
const r = n(t.structuredContent);
if (!r.valid) throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Structured content does not match the tool's output schema: ${r.errorMessage}`);
});
}
} else o.push(d);
}
return {
tools: a,
excludedTools: s,
metadata: { validatorForCall(e) {
return c.get(e);
} },
deniedTools: o
};
}
//#endregion
export { normalizeMcpToolCatalog as t };