spawn static method

Future<StdioTransport> spawn({
  1. required String executable,
  2. List<String> arguments = const <String>[],
  3. String? workingDirectory,
  4. Map<String, String> environment = const <String, String>{},
  5. bool includeParentEnvironment = true,
  6. String? name,
})

Starts executable and connects to it.

environment is merged over the parent's unless includeParentEnvironment is false. Merging is the default because MCP servers routinely need PATH, and a server that cannot find its own runtime fails in a way that looks like a protocol problem.

Implementation

static Future<StdioTransport> spawn({
  required String executable,
  List<String> arguments = const <String>[],
  String? workingDirectory,
  Map<String, String> environment = const <String, String>{},
  bool includeParentEnvironment = true,
  String? name,
}) async {
  final Process process;
  try {
    process = await Process.start(
      executable,
      arguments,
      workingDirectory: workingDirectory,
      environment: environment.isEmpty ? null : environment,
      includeParentEnvironment: includeParentEnvironment,
    );
  } on ProcessException catch (error, stackTrace) {
    throw ConfigurationException(
      'Could not start the MCP server `$executable`: ${error.message}. '
      'Check that it is installed and on PATH.',
      setting: 'executable',
      cause: error,
      causeStackTrace: stackTrace,
    );
  }
  return StdioTransport._(process, name ?? 'stdio:$executable');
}