UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

141 lines (140 loc) • 11.5 kB
/** * The host seat: the source of the MCP pipe the HOST supplies for one leg, mounted by a declaration whose transport is `{ kind: "host" }` * ({@link import("./mcp-server-spec.js").McpServerSpec}). The declaration is data (it rides `TaskSpec.mcp`, the wiring manifest and a * resume config like any other entry); the seat is a live object and rides the trusted channel only (`RunInternals.mcpHostSeats`, keyed * by the declaration's `name`), never a spec, a checkpoint or a retained child's snapshot. * * A seat hands one MCP SDK `Transport` per dial ({@link McpHostSeat.open}); the server behind it answers the whole protocol itself — its * handshake, its instructions, its resources — and goes through the one MCP pipeline every server does (the dial, the listing intake, * the call lane, the failure record), so its tool names, its listed `_meta` and its call `_meta` are what a stdio or http server's would * be. Its server is the host's own code: it is not confined by the engine's sandbox, and what it reaches is not judged by the engine's * host floor (that floor judges an http declaration's URL). * * THE PIPE'S OWNERSHIP — one rule, four checkpoints, all at the dial ({@link openHostPipe}, {@link hostMount}): * · one open per dial: every dial (the first, a re-dial, a child's, a resumed leg's) asks the seat once; no pipe is kept across dials; * · one deadline over the whole mount — the open, the pipe's `start`, the handshake, the first listing — raced against the dial's stop: * the host's pipe can hold any of those steps, where the SDK's own timer covers the requests only. At the deadline the dial fails * `timeout`, and the engine closes the pipe (one that arrives later too) without waiting on the host; * · a pipe is taken once, at its arrival: one handed again — to another dial, or to this one after it stopped waiting — is refused * and left alone (it is its first taker's); a late pipe nobody took is taken and closed; * · a pipe is a transport: `start` / `send` / `close` functions, and no `sessionId` (the SDK skips the handshake on a pipe that holds one, * and the server's capabilities and instructions would never be read). * A pipe taken is the dial's: the engine connects it and the engine closes it — at once, for the engine: the connection reads closed when * the engine closes it, and the host's own close runs on the host's time, never waited on, its failure nobody's (`TakenPipe`). A refusal is * `host_refused` (nothing was sent), in the engine's own words. * * WHAT THE HOST CAN SAY — one class, {@link McpHostSeatFailure}, read by one classifier row wherever it surfaces: thrown by `open` (the * dial fails, nothing was sent), or the rejection of the pipe's `send` of a request (that request fails). A request settles ONCE, by * whichever comes first: the server's answer, its send's rejection, the engine's cancel (its abort, its timer, the idle watchdog), the * pipe's close — a host that closes the pipe and then rejects a send reads as the closed connection. A cancellation the pipe fails to * send is no request's ending (the cancelled request already ended as cancelled): cancelling a call on the host's server is best effort. * * A host that serves requests rather than messages hands {@link hostSeatOfRequests} of its {@link McpHostRequestSeat}: the one adapter * answers the handshake itself, carries `tools/list` and `tools/call` only, and hands back the seat's failures through the same `send` * rejection (see {@link hostSeatOfRequests}). */ import { type Transport } from "@modelcontextprotocol/client"; /** * The host's pipe source for one leg (`RunInternals.mcpHostSeats`, TRUSTED, by declaration name). `open` is asked once per dial of that * declaration and hands one transport (or a promise of one); `server` is the declaration's name, `signal` aborts when the engine stops * waiting for this mount (the run's abort, the mount's deadline). What `open` hands is the dial's from its arrival (see the module note): * a transport handed a second time is refused. `open` throws {@link McpHostSeatFailure} to say why there is no pipe; anything else it * throws refuses the dial in the engine's words. The engine never disposes a seat: the host releases it when its leg ends. */ export interface McpHostSeat { open(at: { readonly server: string; readonly signal: AbortSignal; }): Transport | Promise<Transport>; } /** The requests a request-level host seat answers: a listing and a call. */ export type McpHostSeatMethod = "tools/list" | "tools/call"; /** A request-level host seat's answer to one request: the JSON-RPC result, or the JSON-RPC error the remote answered with. */ export type McpHostSeatAnswer = { readonly result: unknown; } | { readonly error: { readonly code: number; readonly message: string; readonly data?: unknown; }; }; /** * A host's seat that answers requests rather than messages — handed to the engine as {@link hostSeatOfRequests}`(seat)`. `request` * answers one request; `signal` is that request's own cancellation. A request the host could not hand to the remote, or whose ending it * cannot tell, is answered by throwing {@link McpHostSeatFailure}. */ export interface McpHostRequestSeat { request(method: McpHostSeatMethod, params: Readonly<Record<string, unknown>>, signal: AbortSignal): Promise<McpHostSeatAnswer>; } /** * What a host says it could not carry, in the one fact the host knows: `delivered: "no"` — nothing was handed to the remote (no pipe was * handed over, or the request was not sent; the failure word is `host_refused`); `delivered: "unknown"` — the request was handed on and * the host cannot tell how it ended (the failure word is `unknown`; at the dial, nothing of the caller's was sent either way). * `condition` is the host's own sentence about why, read as untrusted text and bounded here; the engine adds what the verdict owes the * model (retry safety, the verify-first warning of a write-capable tool) itself, so the condition carries none of it. A value of this * class whose two facts do not read as they were built is no verdict: it reads as any other thrown value does. */ export declare class McpHostSeatFailure extends Error { readonly delivered: "no" | "unknown"; readonly condition: string; constructor(o: { delivered: "no" | "unknown"; condition: string; }); } /** The host's verdict a failure states: the delivery fact and the sentence a failure renders. */ export interface HostSeatVerdict { readonly delivered: "no" | "unknown"; readonly condition: string; } /** The host's verdict a thrown value states — a {@link McpHostSeatFailure} whose two facts read as they were built, read once into the * engine's own copy (the condition bounded again: re-bounding a bounded sentence leaves it as it is) — else `undefined`, also for a value * whose inspection throws. Structure only: a remote's JSON cannot build one, and its shape is no verdict. */ export declare function hostSeatVerdictOf(err: unknown): HostSeatVerdict | undefined; /** The sentence a host's verdict becomes on the call lane: who failed and why, then the tail the delivery fact owes. */ export declare function hostSeatLine(what: string, serverLabel: string, seat: HostSeatVerdict, writeTail: string): string; /** A host-seat declaration whose leg holds no seat of its name: refused at its dial (`config.mcp_host_seat_missing`), nothing sent. */ export declare function hostSeatMissing(server: string): Error & { code: string; }; export declare const MCP_HOST_SEAT_MISSING = "config.mcp_host_seat_missing"; /** The same refusal as the waker reads it: the woken leg's host-transport servers (`servers`, inherited at its spawn) that this wake hands * no seat, so each fails its dial there with {@link hostSeatMissing}. It states only what the wake knows — the server is a host pipe on * the woken side and this leg handed no seat for it — never why this leg holds none (it may not declare the server, declare it with * another transport, declare it without holding a seat, or run outside a Runner). */ export declare function hostSeatNotHandedLine(servers: readonly string[]): string; /** * One host row's mount under ONE deadline (the module note): `signal` aborts when `ms` (the row's startup bound; absent, the SDK's own * request default) has passed since the mount began — a `RequestTimeout`, which the dial reads as `timeout` — or when `run` (the run's * abort) fires, with its reason. `end` stands both down once the first listing is in, or the dial failed. */ export declare function hostMount(ms: number | undefined, run: AbortSignal | undefined): { readonly signal: AbortSignal; end(): void; }; /** `work`, unless `stop` fires first: then the stop's reason, at once — whatever `work` does later is dropped (never an unhandled * rejection). Without a stop, `work` itself. A mount step on a host's pipe waits through here (the module note). */ export declare function settledBefore<T>(work: Promise<T>, stop: AbortSignal | undefined): Promise<T>; /** Whether a thrown value is a host seat's explicit refusal (`delivered: "no"`, `"refused"`) or a seat/pipe fault (`"fault"`), or neither * (an unmarked `delivered: "unknown"` verdict falls back to generic unavailable wording). */ export declare function hostSeatFailureOrigin(err: unknown): "refused" | "fault" | undefined; /** * The dial's pipe from the leg's seat (the module note's four checkpoints): `open` asked once, raced against `stop` (the mount's signal); * what arrives is taken, then judged a transport. Throws the stop's reason when the dial stopped waiting (a pipe that arrives later is * taken and closed, unless another dial took it), the host's own {@link McpHostSeatFailure} when `open` threw one, and the engine's * refusal for everything else (an `open` that threw anything else or is not a function, a value that is no transport, a pipe already taken). */ export declare function openHostPipe(seat: McpHostSeat, server: string, stop: AbortSignal | undefined): Promise<Transport>; /** * The one adapter from a request-level seat to a pipe source: each `open` hands a fresh pipe over `seat`. What the pipe does itself: * · answers `initialize` locally — the server's name is the dial's declaration name, its version the empty string (a host-seat declaration * states no version), its capabilities `tools` only, no instructions — and swallows `notifications/initialized`; * · hands `tools/list` and `tools/call` to the seat, one {@link McpHostRequestSeat.request} each, with a signal of the request's own — * aborted when the engine cancels that request (its abort, its timeout, the idle watchdog) and when the pipe closes; * · refuses every other method locally (the request is not handed on), and drops an answer that arrives after its request was cancelled. * The seat's answer reaches the SDK as the JSON-RPC response it is; the seat's failure — its {@link McpHostSeatFailure}, anything else it * throws (read as `unknown`, in the engine's words, never the thrown text), an answer the protocol cannot carry — as the rejection of that * request's `send`, which the SDK hands to the waiting request as it is, clearing its handlers. */ export declare function hostSeatOfRequests(seat: McpHostRequestSeat): McpHostSeat;