mcp_dart 0.0.1 copy "mcp_dart: ^0.0.1" to clipboard
mcp_dart: ^0.0.1 copied to clipboard

MCP SDK for Dart

MCP(Model Context Protocol) for Dart #

Model Context Protocol (MCP) is an open protocol that enables seamless integration between LLM applications and external data sources and tools. The goal of this library is to provide a simple way to implement MCP server and client in Dart while implementing the MCP protocol spec in dart.

At the moment, it's very experimental and not ready for production use but if you want to implement a simple MCP server using Dart, you can use this library.

Features #

  • Stdio based protocol
  • Tools
  • Resources
  • Prompts

Getting started #

The library only support stdio based protocol at the moment. The custom transport method can be implemented by extending Transport.

Below code is the simplest way to start the MCP server.

void main() async {
  final transport = StdioTransport();
  MCPServer server = MCPServer(transport, name: 'Calculator', version: '0.0.1');

  server.tool(CalculatorTool()).start(); // Register the tool
}

Below code is the MCP Server tool example for Calculator.

class CalculatorTool extends ToolRunner {
  const CalculatorTool({
    super.inputSchema = const InputSchema(
      type: 'object',
      properties: {
        'operation': {
          'type': 'string',
          'enum': ['add', 'subtract', 'multiply', 'divide'],
        },
        'a': {'type': 'number'},
        'b': {'type': 'number'},
      },
      required: ['operation', 'a', 'b'],
    ),
    super.name = 'calculate',
    super.description = 'Perform basic arithmetic operations',
  });

  @override
  Future<CallToolResult> execute(Map<String, dynamic> args) async {
    final operation = args['operation'];
    final a = args['a'];
    final b = args['b'];
    return CallToolResult(
      content: [
        TextContent(
          text: switch (operation) {
            'add' => 'Result: ${a + b}',
            'subtract' => 'Result: ${a - b}',
            'multiply' => 'Result: ${a * b}',
            'divide' => 'Result: ${a / b}',
            _ => throw Exception('Invalid operation'),
          },
        ),
      ],
      isError: false,
    );
  }
}

Usage #

Once you compile your MCP server, you can compile the client using the below code.

dart dart compile exe example/calculator_mcp_server_example.dart -o ./calculator_mcp_server_example

Or just run it with JIT.

dart run example/calculator_mcp_server_example.dart

To configure it with the client (ex, Claude Desktop), you can use the below code.

{
  "mcpServers": {
    "calculator_jit": {
      "command": "path/to/dart",
      "args": [
        "/path/to/calculator_mcp_server_example.dart"
      ]
    },
    "calculator_aot": {
      "command": "path/to/compiled/calculator_mcp_server_example",
    }
  }
}

Credits #