UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

373 lines (372 loc) • 13.5 kB
import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { d as withPluginRuntimeRegistryScope } from "./gateway-request-scope-BvvDhs5D.mjs"; import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { t as createEmptyPluginRegistry } from "./registry-empty-Cv4VTWYA.mjs"; import { t as assignSafeServerNames } from "./agent-bundle-mcp-names-856aMFJA.mjs"; import { i as collectExplicitAllowlist } from "./tool-policy-BR4v75RA.mjs"; import { c as normalizeToolPolicyName } from "./tool-policy-shared-BHvBopvF.mjs"; import { o as setPluginToolMeta } from "./tool-metadata-rqUpUHlb.mjs"; import { n as resolveEffectiveToolPolicy } from "./agent-tools.policy-CM-3Smt9.mjs"; import { i as resolveConversationCapabilityProfile } from "./conversation-capability-profile-Be5DqCww.mjs"; import { f as isUpdateDoctorLintPass } from "./update-phase-YdZvaQsy.mjs"; import { u as partitionMcpServersByConnectionScope } from "./bundle-mcp-config-Ca9TXxki.mjs"; import { t as loadSessionMcpConfig } from "./agent-bundle-mcp-runtime-config-CBo1pSHY.mjs"; import { t as appendRuntimePluginToolGrant } from "./tool-grant-allowlist-BZ6u_mqs.mjs"; import { t as applyFinalEffectiveToolPolicy } from "./effective-tool-policy-DMFsqGJo.mjs"; import { a as shouldCreateBundleMcpRuntimeForAttempt } from "./attempt-tool-construction-plan-Cp8YcsYs.mjs"; //#region src/flows/doctor-tool-schema-runtime.ts function bundleMcpRuntimeFailureFinding(e, t) { return { checkId: "core/doctor/runtime-tool-schemas", severity: "error", message: `Configured MCP tool schema validation could not ${t} the runtime tool set.`, path: "mcp.servers", requirement: formatErrorMessage(e), fixHint: t === "normalize" ? "Fix provider/plugin schema normalization errors, then rerun doctor before relying on assistant tool startup." : "Fix or disable the offending MCP server, then rerun doctor before relying on assistant tool startup." }; } function bundleMcpRuntimeDiagnosticFinding(e) { return { checkId: "core/doctor/runtime-tool-schemas", severity: "error", message: `Configured MCP server "${e.serverName}" could not expose runtime tools for schema validation.`, path: `mcp.servers.${e.serverName}`, requirement: e.message, fixHint: "Fix or disable the offending MCP server, then rerun doctor before relying on assistant tool startup." }; } function bundleMcpRequesterInspectionFinding(e) { return { checkId: "core/doctor/runtime-tool-schemas", severity: "info", message: `Configured requester-scoped MCP server "${e}" was not checked without an authenticated requester.`, path: `mcp.servers.${e}`, requirement: "authenticated requester context", fixHint: "Verify this server from an authenticated agent turn." }; } function makeBundleMcpDiagnosticSentinel(e) { const t = { name: e, label: "Bundle MCP diagnostic", description: "Internal doctor sentinel for bundle MCP schema diagnostics.", parameters: { type: "object", properties: {} }, execute: async () => ({ content: [], details: {} }) }; setPluginToolMeta(t, { pluginId: "bundle-mcp", optional: false }); return t; } function synthesizeBundleMcpAllowlistSentinelName(e) { const t = normalizeToolPolicyName(e.allowlistEntry); const n = normalizeToolPolicyName(`${e.safeServerName}${"__"}`); if (t.startsWith(n)) return t; const r = t.lastIndexOf("__"); if (r < 0) return; const i = t.slice(r + "__".length); if (!i) return; const a = i.replace(/\*/g, "diagnostic").replace(/\?/g, "x"); return `${e.safeServerName}${"__"}${a}`; } function collectBundleMcpDiagnosticSentinels(e) { const t = [makeBundleMcpDiagnosticSentinel(`${e.diagnostic.safeServerName}${"__"}runtime_schema`)]; const n = resolveEffectiveToolPolicy({ config: e.cfg, agentId: e.agentId, modelProvider: e.modelRef.provider, modelId: e.modelRef.model }); const r = collectExplicitAllowlist([ n.globalPolicy, n.globalProviderPolicy, n.agentPolicy, n.agentProviderPolicy, n.profileAlsoAllow ? { allow: n.profileAlsoAllow } : void 0, n.providerProfileAlsoAllow ? { allow: n.providerProfileAlsoAllow } : void 0 ]); for (const n of r) { const r = synthesizeBundleMcpAllowlistSentinelName({ safeServerName: e.diagnostic.safeServerName, allowlistEntry: n }); if (r) t.push(makeBundleMcpDiagnosticSentinel(r)); } return t; } function shouldReportBundleMcpRuntimeDiagnostic(e) { return applyFinalEffectiveToolPolicy({ bundledTools: collectBundleMcpDiagnosticSentinels(e), config: e.cfg, conversationCapabilityProfile: resolveConversationCapabilityProfile({ config: e.cfg, agentId: e.agentId, modelProvider: e.modelRef.provider, modelId: e.modelRef.model }), warn: () => {} }).length > 0; } async function collectRuntimeToolSchemaFindings(e, t = {}) { try { var n = _usingCtx(); const [{ captureRuntimeConfig: r }, { prepareDoctorToolSchemaFrames: i }, { collectAgentRuntimeToolSchemaFindings: a }] = await Promise.all([ import("./runtime-source-projection-ZXhcZEOj.mjs"), import("./doctor-tool-schema-frames-CbyAT0f0.mjs"), import("./doctor-tool-schema-projection-BvTWA3hc.mjs") ]); const o = r(e); const s = t.env ?? process.env; const c = n.a(new AsyncDisposableStack()); const l = t.runWithPluginMetadataSnapshot ?? c.use((await import("./plugin-metadata-snapshot-scope-BqMk1x5X.mjs")).createDoctorPluginMetadataSnapshotScope({ env: s })).run; const { frames: u, findings: d } = await i(o, { ...t, env: s, runWithPluginMetadataSnapshot: l }); const f = isUpdateDoctorLintPass(s); const p = /* @__PURE__ */ new Set(); const m = /* @__PURE__ */ new Map(); const h = /* @__PURE__ */ new Map(); const g = /* @__PURE__ */ new Set(); const _ = /* @__PURE__ */ new Set(); const v = /* @__PURE__ */ new Set(); let y; try { if (u.length > 0) { try { const [{ acquirePluginToolInspectionRegistry: e }, { resolvePluginRuntimeLoadContext: t }] = await Promise.all([import("./tools-GMgCHJ6S.mjs"), import("./load-context.resolve--9aVrgKw.mjs")]); y = await l({ config: o }, () => e({ loadContext: t({ config: o, env: s }), runWithPluginMetadataSnapshot: l, scopes: u.map((e) => ({ context: { config: o, runtimeConfig: o, agentId: e.agentId, agentDir: e.agentDir, workspaceDir: e.workspaceDir }, env: s, allowGatewaySubagentBinding: true, toolAllowlist: appendRuntimePluginToolGrant(e.capabilityProfile.policy.explicitToolAllowlist, e.capabilityProfile.policy.runtimePluginToolGrant), toolDenylist: e.capabilityProfile.policy.explicitToolDenylist })) })); } catch (e) { d.push({ checkId: "core/doctor/runtime-tool-schemas", severity: "warning", message: "Runtime tool schema inspection could not prepare plugin registrations.", requirement: formatErrorMessage(e), fixHint: "Fix plugin loading errors, then rerun doctor to inspect active tools." }); return d; } for (const e of y.registry?.plugins ?? []) if (e.status === "error") d.push({ checkId: "core/doctor/runtime-tool-schemas", severity: "warning", message: `Plugin ${e.id} tool schemas were not inspected because registration failed.`, path: `plugins.entries.${e.id}`, target: e.id, requirement: e.error ?? "plugin-registration-failed", fixHint: "Fix or disable the plugin, then rerun doctor." }); } const e = y?.registry ?? createEmptyPluginRegistry(); for (const t of u) { const { agentId: n, agentDir: r, workspaceDir: i, modelRef: s, model: c } = t; const collectForAgent = async () => { d.push(...await withPluginRuntimeRegistryScope(y?.registry, () => a({ ...t, cfg: o, toolRegistry: e }))); if (!shouldCreateBundleMcpRuntimeForAttempt({ toolsEnabled: true })) return; const l = loadSessionMcpConfig({ workspaceDir: i, cfg: o, logDiagnostics: false }); if (f) { for (const e of Object.keys(l.loaded.mcpServers)) { if (p.has(e)) continue; p.add(e); d.push({ checkId: "core/doctor/runtime-tool-schemas", severity: "warning", message: `MCP server "${sanitizeTerminalText(e)}" was not started for update validation. Run \`openclaw doctor --lint --only core/doctor/runtime-tool-schemas\` after the update to inspect its tools.`, path: `mcp.servers.${e}` }); } return; } const u = assignSafeServerNames(Object.keys(l.loaded.mcpServers)); const { requesterScopedServerNames: b } = partitionMcpServersByConnectionScope(l.loaded.mcpServers); for (const e of b) { if (v.has(e)) continue; const t = { serverName: e, safeServerName: u.get(e) ?? e, launchSummary: "requester-scoped connection", message: "authenticated requester context required" }; if (shouldReportBundleMcpRuntimeDiagnostic({ cfg: o, agentId: n, modelRef: s, diagnostic: t })) { d.push(bundleMcpRequesterInspectionFinding(e)); v.add(e); } } const x = new Set(b); for (const [e, t] of Object.entries(l.loaded.mcpServers)) { if (x.has(e) || t.auth !== "oauth") continue; x.add(e); const r = { serverName: e, safeServerName: u.get(e) ?? e, launchSummary: "OAuth inspection deferred", message: "OAuth refresh requires durable credential ownership" }; if (!g.has(e) && shouldReportBundleMcpRuntimeDiagnostic({ cfg: o, agentId: n, modelRef: s, diagnostic: r })) { d.push({ checkId: "core/doctor/runtime-tool-schemas", severity: "info", message: `Configured MCP server "${e}" was not checked during read-only inspection because OAuth may rotate external credentials.`, path: `mcp.servers.${e}`, fixHint: "For configured servers, run `openclaw mcp probe <name>` against the serving configuration. Validate plugin-provided or agent-local MCP servers from an authenticated serving-agent turn so refreshed credentials persist with their owner." }); g.add(e); } } const S = loadSessionMcpConfig({ workspaceDir: i, cfg: o, logDiagnostics: false, excludeServerNames: x, safeServerNamesByServer: u }).fingerprint; if (!m.has(S) && !h.has(S)) try { const { createBundleMcpToolRuntime: e } = await import("./agent-bundle-mcp-tools-BrDJpSRu.mjs"); m.set(S, await e({ workspaceDir: i, agentDir: r, cfg: o, excludeServerNames: x, safeServerNamesByServer: u })); } catch (e) { h.set(S, bundleMcpRuntimeFailureFinding(e, "load")); } const C = h.get(S); if (C) { if (!_.has(S)) { d.push(C); _.add(S); } return; } const w = m.get(S); if (w) { const e = (w.diagnostics ?? []).filter((e) => shouldReportBundleMcpRuntimeDiagnostic({ cfg: o, agentId: n, modelRef: s, diagnostic: e })); for (const t of e) { if (g.has(t.serverName)) continue; d.push(bundleMcpRuntimeDiagnosticFinding(t)); g.add(t.serverName); } d.push(...await (async () => { const { collectNormalizedToolSchemaFindings: e } = await import("./doctor-tool-schema-projection-BvTWA3hc.mjs"); const t = applyFinalEffectiveToolPolicy({ bundledTools: w.tools, config: o, conversationCapabilityProfile: resolveConversationCapabilityProfile({ config: o, agentId: n, modelProvider: s.provider, modelId: s.model }), warn: () => {} }); return e({ cfg: o, agentId: n, workspaceDir: i, modelRef: s, model: c, tools: t, normalizationFailureFinding: (e) => bundleMcpRuntimeFailureFinding(e, "normalize") }); })()); } }; await l({ config: o, workspaceDir: i }, collectForAgent); } } finally { const e = await Promise.allSettled([...m.values()].map(async (e) => await e.dispose())); for (const t of e) if (t.status === "rejected") d.push({ checkId: "core/doctor/runtime-tool-schemas", severity: "error", message: "Configured MCP tool schema inspection could not confirm child-process cleanup.", path: "mcp.servers", requirement: formatErrorMessage(t.reason), fixHint: "Inspect or stop the configured MCP server processes, then rerun doctor." }); if (y && t.deferInspectionDisposal) { const e = y; c.defer(() => e.release()); const n = c.move(); let r = false; try { t.deferInspectionDisposal(() => n.disposeAsync()); r = true; } finally { if (!r) await n.disposeAsync(); } } else try { await y?.release(); } catch (e) { d.push({ checkId: "core/doctor/runtime-tool-schemas", severity: "warning", message: "Runtime tool schema inspection could not confirm plugin cleanup.", requirement: formatErrorMessage(e), fixHint: "Inspect the plugin cleanup error, then rerun doctor." }); } } return d; } catch (e) { n.e = e; } finally { await n.d(); } } //#endregion export { collectRuntimeToolSchemaFindings };