UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

183 lines (182 loc) • 7.11 kB
import { d as asPositiveFiniteNumber, h as clampPositiveTimerTimeoutMs } from "./number-coercion-D6u-4YD8.mjs"; import { i as asOptionalObjectRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { o as racePromiseWithAbortSignal } from "./src-BPmQBLJp.mjs"; import "./abort-signal-D4Q6SEw7.mjs"; import { t as boundedJsonUtf8Bytes } from "./json-utf8-bytes-DNwtq2cu.mjs"; import { isDeepStrictEqual } from "node:util"; import { CallToolResultSchema, GetPromptResultSchema, ListResourceTemplatesResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/core"; import { CallToolResultSchema as CallToolResultSchema$1, GetPromptResultSchema as GetPromptResultSchema$1, ListResourceTemplatesResultSchema as ListResourceTemplatesResultSchema$1, ListToolsResultSchema as ListToolsResultSchema$1 } from "@modelcontextprotocol/sdk/types.js"; //#region src/agents/mcp-catalog-listing.ts const MCP_CATALOG_MAX_PAGES = 128; const MCP_CATALOG_MAX_ITEMS = 16384; const MCP_CATALOG_MAX_BYTES = 10485760; function abortError(e, t) { return e.reason instanceof Error ? e.reason : /* @__PURE__ */ new Error(`${t} aborted`); } /** Collects one complete MCP list under a single bounded lifecycle. */ async function collectMcpPaginatedItems(e) { const t = clampPositiveTimerTimeoutMs(e.timeoutMs); if (t === void 0) throw new Error(`${e.label} requires a positive timeout`); const n = new AbortController(); const r = e.signal ? AbortSignal.any([e.signal, n.signal]) : n.signal; if (r.aborted) throw abortError(r, e.label); const i = performance.now() + t; const a = /* @__PURE__ */ new Error(`${e.label} timed out after ${t}ms`); const o = setTimeout(() => n.abort(a), t); o.unref(); const assertActive = () => { if (r.aborted) throw abortError(r, e.label); if (performance.now() >= i) { n.abort(a); throw a; } }; const s = []; const c = /* @__PURE__ */ new Set(); let l = 0; let u; try { for (let n = 0; n < MCP_CATALOG_MAX_PAGES; n += 1) { assertActive(); const n = await racePromiseWithAbortSignal(e.loadPage({ cursor: u, requestTimeoutMs: t, signal: r }), r, () => abortError(r, e.label)); assertActive(); const i = boundedJsonUtf8Bytes(n.serializedValue ?? { items: n.items, nextCursor: n.nextCursor }, MCP_CATALOG_MAX_BYTES - l); if (!i.complete || l + i.bytes > MCP_CATALOG_MAX_BYTES) throw new Error(`${e.label} exceeded ${MCP_CATALOG_MAX_BYTES} bytes`); l += i.bytes; for (const t of n.items) { if (s.length >= MCP_CATALOG_MAX_ITEMS) throw new Error(`${e.label} exceeded ${MCP_CATALOG_MAX_ITEMS} ${e.itemLabel}`); s.push(t); } const a = n.nextCursor; if (a === void 0) return s; if (c.has(a)) throw new Error(`${e.label} returned a repeated pagination cursor`); c.add(a); u = a; } throw new Error(`${e.label} exceeded ${MCP_CATALOG_MAX_PAGES} pages`); } finally { clearTimeout(o); } } async function listAllMcpTools(e, t, n) { return await collectMcpPaginatedItems({ label: "MCP tool listing", itemLabel: "tools", timeoutMs: t, signal: n, loadPage: async ({ cursor: t, requestTimeoutMs: n, signal: r }) => { const i = await e.request({ method: "tools/list", params: t === void 0 ? void 0 : { cursor: t } }, { timeout: n, maxTotalTimeout: n, signal: r }); return { items: i.tools, nextCursor: i.nextCursor, serializedValue: i }; } }); } function listMcpServerItems(e, { client: t, timeoutMs: n, signal: r, callerSignal: i, runRequest: a }) { return collectMcpPaginatedItems({ label: `MCP ${e === "resources" ? "resource" : "prompt"} listing`, itemLabel: e, timeoutMs: n, signal: i ? AbortSignal.any([r, i]) : r, loadPage: ({ cursor: n, requestTimeoutMs: r, signal: i }) => a(async (i) => { const a = n === void 0 ? void 0 : { cursor: n }; const o = { timeout: r, maxTotalTimeout: r, signal: i }; if (e === "resources") { const e = await t.request({ method: "resources/list", params: a }, o); return { items: e.resources, nextCursor: e.nextCursor, serializedValue: e }; } const s = await t.request({ method: "prompts/list", params: a }, o); return { items: s.prompts, nextCursor: s.nextCursor, serializedValue: s }; }, i) }); } const BUNDLE_MCP_CATALOG_LIST_TIMEOUT_MS = 1e4; function getCatalogListTimeoutMs(e, t) { const n = asOptionalObjectRecord(e); return ["requestTimeoutMs", "timeout"].some((e) => asPositiveFiniteNumber(n?.[e]) !== void 0) ? t : BUNDLE_MCP_CATALOG_LIST_TIMEOUT_MS; } //#endregion //#region src/agents/mcp-sdk-compatibility.ts /** The published plugin SDK retains its v1 result shapes for one release window. */ var McpSdkCompatibilityError = class extends Error { constructor(e) { super(`MCP ${e} result cannot be represented losslessly by the current plugin SDK`); this.method = e; this.code = "MCP_SDK_RESULT_UNREPRESENTABLE"; this.name = "McpSdkCompatibilityError"; } }; function preservesWireFields(e, t) { if (Array.isArray(e)) return Array.isArray(t) && e.length === t.length && e.every((e, n) => preservesWireFields(e, t[n])); if (isRecord(e)) return isRecord(t) && Object.entries(e).every(([e, n]) => Object.hasOwn(t, e) && preservesWireFields(n, t[e])); return Object.is(e, t); } function losslessWireResultSchema(e, t) { return { "~standard": { version: 1, vendor: "openclaw", validate(n) { const r = t.safeParse(n); if (!r.success) return { issues: r.error.issues }; if (!preservesWireFields(n, r.data)) throw new McpSdkCompatibilityError(e); return { value: r.data }; } } }; } const mcpCallToolResultSchema = losslessWireResultSchema("tools/call", CallToolResultSchema); const mcpListToolsResultSchema = losslessWireResultSchema("tools/list", ListToolsResultSchema); const mcpListResourceTemplatesResultSchema = losslessWireResultSchema("resources/templates/list", ListResourceTemplatesResultSchema); const mcpGetPromptResultSchema = losslessWireResultSchema("prompts/get", GetPromptResultSchema); function legacyResult(e, t, n) { const r = t.safeParse(n); if (!r.success || !isDeepStrictEqual(n, r.data)) throw new McpSdkCompatibilityError(e); return r.data; } function toLegacyMcpCallToolResult(e) { return legacyResult("tools/call", CallToolResultSchema$1, e); } function toLegacyMcpListToolsResult(e) { return legacyResult("tools/list", ListToolsResultSchema$1, e); } function toLegacyMcpListResourceTemplatesResult(e) { return legacyResult("resources/templates/list", ListResourceTemplatesResultSchema$1, e); } function toLegacyMcpGetPromptResult(e) { return legacyResult("prompts/get", GetPromptResultSchema$1, e); } //#endregion export { mcpListToolsResultSchema as a, toLegacyMcpListResourceTemplatesResult as c, listAllMcpTools as d, listMcpServerItems as f, mcpListResourceTemplatesResultSchema as i, toLegacyMcpListToolsResult as l, mcpCallToolResultSchema as n, toLegacyMcpCallToolResult as o, mcpGetPromptResultSchema as r, toLegacyMcpGetPromptResult as s, McpSdkCompatibilityError as t, getCatalogListTimeoutMs as u };