|
| 1 | +import 'package:mcp_dart/mcp_dart.dart'; |
| 2 | + |
| 3 | +void main() { |
| 4 | + // Create a tool with required fields - this simulates what an MCP server would send |
| 5 | + final calculatorTool = Tool( |
| 6 | + name: 'calculate', |
| 7 | + description: 'Performs mathematical calculations', |
| 8 | + inputSchema: ToolInputSchema( |
| 9 | + properties: { |
| 10 | + 'operation': { |
| 11 | + 'type': 'string', |
| 12 | + 'enum': ['add', 'subtract', 'multiply', 'divide'], |
| 13 | + 'description': 'The mathematical operation to perform' |
| 14 | + }, |
| 15 | + 'a': {'type': 'number', 'description': 'First number'}, |
| 16 | + 'b': {'type': 'number', 'description': 'Second number'}, |
| 17 | + 'precision': { |
| 18 | + 'type': 'integer', |
| 19 | + 'description': 'Number of decimal places (optional)', |
| 20 | + 'default': 2 |
| 21 | + } |
| 22 | + }, |
| 23 | + required: ['operation', 'a', 'b'], // ← This is now preserved! |
| 24 | + ), |
| 25 | + outputSchema: ToolOutputSchema( |
| 26 | + properties: { |
| 27 | + 'result': {'type': 'number', 'description': 'The calculation result'}, |
| 28 | + 'equation': { |
| 29 | + 'type': 'string', |
| 30 | + 'description': 'The equation that was calculated' |
| 31 | + } |
| 32 | + }, |
| 33 | + required: ['result'], // ← Output required fields also preserved! |
| 34 | + ), |
| 35 | + ); |
| 36 | + |
| 37 | + print('=== MCP Tool Schema Demo ==='); |
| 38 | + print('Tool: ${calculatorTool.name}'); |
| 39 | + print('Description: ${calculatorTool.description}'); |
| 40 | + |
| 41 | + // Serialize to JSON (what MCP client receives) |
| 42 | + final toolJson = calculatorTool.toJson(); |
| 43 | + print('\n=== Serialized Tool JSON ==='); |
| 44 | + print('Input required fields: ${toolJson['inputSchema']['required']}'); |
| 45 | + print('Output required fields: ${toolJson['outputSchema']['required']}'); |
| 46 | + |
| 47 | + // This demonstrates the fix - required fields are preserved in JSON |
| 48 | + print('\n=== Full Input Schema ==='); |
| 49 | + final inputSchema = toolJson['inputSchema'] as Map<String, dynamic>; |
| 50 | + print('Type: ${inputSchema['type']}'); |
| 51 | + print('Required: ${inputSchema['required']}'); |
| 52 | + print('Properties: ${(inputSchema['properties'] as Map).keys.join(', ')}'); |
| 53 | + |
| 54 | + // Deserialize back from JSON (roundtrip test) |
| 55 | + final deserializedTool = Tool.fromJson(toolJson); |
| 56 | + print('\n=== Roundtrip Test ==='); |
| 57 | + print('Original required: ${calculatorTool.inputSchema.required}'); |
| 58 | + print('Deserialized required: ${deserializedTool.inputSchema.required}'); |
| 59 | + print( |
| 60 | + 'Match: ${_listsEqual(calculatorTool.inputSchema.required, deserializedTool.inputSchema.required)}'); |
| 61 | + |
| 62 | + // Convert to OpenAI function calling format |
| 63 | + print('\n=== OpenAI Function Format ==='); |
| 64 | + final openaiFunction = <String, dynamic>{ |
| 65 | + 'type': 'function', |
| 66 | + 'function': <String, dynamic>{ |
| 67 | + 'name': calculatorTool.name, |
| 68 | + 'description': calculatorTool.description, |
| 69 | + 'parameters': calculatorTool.inputSchema.toJson(), |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + final functionObj = openaiFunction['function'] as Map<String, dynamic>; |
| 74 | + final parameters = functionObj['parameters'] as Map<String, dynamic>; |
| 75 | + print('Function name: ${functionObj['name']}'); |
| 76 | + print('Required parameters: ${parameters['required']}'); |
| 77 | + print('✓ Ready for LLM integration!'); |
| 78 | + |
| 79 | + // Convert to Anthropic Claude format |
| 80 | + print('\n=== Anthropic Claude Format ==='); |
| 81 | + final anthropicTool = <String, dynamic>{ |
| 82 | + 'name': calculatorTool.name, |
| 83 | + 'description': calculatorTool.description, |
| 84 | + 'input_schema': calculatorTool.inputSchema.toJson(), |
| 85 | + }; |
| 86 | + |
| 87 | + final claudeSchema = anthropicTool['input_schema'] as Map<String, dynamic>; |
| 88 | + print('Tool name: ${anthropicTool['name']}'); |
| 89 | + print('Required parameters: ${claudeSchema['required']}'); |
| 90 | + print('✓ Ready for Claude integration!'); |
| 91 | + |
| 92 | + // Simulate a real MCP server response |
| 93 | + print('\n=== Simulated MCP Server Response ==='); |
| 94 | + final listToolsResult = ListToolsResult(tools: [calculatorTool]); |
| 95 | + final serverResponse = listToolsResult.toJson(); |
| 96 | + |
| 97 | + print('Server response preserves required fields:'); |
| 98 | + final tools = serverResponse['tools'] as List; |
| 99 | + final firstTool = tools[0] as Map<String, dynamic>; |
| 100 | + final firstToolInputSchema = firstTool['inputSchema'] as Map<String, dynamic>; |
| 101 | + print(' Tool: ${firstTool['name']}'); |
| 102 | + print(' Required: ${firstToolInputSchema['required']}'); |
| 103 | + print('✓ MCP server integration works!'); |
| 104 | +} |
| 105 | + |
| 106 | +bool _listsEqual(List<String>? a, List<String>? b) { |
| 107 | + if (a == null && b == null) return true; |
| 108 | + if (a == null || b == null) return false; |
| 109 | + if (a.length != b.length) return false; |
| 110 | + for (int i = 0; i < a.length; i++) { |
| 111 | + if (a[i] != b[i]) return false; |
| 112 | + } |
| 113 | + return true; |
| 114 | +} |
0 commit comments