|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:mcp_dart/src/shared/transport.dart'; |
| 4 | +import 'package:mcp_dart/src/types.dart'; |
| 5 | + |
| 6 | +Never _unsupported() => throw UnsupportedError( |
| 7 | + 'StdioClientTransport is only available on Dart IO platforms.', |
| 8 | + ); |
| 9 | + |
| 10 | +/// Configuration parameters for launching a stdio server process. |
| 11 | +/// |
| 12 | +/// This web/default-platform stub preserves the public API shape without |
| 13 | +/// importing `dart:io`. The real implementation is selected on Dart IO |
| 14 | +/// platforms through the package barrel's conditional export. |
| 15 | +class StdioServerParameters { |
| 16 | + /// The executable command to run to start the server process. |
| 17 | + final String command; |
| 18 | + |
| 19 | + /// Command line arguments to pass to the executable. |
| 20 | + final List<String> args; |
| 21 | + |
| 22 | + /// Environment variables to use when spawning the process. |
| 23 | + final Map<String, String>? environment; |
| 24 | + |
| 25 | + /// How to handle the stderr stream of the child process on IO platforms. |
| 26 | + final Object? stderrMode; |
| 27 | + |
| 28 | + /// The working directory to use when spawning the process. |
| 29 | + final String? workingDirectory; |
| 30 | + |
| 31 | + /// Creates parameters for launching the stdio server. |
| 32 | + const StdioServerParameters({ |
| 33 | + required this.command, |
| 34 | + this.args = const [], |
| 35 | + this.environment, |
| 36 | + this.stderrMode, |
| 37 | + this.workingDirectory, |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +/// Stub for the stdio client transport on platforms without `dart:io`. |
| 42 | +class StdioClientTransport implements Transport { |
| 43 | + /// Creates a stdio client transport stub. |
| 44 | + StdioClientTransport(this.serverParams); |
| 45 | + |
| 46 | + /// Configuration for launching the server process. |
| 47 | + final StdioServerParameters serverParams; |
| 48 | + |
| 49 | + @override |
| 50 | + void Function()? onclose; |
| 51 | + |
| 52 | + @override |
| 53 | + void Function(Error error)? onerror; |
| 54 | + |
| 55 | + @override |
| 56 | + void Function(JsonRpcMessage message)? onmessage; |
| 57 | + |
| 58 | + @override |
| 59 | + String? get sessionId => null; |
| 60 | + |
| 61 | + /// Stderr is unavailable without a spawned process. |
| 62 | + Stream<List<int>>? get stderr => null; |
| 63 | + |
| 64 | + @override |
| 65 | + Future<void> start() async => _unsupported(); |
| 66 | + |
| 67 | + @override |
| 68 | + Future<void> close() async { |
| 69 | + onclose?.call(); |
| 70 | + } |
| 71 | + |
| 72 | + @override |
| 73 | + Future<void> send(JsonRpcMessage message, {int? relatedRequestId}) async => |
| 74 | + _unsupported(); |
| 75 | +} |
0 commit comments