@sema-agent/core
Version:
Stateless, task-oriented AI agent core
52 lines (51 loc) • 3.31 kB
JavaScript
import { entriesNotAppliedDetail } from "./entries-not-applied.js";
export const MCP_INJECTION_DROP_REASONS = Object.freeze([
"malformed_entry",
"name_reserved_by_deployment",
"gate_closed",
"over_cap",
"key_retired",
"credentials_not_carried",
"name_already_mounted",
]);
export const MCP_INJECTION_DROP_TEXT = {
malformed_entry: "its declaration could not be read — fix the entry and start a new session",
name_reserved_by_deployment: "the deployment mounts its own server of that name, and that server is the one in use — keep using it under this name; rename this entry only to add a second, different server",
gate_closed: "this deployment does not accept MCP servers from the request on this leg — ask the operator, or use the deployment's servers",
over_cap: "the request named more MCP servers than this deployment mounts per leg — trim the list",
key_retired: "its declaration carries a configuration key this service no longer accepts — update the application that sent this entry, or remove that key from the entry, then start a new session",
credentials_not_carried: "requests do not carry credentials for MCP servers, so the deployment has to mount this one — ask an administrator to configure it in the deployment, or remove the credentials from this entry",
name_already_mounted: "another server of that name is already mounted on this run and stays in use — rename the server in this agent's configuration, or remove the duplicate",
};
const SENTENCE_NAMES_FIELD = {
malformed_entry: true,
name_reserved_by_deployment: false,
gate_closed: false,
over_cap: false,
key_retired: true,
credentials_not_carried: true,
name_already_mounted: false,
};
const isReason = (x) => typeof x === "string" && MCP_INJECTION_DROP_REASONS.includes(x);
export function mcpInjectionDroppedNotice(facts) {
const { sessionId, server, reason, field, agent } = facts;
if (typeof sessionId !== "string" || sessionId.length === 0) {
throw new TypeError("mcpInjectionDroppedNotice: sessionId is required — a user-audience notice with no session routes nowhere");
}
if (typeof server !== "string" || server.length === 0) {
throw new TypeError("mcpInjectionDroppedNotice: server name is required");
}
if (!isReason(reason)) {
throw new TypeError(`mcpInjectionDroppedNotice: reason ${JSON.stringify(reason)} is not one of ${MCP_INJECTION_DROP_REASONS.join("/")}`);
}
const fieldSeat = typeof field === "string" && field.length > 0 ? { field } : {};
const agentName = typeof agent === "string" && agent.length > 0 ? agent : undefined;
const agentSeat = agentName !== undefined ? { agent: agentName } : {};
const where = SENTENCE_NAMES_FIELD[reason] && typeof field === "string" && field.length > 0 ? ` (field \`${field}\`)` : "";
const source = agentName !== undefined ? `declared by agent "${agentName}"` : "from this request";
return {
code: "config.entries_not_applied",
message: `MCP server "${server}" ${source} was not mounted: ${MCP_INJECTION_DROP_TEXT[reason]}${where}.`,
detail: { ...entriesNotAppliedDetail("mcp_injection_dropped", [{ entry: server, reason, ...fieldSeat, ...agentSeat }]), sessionId },
};
}