|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'dart:convert'; |
| 4 | + |
| 5 | +import 'package:mcp_dart/mcp_dart.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('stdio transport integration test', () { |
| 10 | + late Client client; |
| 11 | + late StdioClientTransport transport; |
| 12 | + final stderrOutput = <String>[]; |
| 13 | + StreamSubscription<String>? stderrSub; |
| 14 | + |
| 15 | + // Path to the example stdio server file |
| 16 | + final String serverFilePath = |
| 17 | + '${Directory.current.path}/example/server_stdio.dart'; |
| 18 | + |
| 19 | + setUp(() async { |
| 20 | + // Verify the server file exists |
| 21 | + final serverFile = File(serverFilePath); |
| 22 | + expect(await serverFile.exists(), isTrue, |
| 23 | + reason: 'Example server file not found'); |
| 24 | + |
| 25 | + // Create the client and transport |
| 26 | + client = |
| 27 | + Client(Implementation(name: "test-stdio-client", version: "1.0.0")); |
| 28 | + transport = StdioClientTransport( |
| 29 | + StdioServerParameters( |
| 30 | + command: 'dart', |
| 31 | + args: [serverFilePath], |
| 32 | + stderrMode: ProcessStartMode.normal, // Pipe stderr for debugging |
| 33 | + ), |
| 34 | + ); |
| 35 | + |
| 36 | + // Set up error handlers |
| 37 | + client.onerror = (error) => fail('Client error: $error'); |
| 38 | + |
| 39 | + transport.onerror = (error) { |
| 40 | + // Don't fail here as some non-critical errors might occur |
| 41 | + stderrOutput.add('Transport error: $error'); |
| 42 | + }; |
| 43 | + |
| 44 | + // Capture stderr output from the server process |
| 45 | + stderrSub = transport.stderr |
| 46 | + ?.transform(utf8.decoder) |
| 47 | + .transform(const LineSplitter()) |
| 48 | + .listen((line) { |
| 49 | + stderrOutput.add(line); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + tearDown(() async { |
| 54 | + // Clean up resources |
| 55 | + try { |
| 56 | + await transport.close(); |
| 57 | + } catch (e) { |
| 58 | + // Ignore errors during cleanup |
| 59 | + } |
| 60 | + |
| 61 | + await stderrSub?.cancel(); |
| 62 | + }); |
| 63 | + |
| 64 | + test('client and server communicate successfully using stdio transport', |
| 65 | + () async { |
| 66 | + // Connect client to the transport and establish communication |
| 67 | + await client.connect(transport); |
| 68 | + |
| 69 | + // Make sure connection is established |
| 70 | + await Future.delayed(Duration(milliseconds: 500)); |
| 71 | + |
| 72 | + // Get available tools |
| 73 | + final tools = await client.listTools(); |
| 74 | + expect(tools.tools.isNotEmpty, isTrue, |
| 75 | + reason: 'Server should return at least one tool'); |
| 76 | + expect(tools.tools.any((tool) => tool.name == 'calculate'), isTrue, |
| 77 | + reason: 'Server should have a "calculate" tool'); |
| 78 | + |
| 79 | + // Test all calculator operations |
| 80 | + final operations = [ |
| 81 | + { |
| 82 | + 'operation': 'add', |
| 83 | + 'a': 5, |
| 84 | + 'b': 3, |
| 85 | + 'expected': 'Result: 8', |
| 86 | + 'description': 'Addition' |
| 87 | + }, |
| 88 | + { |
| 89 | + 'operation': 'subtract', |
| 90 | + 'a': 10, |
| 91 | + 'b': 4, |
| 92 | + 'expected': 'Result: 6', |
| 93 | + 'description': 'Subtraction' |
| 94 | + }, |
| 95 | + { |
| 96 | + 'operation': 'multiply', |
| 97 | + 'a': 6, |
| 98 | + 'b': 7, |
| 99 | + 'expected': 'Result: 42', |
| 100 | + 'description': 'Multiplication' |
| 101 | + }, |
| 102 | + { |
| 103 | + 'operation': 'divide', |
| 104 | + 'a': 20, |
| 105 | + 'b': 5, |
| 106 | + 'expected': r'Result: 4(\.0)?$', |
| 107 | + 'description': 'Division', |
| 108 | + 'isRegex': true |
| 109 | + } |
| 110 | + ]; |
| 111 | + |
| 112 | + for (final op in operations) { |
| 113 | + final params = CallToolRequestParams( |
| 114 | + name: 'calculate', |
| 115 | + arguments: {'operation': op['operation'], 'a': op['a'], 'b': op['b']}, |
| 116 | + ); |
| 117 | + |
| 118 | + final result = await client.callTool(params); |
| 119 | + expect(result.content.first is TextContent, isTrue, |
| 120 | + reason: 'Result should contain TextContent'); |
| 121 | + |
| 122 | + final textContent = result.content.first as TextContent; |
| 123 | + |
| 124 | + if (op['isRegex'] == true) { |
| 125 | + expect(textContent.text, matches(op['expected'] as String), |
| 126 | + reason: '${op['description']} result incorrect'); |
| 127 | + } else { |
| 128 | + expect(textContent.text, op['expected'], |
| 129 | + reason: '${op['description']} result incorrect'); |
| 130 | + } |
| 131 | + } |
| 132 | + }, timeout: Timeout(Duration(seconds: 30))); |
| 133 | + }); |
| 134 | +} |
0 commit comments