UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

170 lines (169 loc) • 13.1 kB
/** * The MCP surface a run mounts, as a delegation tool reads it at spawn: per declared server, the tools of it that are mounted * right now (the wire name and the name the server itself knows the tool by), and the names of the aggregate tools no server * owns (the cross-server resource readers). Read off the run's LIVE roster, so a refresh that added or removed a server's * tools is what a child spawned after it inherits. * * The run fills it on `ToolExecuteContext.mountedMcp`; the delegation tool puts the same universe through the child's lists * and declares the child the servers they leave. */ import { type McpServerSpec } from "./mcp-server-spec.js"; import type { ToolContentOrigin } from "./tool-spec.js"; import type { McpHostSeat } from "./mcp-host-seat.js"; import type { McpInstructionsEntry } from "./prompt-section-seat.js"; /** * The reserved names of a server's `allowTools`: each names a FACE of the server — a kind of read the engine makes of it — and never a * tool. `list_resources` / `read_resource` open the server to the cross-server resource readers (the model's resource tools and the host's * `listResources` / `readResource`), `list_resource_templates` to the host's template listing, `list_prompts` / `get_prompt` to the host's * prompt ports. The one law, read by {@link faceAllowed}: no list ⇒ every face open; a list (even `[]`) ⇒ only the faces it names. Every * other string in the list is a tool name. A server's own tool listed under one of these names is not mounted (the intake's drop lane says * so on both faces), and a `toolAxes` / `toolFaces` declaration keyed by one is refused at the entry's judge: the key could never name a tool. */ export declare const MCP_RESOURCE_LIST_ALLOW = "list_resources"; export declare const MCP_RESOURCE_READ_ALLOW = "read_resource"; export declare const MCP_RESOURCE_TEMPLATE_LIST_ALLOW = "list_resource_templates"; export declare const MCP_PROMPT_LIST_ALLOW = "list_prompts"; export declare const MCP_PROMPT_GET_ALLOW = "get_prompt"; export declare const MCP_FACE_WORDS: readonly ["list_resources", "read_resource", "list_resource_templates", "list_prompts", "get_prompt"]; export type McpFaceWord = (typeof MCP_FACE_WORDS)[number]; /** Is `name` one of the reserved face words ({@link MCP_FACE_WORDS})? Such a name is never a tool's. */ export declare function isMcpFaceWord(name: unknown): name is McpFaceWord; /** The one judge of whether a server's `allowTools` opens `face` ({@link MCP_FACE_WORDS}): absent ⇒ open; present ⇒ open iff it names it. */ export declare function faceAllowed(allowTools: readonly string[] | undefined, face: McpFaceWord): boolean; /** The drop reason of a server tool listed under a reserved face word ({@link MCP_FACE_WORDS}). */ export declare const MCP_FACE_WORD_DROP_REASON = "its name is one of the reserved MCP face words an allowTools list uses for a kind of read (list_resources, read_resource, list_resource_templates, list_prompts, get_prompt), never a tool, so the tool is not mounted"; /** The canonical names of the resource tools (the trio: list-all / read-one / list-under-a-directory). They belong to no one server: each aggregates the resources of every connected server that advertises them. */ export declare const LIST_MCP_RESOURCES = "ListMcpResourcesTool"; export declare const READ_MCP_RESOURCE = "ReadMcpResourceTool"; export declare const READ_MCP_RESOURCE_DIR = "ReadMcpResourceDirTool"; export declare const MCP_RESOURCE_FACES: readonly string[]; /** Does the valve (`leaves`: its one predicate) leave at least one of the cross-server resource tools? While it does, a connected server may contribute to them whatever it lists. */ export declare function resourceFacesLeft(leaves: (tool: { readonly name: string; }) => boolean): boolean; /** * Can the valve leave anything a declared MCP server could bring — a tool of its prefix domain, or a cross-server resource face * (those aggregate every connected server's resources, known only after the dial)? Given the valve's two readings (`leaves` per * tool, `leavesPeer` per prefix). The one answer to "dial it at the leg's start" and to "may the leg's face still gain a tool * from it" — a server answered `false` is not dialed, and a host's re-dial pushes its listing through the same valve. */ export declare function mcpServerReached(leaves: (tool: { readonly name: string; }) => boolean, leavesPeer: (prefix: string) => boolean): (serverName: string) => boolean; /** * Are a server's instructions about something the run holds? Yes when the run's roster (`held`) holds a tool of the server's prefix * domain the valve leaves, or a cross-server resource tool the valve leaves (the aggregates exist only if some connected server * advertised resources, and which one is not recorded, so a held face keeps the instructions of every server); a server whose own * listing (`listed` — any superset of it) has no tool at all (instructions only) is taught while any resource face is left. Read off * the roster, a tool the deployment's `toolFaces` overlay refused counts as what it is — not held. A server the run holds nothing of * is not taught: its instructions describe tools the model will not hold. * * The one answer to "should the model be taught them": the initial instructions block and the delta lane's adds after a roster change read * it. Whether the model HOLDS them is the ledger's ({@link instructionsTold}), which the disconnect line reads. */ export declare function instructionsTaught(leaves: (tool: { readonly name: string; readonly aliases?: readonly string[]; }) => boolean, serverName: string, held: ReadonlyArray<{ readonly name: string; readonly aliases?: readonly string[]; }>, listed: ReadonlyArray<{ readonly name: string; }>): boolean; /** A materialization's instructions delta lane (`MaterializedMcp.instructionsDelta`): what the run drains at its next boundary. */ export interface InstructionsLane { pendingAdds: McpInstructionsEntry[]; pendingRemovals: string[]; } /** Are this server's instructions in the model's context now? */ export declare function instructionsTold(lane: InstructionsLane, server: string): boolean; /** * THE judge of the add lane: is this connection's entry the text the model holds, or is about to hold (an unread copy of it is queued)? A server never * told, one whose disconnect line is queued (the model is being told the text no longer applies), and one holding or awaiting another text or author * are all "no". Every caller that would queue an entry — a re-dial's reconcile and a roster change's settle — asks it through {@link tellInstructions}. */ export declare function instructionsUnchanged(lane: InstructionsLane, entry: McpInstructionsEntry): boolean; /** A dial's default: the server's instructions are taught (a materialization alone takes every entry it returns as taught, until a run states its block). */ export declare function toldOnDial(lane: InstructionsLane, entry: McpInstructionsEntry): void; /** The run's start: the stable block (`taught`) is what the model is told. A removal already queued for a server outside the block names instructions the * model never held, so it is withdrawn; a server inside the block with a removal queued is not told (the frame will retract it), the block's text is * kept for a re-dial that withdraws the removal. */ export declare function toldAtStart(lane: InstructionsLane, taught: ReadonlyArray<McpInstructionsEntry>): void; /** Take this server's unread adds off the lane; a first copy that never reached the model leaves the ledger with it, a supersede leaves the model the text it holds. */ export declare function withdrawInstructionAdds(lane: InstructionsLane, server: string): void; /** Queue this server's current text unless the model holds (or is about to hold) exactly it. A different unread copy is superseded: the newest text * is the one queued, and when the newest is the text the model already holds, the stale unread copy is simply withdrawn. */ export declare function tellInstructions(lane: InstructionsLane, entry: McpInstructionsEntry): void; /** The server's connection is gone: unread adds are withdrawn, and the model is told its instructions no longer apply only if it holds them * (the text stays on the ledger until that line is delivered, for a re-dial that withdraws it). */ export declare function retractInstructions(lane: InstructionsLane, server: string): void; /** A re-dial that connected: a retraction still unread never reached the model, so it is withdrawn and the text the model holds stands. */ export declare function unretractInstructions(lane: InstructionsLane, server: string): void; /** One declared MCP server and its mounted tools. */ export interface MountedMcpServer { readonly server: McpServerSpec; readonly tools: ReadonlyArray<{ readonly name: string; readonly remoteName: string; }>; } /** What a run mounts of MCP: its servers, and the aggregate tool names that belong to none of them. */ export interface MountedMcp { readonly servers: ReadonlyArray<MountedMcpServer>; readonly aggregate: ReadonlyArray<string>; /** The seats of the host-transport servers this leg declares ({@link hostSeatsAlong}): a child that inherits one, or an agent this * leg wakes whose own list holds one, is handed that server's seat. A seat the leg holds for a server it does not declare is not here. */ readonly hostSeats?: ReadonlyMap<string, McpHostSeat>; } /** THE seat judge: of `held`, the seats of the host-transport servers among `servers`, by declaration name — `undefined` when none. A * seat follows a declaration; the mount (the leg's own servers), a spawn (the servers a child inherits) and a wake (the woken leg's own * list, read against the waker's mount) all read it. */ export declare function hostSeatsAlong(held: ReadonlyMap<string, McpHostSeat> | undefined, servers: ReadonlyArray<McpServerSpec>): ReadonlyMap<string, McpHostSeat> | undefined; /** A roster entry as far as this reading goes. */ interface RosterEntry { readonly name: string; readonly mcpRemoteName?: string; } /** * The names among `names` that belong to the declared server `serverName`: those under its prefix domain (`mcp__<server>__`), which * is the roster's own definition of a server's tools (the live roster swaps a server's tools by it). Handed a roster's names, it is * the ONE answer to "how many tools of this server does the run hold" — the re-dial's receipt and notice, the content-class * declaration line, the wiring manifest's per-server count and {@link mountedMcpOf} all read it, so no two of them can count * differently. Handed a server's own listing it answers what the server offers, which is not a mount count. */ export declare function toolNamesOfServer(names: Iterable<string>, serverName: string): string[]; /** * The tool cells of a connected server's manifest row, off ONE read of the roster: `toolCount` is how many names {@link toolNamesOfServer} * answers, `toolNames` the first `max` of them that are wire names (in the roster's order) — present iff there is one — and * `toolNamesTruncated: true` iff the list is short of the count (over `max`, or a held name that is not listable); never `false`. A wire * name is the provider's tool-name charset and length — the fixed point of the mint's own fold ({@link normalizeNameSegment}), so there is * no second pattern to keep in step — and a held name outside it is counted and not listed, because a roster is host-fed and a listing is * read by a person. */ export declare function toolFieldsOfServer(rosterNames: Iterable<string>, serverName: string, max: number): { toolCount: number; toolNames?: string[]; toolNamesTruncated?: true; }; /** * Read the run's MCP surface off its roster. `declared` is the run's frozen server declarations; `aggregateNames` the names of the * tools its materialization minted that no server owns. A tool is a server's when its wire name sits under the server's prefix * domain and carries the remote name the mint recorded on it; a tool of a server that failed to connect is simply not mounted, * so that server has no tools here and a child is not declared it. */ export declare function mountedMcpOf(input: { readonly declared: ReadonlyArray<McpServerSpec> | undefined; readonly roster: ReadonlyArray<RosterEntry>; readonly aggregateNames: ReadonlySet<string>; readonly hostSeats?: ReadonlyMap<string, McpHostSeat>; }): MountedMcp; /** * The MCP tools of a mount as pool rows (name and the server's declared content class) — what the memory content-origin face adds to * a delegation tool's pool when its children inherit the mount. The rows are the universe the spawn's lists are read over, so * "can the child reach an MCP tool" has one answer. */ export declare function mountedMcpPoolRows(mounted: MountedMcp): Array<{ name: string; contentOrigin?: ToolContentOrigin; }>; export {};