MCP飞镖SDK
](https://pub.dev/packages/mcp_dart_sdk) 
Dart实现 模型上下文协议(MCP) 用于LLM应用程序和外部数据源/工具之间的无缝集成。
特性
- 全面实施模型上下文协议
- 多种传输选项(stdio、HTTP+SSE、WebSocket)
- 支持所有核心功能(资源、工具、提示、日志记录)
- 带有Dart习惯用法的类型安全API
- 全面的文档和示例
- 支持Flutter和独立Dart应用程序
安装
添加 mcp_dart_sdk 作为你的依赖 pubspec.yaml 文件:
dependencies:
mcp_dart_sdk: ^0.1.0然后运行:
dart pub get或者用Flutter:
flutter pub get用法
客户端示例
import 'dart:io';
import 'package:mcp_dart_sdk/mcp_dart_sdk.dart';
Future main() async {
// Launch a server process (e.g., a Python MCP server)
final process = await Process.start(
'python3',
['-m', 'mcp.server.stdio'],
mode: ProcessStartMode.normal,
);
// Create a client
final client = McpClient();
try {
// Connect to the server using stdio transport
await client.connect(StdioTransport(process));
// Initialize the session
final capabilities = await client.initialize(
clientInfo: ClientInfo(name: 'Dart MCP Client', version: '1.0.0'),
capabilities: ClientCapabilities(),
);
print('Connected to server: ${client.serverInfo?.name} ${client.serverInfo?.version}');
// Check if the server supports tools
if (client.isCapabilitySupported('tools')) {
// List available tools
final result = await client.sendRequest('tools/list');
final tools = result['tools'] as List;
print('Available tools:');
for (final tool in tools) {
print('- ${tool['name']}: ${tool['description']}');
}
// Call a tool
if (tools.isNotEmpty) {
final toolName = tools.first['name'];
final toolResult = await client.sendRequest('tools/execute', {
'name': toolName,
'params': {},
});
print('Tool result: $toolResult');
}
}
// Clean shutdown
await client.shutdown();
await client.exit();
} catch (e) {
print('Error: $e');
} finally {
await client.close();
process.kill();
}
}WebSocket传输示例
import 'package:mcp_dart_sdk/mcp_dart_sdk.dart';
Future main() async {
final client = McpClient();
try {
// Connect to a WebSocket server
final transport = WebSocketTransport(url: 'ws://localhost:8000/ws');
await transport.connect();
await client.connect(transport);
await client.initialize(
clientInfo: ClientInfo(name: 'Dart MCP Client', version: '1.0.0'),
capabilities: ClientCapabilities(),
);
// Use the client...
await client.shutdown();
await client.exit();
} catch (e) {
print('Error: $e');
} finally {
await client.close();
}
}HTTP+SSE传输示例
import 'package:mcp_dart_sdk/mcp_dart_sdk.dart';
Future main() async {
final client = McpClient();
try {
// Connect using HTTP+SSE
final transport = HttpSseTransport(
sendUrl: 'http://localhost:8000/send',
receiveUrl: 'http://localhost:8000/events',
);
await transport.connect();
await client.connect(transport);
await client.initialize(
clientInfo: ClientInfo(name: 'Dart MCP Client', version: '1.0.0'),
capabilities: ClientCapabilities(),
);
// Use the client...
await client.shutdown();
await client.exit();
} catch (e) {
print('Error: $e');
} finally {
await client.close();
}
}文档
贡献
欢迎投稿!请随时提交拉取请求。
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
