UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

52 lines (51 loc) • 4.74 kB
/** * The read bound of a stdio MCP connection and what it means when a reply outgrows it. The client package reads a stdio server's * output into one buffer; when the buffer for ONE message passes its bound it empties the buffer, reports an error to the transport's * `onerror`, and closes the connection — the request that was waiting then fails as a closed connection, which says nothing of why. The * engine dials stdio with a bound it states itself ({@link MCP_STDIO_READ_BOUND}) and watches the transport's `onerror` for that * one report, so the call that was waiting can be told what happened: the server answered, the reply was too big to read. * * The client package reports the overflow as a plain `Error` whose text states the bound (`ReadBuffer.append`, the client package's * shared stdio module) and nothing else — no code, no size of the reply. That text is the one word this module reads from the package; * a package that words it differently leaves the engine on its old, conservative reading (a closed connection, outcome unknown), never * on a certain one, and the pin that runs a real child over the bound fails on the package upgrade that does it. */ import { type Transport } from "@modelcontextprotocol/client"; /** * The most bytes the engine reads for ONE message from a stdio MCP server (newline included): 10 MiB, the client package's own bound as it * stands, passed explicitly so the engine knows the number and says it, and so it does not move when the package's default does. It is * not higher because the package grows its buffer by copying it on every chunk — a reply costs about the square of its length in time and * memory (docs/KNOWN-LIMITS.md has the measured figures) — so a larger bound turns a lost connection into a stalled, memory-hungry host. */ export declare const MCP_STDIO_READ_BOUND: number; /** A reply from a stdio server that outgrew the connection's read bound: the server answered, the client did not read it and closed the * connection. Thrown in place of the closed connection the call would otherwise fail as; `mcp-failure.ts` classifies it. */ export declare class McpReplyOverReadBound extends Error { readonly bound: number; constructor(bound: number, cause?: unknown); } /** What a call whose reply outgrew the bound reads: the server answered, the client did not read it, the call may have run. The size of the * reply is not in it — the package reports none. `writeWarning` is the caller's write-capable sentence (empty for a read). */ export declare function overReadBoundSentence(what: string, serverLabel: string, bound: number, writeWarning: string): string; /** The clause a lost-connection line adds when the connection closed on a reply over the bound that cannot be named as this call's own. */ export declare function readBoundWhy(bound: number): string; /** One connection's overflow: the bound it was dialed with, and whether the request whose reply it was can be named. The package reports no * message, so the only evidence is which requests the connection had sent and not yet had answered: with at most one (`sole`), every * request that failed with the connection is that one and the reply that outgrew the bound was its answer. A request that timed out or was * cancelled stays unanswered here — the server may still send its reply later — so it keeps a later request from being named (one small * id per such request, for the life of the connection). With several unanswered the reply that outgrew the bound is one of theirs and the * transport cannot say which. (A message that is no reply — a server's own notification or request, a line of output that is not a message * — over the bound is not told apart from one: with a single request unanswered it reads as that request's reply.) */ export interface ReadBoundBreach { readonly bound: number; readonly sole: boolean; } /** * Watch `transport` (a stdio transport dialed with `bound`, not yet connected) for the overflow report, and record it against `client`, * the connection it will serve. The client package chains the callbacks a transport already holds when it connects, so the two this sets * (`onerror`, `onmessage`) run ahead of the client's own. The requests the connection has sent and not had answered are kept here, at the one * place every request of every kind (a call, a resource read, a listing, a refresh) and every holder of a pooled connection passes through. */ export declare function watchReadBound(client: object, transport: Transport, bound: number): void; /** The overflow `client`'s connection met, if it did. */ export declare function readBoundBreachOf(client: object): ReadBoundBreach | undefined;