spawn static method

Future<IoMcpByteChannel> spawn({
  1. required String command,
  2. required List<String> args,
  3. required Map<String, String> env,
  4. required String cwd,
})

Spawns command with args in cwd with extra environment env. Throws McpServerUnavailableException when the executable cannot be started (e.g. not on PATH) so the server lands in failed status instead of crashing the session.

Implementation

static Future<IoMcpByteChannel> spawn({
  required String command,
  required List<String> args,
  required Map<String, String> env,
  required String cwd,
}) async {
  final Process process;
  try {
    process = await Process.start(
      command,
      args,
      workingDirectory: cwd,
      environment: env,
      mode: ProcessStartMode.normal,
    );
  } on Object catch (error) {
    throw McpServerUnavailableException(
      'cannot start MCP server `$command`: $error. '
      'Is it installed and on PATH?',
      cause: error,
    );
  }
  // stderr is diagnostic noise for the human, not protocol: drain it so a
  // chatty server cannot fill the pipe buffer and wedge itself.
  unawaited(process.stderr.drain<void>());
  return IoMcpByteChannel._(process);
}