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

Dart and Flutter SDK for building Model Context Protocol (MCP) servers, clients, hosts, and AI tools.

example/example.md

Examples #

Stdio Server #

import 'package:mcp_dart/mcp_dart.dart';

void main() async {
  McpServer server = McpServer(
    Implementation(name: "example-server", version: "1.0.0"),
    options: McpServerOptions(
      capabilities: ServerCapabilities(
        resources: ServerCapabilitiesResources(),
        tools: ServerCapabilitiesTools(),
      ),
    ),
  );

  server.registerTool(
    "calculate",
    description: 'Perform basic arithmetic operations',
    inputSchema: JsonSchema.object(
      properties: {
        'operation': JsonSchema.string(
          enumValues: ['add', 'subtract', 'multiply', 'divide'],
        ),
        'a': JsonSchema.number(),
        'b': JsonSchema.number(),
      },
      required: ['operation', 'a', 'b'],
    ),
    callback: (args, extra) 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'),
            },
          ),
        ],
      );
    },
  );

  server.connect(StdioServerTransport());
}

Streamable HTTP Server #

import 'dart:io';
import 'package:mcp_dart/mcp_dart.dart';

void main() async {
  final server = McpServer(
    Implementation(name: "example-http-server", version: "1.0.0"),
    options: McpServerOptions(
      capabilities: ServerCapabilities(
        tools: ServerCapabilitiesTools(),
      ),
    ),
  );

  server.registerTool(
    "echo",
    description: 'Echoes back the input',
    inputSchema: JsonSchema.object(properties: {
      'message': JsonSchema.string(),
    }),
    callback: (args, extra) async {
      return CallToolResult(
        content: [TextContent(text: "Echo: ${args['message']}")],
      );
    },
  );

  final transport = StreamableHTTPServerTransport(
      options: StreamableHTTPServerTransportOptions());
  await server.connect(transport);

  final httpServer = await HttpServer.bind(InternetAddress.anyIPv4, 3000);
  print('Server listening on http://localhost:3000/mcp');

  await for (final request in httpServer) {
    if (request.uri.path == '/mcp') {
      await transport.handleRequest(request);
    } else {
      request.response.statusCode = 404;
      await request.response.close();
    }
  }
}

For a more complex example handling multiple sessions, tasks, and interactive capabilities, see simple_task_interactive_server.dart.

More Examples #

63
likes
160
points
82k
downloads

Documentation

Documentation
API reference

Publisher

verified publisherleehack.com

Weekly Downloads

Dart and Flutter SDK for building Model Context Protocol (MCP) servers, clients, hosts, and AI tools.

Repository (GitHub)
View/report issues

Topics

#mcp #ai #llm #api #dart

License

MIT (license)

Dependencies

crypto, http, meta

More

Packages that depend on mcp_dart