Skip to content

Stdio

Category: Mcp_client

Source: stdio.dart

Subprocess-backed JSON-RPC transport for MCP stdio servers.

Wraps Process.start with: • env hygiene — scrub the parent environment to a small allowlist so user secrets (OPENAI_API_KEY, AWS creds, .netrc) don't leak into every MCP server they install. Explicitly listed env: entries from the server config are added back. • clean shutdown — close() sends SIGTERM, waits 2s, then SIGKILL. • exposes the underlying [JsonRpcTransport] surface for [McpClient].

Orphan-prevention (PR_SET_PDEATHSIG on Linux, kqueue watchdog on macOS) is a deferred follow-up — for now we rely on the SIGTERM path during normal Glue shutdown.

Classes

McpStdioTransportSpawnError

Constructor

dart
const McpStdioTransportSpawnError(this.command, this.cause)

Properties

PropertyTypeDescription
commandString
causeObject

Methods

String toString()

McpStdioTransport

Properties

PropertyTypeDescription
incomingStream<JsonRpcMessage> get
pidint getThe OS PID of the spawned server. Useful for tests and diagnostics.
exitCodeFuture<int> getResolves to the child's exit code. Lets callers detect "the server died on its own" vs. "we shut it down".
incomingStream<JsonRpcMessage> get
pidint getThe OS PID of the spawned server. Useful for tests and diagnostics.
exitCodeFuture<int> getResolves to the child's exit code. Lets callers detect "the server died on its own" vs. "we shut it down".

Methods

`static Future<McpStdioTransport> spawn({
required String command,
List&lt;String&gt; args = const [],
Map&lt;String, String&gt; extraEnv = const {},
String? workingDirectory,
bool inheritFullEnv = false,

})`

Spawn a subprocess. The child receives only [_alwaysForwardedEnvKeys] plus any keys in [extraEnv] (which is the server config's env: block resolved through env-var interpolation).

Pass [inheritFullEnv] = true to opt out of scrubbing (matches Claude Desktop's behaviour) — should be rare and explicit.

void send(JsonRpcMessage message)
Future&lt;void&gt; close()

Functions

`Map<String, String> buildMcpStdioEnv(

Map<String, String> parent, Map<String, String> extra, )`

Builds the env the child subprocess sees: the always-forwarded allowlist from [parent] plus any [extra] keys from the server config. extra wins on conflict (the config is the authority).

Public for testing. Production code uses it via [McpStdioTransport.spawn].

Released under the MIT License.