UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

116 lines (115 loc) • 7.41 kB
import { endNoticeFact } from "./engine-notice.js"; import { hopRefusalKindOf } from "./host-floor.js"; import { owesAuthorization } from "./mcp-failure.js"; import { hostSeatFailureOrigin, MCP_HOST_SEAT_MISSING } from "./mcp-host-seat.js"; import { readMember } from "./thrown-value.js"; import { inlineUntrusted } from "./untrusted-text.js"; export const MCP_SERVER_UNAVAILABLE = "mcp.server_unavailable"; const UNAVAILABLE_CAUSE = Symbol(); export function mcpUnavailableCauseOf(err) { const seatOrigin = hostSeatFailureOrigin(err); if (seatOrigin === "refused") return "host_seat_refused"; if (seatOrigin === "fault") return "host_seat_fault"; const hopKind = hopRefusalKindOf(err); if (hopKind === "host_floor") return "host_floor_refused"; if (hopKind === "unsupported_scheme") return "unsupported_url_scheme"; if (err instanceof Error && readMember(err, "code") === MCP_HOST_SEAT_MISSING) return "host_seat_missing"; return undefined; } export function attachUnavailableCause(warning, err) { if (warning.code !== MCP_SERVER_UNAVAILABLE) return warning; const cause = mcpUnavailableCauseOf(err); if (cause !== undefined) Object.defineProperty(warning, UNAVAILABLE_CAUSE, { value: cause }); return warning; } function unavailableCauseOfWarning(warning) { const cause = warning[UNAVAILABLE_CAUSE]; return cause === "host_seat_refused" || cause === "host_seat_fault" || cause === "host_seat_missing" || cause === "host_floor_refused" || cause === "unsupported_url_scheme" ? cause : undefined; } function unavailableMessage(name, memory, cause) { if (memory === "kept") { return (`MCP server "${name}" did not answer within its startup timeout, so it was skipped — its tools are not available to the assistant ` + `for the rest of this session, which goes on with the tools of the servers that did connect. It is not tried again unless its ` + `configuration changes or it is reconnected; if it is only slow to start, give it a longer startup timeout.`); } switch (cause) { case "host_seat_refused": return (`MCP server "${name}" was refused by the host when setting up its connection, so it was skipped — its tools are not available to the assistant until it connects, ` + `and the run goes on with the tools of the servers that did connect. Check the host application or ask the operator why it declined this server, ` + `then try again or reconnect once it is ready. This is said once per session: if the server connects and later fails again, it is said again.`); case "host_seat_fault": return (`MCP server "${name}" could not connect because the host's connection seat failed unexpectedly, so it was skipped — its tools are not available to the assistant until it connects, ` + `and the run goes on with the tools of the servers that did connect. Ask the operator or host maintainer to check the host seat logs and fix the integration before retrying. ` + `This is said once per session: if the server connects and later fails again, it is said again.`); case "host_seat_missing": return (`MCP server "${name}" requires a host-supplied connection, but no host seat was provided for it in this run, so it was skipped — its tools are not available to the assistant until it connects, ` + `and the run goes on with the tools of the servers that did connect. Ask the operator or host application to wire a host seat for this server, or remove it from the task configuration. ` + `This is said once per session: if the server connects and later fails again, it is said again.`); case "host_floor_refused": return (`MCP server "${name}" was blocked because its URL is not allowed by this deployment's host policy, so it was skipped — its tools are not available to the assistant until it connects, ` + `and the run goes on with the tools of the servers that did connect. Ask the operator or deployment administrator to allow this server's host in the deployment's host policy, ` + `or change the server to an allowed URL. This is said once per session: if the server connects and later fails again, it is said again.`); case "unsupported_url_scheme": return (`MCP server "${name}" has an unsupported URL protocol in its configuration, so it was skipped — its tools are not available to the assistant until it connects, ` + `and the run goes on with the tools of the servers that did connect. Ask the operator to fix this server's URL configuration to use a supported web address. ` + `This is said once per session: if the server connects and later fails again, it is said again.`); default: return (`MCP server "${name}" could not be reached, so it was skipped — its tools are not available to the assistant until it connects, ` + `and the run goes on with the tools of the servers that did connect. This is said once per session: if the server connects and ` + `later fails again, it is said again.`); } } export function mcpFailureRemembered(warnings) { const remembered = new Set(); for (const w of warnings) { const { code, server, memory } = w; if (code === MCP_SERVER_UNAVAILABLE && typeof server === "string" && memory !== undefined) remembered.add(server); } return (server) => remembered.has(server); } export function mcpServerUnavailableNotice(warning, statuses, leg) { const { code, server, memory } = warning; if (code !== MCP_SERVER_UNAVAILABLE || typeof server !== "string" || memory === "recalled") return undefined; if (statuses.some((s) => s.name === server && s.authorization !== undefined && owesAuthorization(s.authorization))) return undefined; const name = inlineUntrusted(server, 160); return { code: "mcp.server_unavailable", message: unavailableMessage(name, memory, unavailableCauseOfWarning(warning)), detail: { server, ...(memory === "kept" ? { memory } : {}), sessionId: leg.sessionId, runId: leg.runId }, }; } export function mcpServerConnectingNotice(server, waitedMs, boundMs, leg) { const seconds = (ms) => String(Math.round(ms / 100) / 10); return { code: "mcp.server_connecting", message: `MCP server "${inlineUntrusted(server, 160)}" is still connecting as this task starts — ${seconds(waitedMs)} s so far; its startup timeout is ${seconds(boundMs)} s.`, detail: { server, waitedMs, boundMs, sessionId: leg.sessionId, runId: leg.runId }, }; } export function endMcpServerUnavailable(told, statuses, warnings) { for (const s of statuses) { if (s.status === "connected" || s.authorization?.reason === "rejected" || s.authorization?.reason === "scope") endNoticeFact(told, MCP_SERVER_UNAVAILABLE, { server: s.name }); } for (const w of warnings) { const { code, server, memory } = w; if (code === MCP_SERVER_UNAVAILABLE && typeof server === "string" && memory === "kept") endNoticeFact(told, MCP_SERVER_UNAVAILABLE, { server }); } }