UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

75 lines (74 loc) • 7.39 kB
/** * What a stdio MCP server writes to its stderr while its connection is being established (`mcp.server_stderr`, audience operator) — * the reference's form (Claude Code 2.1.321 reads the child's piped `stderr` (cli321.js:314948) until the connect settles, then lets * it flow unread): the child's stderr is a pipe the engine reads from the moment the transport starts until the dial settles, and * drains unread from then on; never the host's own stderr. * * · One reading per dial, made where the transport is built (`buildTransport` in mcp.ts) and settled by the dial (`connectServer`): * connected, failed, or stopped. A connected or failed dial whose server wrote anything is told ONE notice, to the seat of the leg * that dialed (the library entrance hands none: the process's default seat, else the throat's one line writer); a dial its stop * ended (the run's abort; a pooled dial's stop, when the last leg waiting on it walks away) is told nothing — the reference skips its * log once its shutdown has begun — and a server that wrote nothing is told nothing. The pool's close stops no dial in the air: that * dial connects and tells its one notice. What the server writes after the dial settled is not read (the one place: {@link settle}). * · Judged before it is cut: what is read is held until {@link egressSafeCut} lets a run of whole lines out; that run is rendered * whole (a key block is withheld as one, however its lines and chunks fell), and only its rendering is kept: the notice carries the * latest lines of what was let out, at most {@link MCP_STDERR_NOTICE_LINES} lines and {@link MCP_STDERR_NOTICE_CHARS} characters, * each folded to one line. Every judgment reads the last {@link MCP_STDERR_CONTEXT_CHARS} characters judged before it (never shown * again; each run of white space kept as one character): a find that began there, or a label there whose value is here, is * withheld here. The dial's end is the end of the words: a key block not closed by then is withheld to the end. * · A cut head is never shown: it can end in half a credential no reading knows. So held past {@link MCP_STDERR_HOLD_CHARS} with * nothing to let out (a line that long, a block that does not end), all of it is withheld, not its head, and an over-long line is * withheld to its end; and at the dial's end a last line with no line break is shown only when the stream itself ended (the server * closed its stderr: those are its last words, whole) — a dial that settles with the stream still open may have cut that line * anywhere, so it is withheld and counted. A find the run leaves open (a key block's banner, its footer not yet read) keeps the line * it began on before the judged words, so what follows is still read as part of it until words are let out — the stream withholds * what a reading of the whole text would. A held text longer than {@link JUDGE_EVERY_STEP_UP_TO} is judged again only once it has * doubled (or crossed the bound). * · The account: how many lines the server wrote (not blank), how many the notice does not show, and how many of those were withheld * unshown (their characters swallowed by a credential's mark, past the bound, or a last line the dial cut) — so a withheld block * reads as withheld, not as a server that said nothing; and whether words were still arriving when the dial settled (`truncated`: * what the server wrote before it answered the handshake can still be on its way — in the pipe, or queued in the child — and the * reading waits for it at most {@link SETTLE_MS}). * · The dial's own credentials (the values its peer seat answered for it that pass the credential floor): the notice carries a line the * server wrote only when none of them stands in what it wrote — read as written, in each line it wrote folded to one line (before * the vocabulary rewrites it), and in its lines as they leave the notice throat, each by the sent-credential matcher. A hit in any of the three, or a list that cannot be read, tells the header * only: every line counted, every line withheld. The header's own words are the engine's and the host's, never read for them. * · Bounded work: the data callback only takes the words in; they are judged {@link STEP_CHARS} at a time on later turns of the event * loop, for about {@link SLICE_MS} a turn. Past {@link MCP_STDERR_BACKLOG_CHARS} read and not yet judged the stream is paused until * the judging catches up (words are never dropped unjudged: a dropped banner would let the key after it out). */ import { Readable } from "node:stream"; import type { StdioClientTransport } from "@modelcontextprotocol/client/stdio"; import { type EngineNotice } from "./engine-notice.js"; /** The most characters held unshown: past it, with nothing to let out, the held words are withheld whole. */ export declare const MCP_STDERR_HOLD_CHARS = 65536; /** The characters already judged that each judgment reads before what it judges. */ export declare const MCP_STDERR_CONTEXT_CHARS = 4096; /** The most characters, and the most lines, of the latest lines one notice carries (a longer last line is cut). */ export declare const MCP_STDERR_NOTICE_CHARS = 2048; export declare const MCP_STDERR_NOTICE_LINES = 32; /** The most characters read and not yet judged: past it the stream is paused until the judging catches up. */ export declare const MCP_STDERR_BACKLOG_CHARS = 262144; type NoticeSeat = ((notice: EngineNotice) => void) | undefined; /** How a dial ended: its connection is up, it failed, or its stop ended it (told nothing). */ export type McpStderrOutcome = "connected" | "failed" | "stopped"; /** One dial's reading of its server's stderr. */ export interface McpStderrReading { /** The dial's end: the words read so far are judged as the end of the words, the notice is told (see the module header), and the * stream is drained unread from here. Only the first call does anything; every call answers the same promise. */ settle(outcome: McpStderrOutcome): Promise<void>; /** No more words will come (the stream's end, close or error, or its source's error). */ end(): void; } /** * Read `transport`'s stderr ({@link readMcpServerStderr}) — before the transport starts. The SDK pipes the child's own stderr into * this stream when it starts the child (`source.pipe(stream)`) and listens on neither end of that pipe; the pipe hands the source to * this stream's `pipe` listeners, and from there the source's error ends the reading instead of the host process, and a source this * stream lets go of (unpiped: this stream was destroyed — before the source came, too) is drained, so a child still writing is never * stalled on a full pipe. */ export declare function followMcpServerStderr(transport: StdioClientTransport, server: string, seat: NoticeSeat, credentials: readonly string[]): McpStderrReading; /** Read what `stream` carries until the dial settles, and tell it as one `mcp.server_stderr` notice for `seat` (see the module header); * `credentials`: the ones this dial handed the server, read here, once, by the sent-credential doors' own reading of a list. */ export declare function readMcpServerStderr(stream: Readable, server: string, seat: NoticeSeat, credentials: readonly string[]): McpStderrReading; export {};