UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

71 lines (70 loc) • 3.5 kB
import { deliverEngineNotice } from "./engine-notice.js"; import { eraseSentCredentials, scrubRequestEcho } from "./protocol-http-auth.js"; import { namesASecret } from "./secret-env.js"; import { inlineUntrusted } from "./untrusted-text.js"; export const CREDENTIAL_ECHO_SITES = Object.freeze(["server_info", "instructions", "tool_listing", "prompt_listing", "resource_listing", "agent_card"]); export const NO_CREDENTIAL_ECHO = Object.freeze({ credentials: Object.freeze([]), announce: () => undefined }); export const CREDENTIAL_ECHO_DROP_REASON = "its name or input schema holds a credential this connection's requests carried (the server echoed it back); neither is rewritten, so the tool is not mounted"; export function erasedEcho(text, echo, where) { if (typeof text !== "string") return text; const erased = eraseSentCredentials(text, echo.credentials); if (erased !== text) echo.announce(where); return erased; } export function nameEchoes(name, echo, where) { const echoed = typeof name === "string" && eraseSentCredentials(name, echo.credentials) !== name; if (echoed) echo.announce(where); return echoed; } export function schemaText(schema) { try { return JSON.stringify(schema) ?? ""; } catch { return ""; } } export function echoedToolDrop(name, inputSchema, echo, declared) { const carried = declared.filter((v) => !/^[ \t]*$/.test(v)); const namedWithCarried = carried.some((v) => name === v || (namesASecret(v) && name.includes(v))); if (namedWithCarried) echo.announce("tool_listing"); else if (!nameEchoes(name, echo, "tool_listing") && !nameEchoes(schemaText(inputSchema), echo, "tool_listing")) return undefined; return { tool: inlineUntrusted(scrubRequestEcho(eraseSentCredentials(name, echo.credentials), carried)), reason: CREDENTIAL_ECHO_DROP_REASON }; } export function carrierEcho(carrier, announce) { return { credentials: carrier.credentials, announce: announce ?? NO_CREDENTIAL_ECHO.announce }; } export function serverInfoOf(info, echo) { if (info === undefined) return undefined; if (nameEchoes(info.name, echo, "server_info")) return undefined; return { name: info.name, version: erasedEcho(info.version, echo, "server_info") ?? info.version }; } export function echoAnnouncer(onCredentialEchoed) { const announced = new Set(); return (peer, where) => { const key = `${peer}\u0000${where}`; if (onCredentialEchoed === undefined || announced.has(key)) return; announced.add(key); try { onCredentialEchoed(peer, where); } catch { } }; } const SITE_WORDS = { server_info: "serverInfo", instructions: "instructions", tool_listing: "tool listing", prompt_listing: "prompt listing", resource_listing: "resource listing", agent_card: "agent card" }; export function credentialEchoNotice(onNotice, sessionId, runId, protocol) { return (server, where) => deliverEngineNotice(onNotice, { code: "mcp.credential_echoed", message: `${protocol === "mcp" ? "MCP server" : "A2A peer"} "${inlineUntrusted(server, 120)}" echoed a credential its requests carried into its ${SITE_WORDS[where]} — the engine erased it there (a name holding it was refused, not rewritten); the endpoint repeats what it is sent, so check what it logs and publishes.`, detail: { protocol, server, where, sessionId, runId }, }); }