Dart-MCP 工具
一个可分发的模型上下文协议(MCP)服务器,用于暴露 Dart SDK 命令以实现人工智能驱动的开发。该服务器通过实现模型上下文协议(MCP),弥合了人工智能代码助手与 Dart/Flutter 开发工作流之间的差距。
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"dart": {
"args": [
"-y",
"@egyleader/dart-mcp-server"
],
"command": "npx"
}
}
}
该服务需要配置环境变量:DART_MCP_VERBOSE
服务介绍
Dart MCP 服务器
这是一个可分发的模型上下文协议 (MCP) 服务器,它为 AI 驱动的开发提供了 Dart SDK 命令。该服务器通过实现模型上下文协议 (MCP),在 AI 编码助手与 Dart/Flutter 开发工作流之间架起了桥梁。
特性
此 MCP 服务器提供了对以下 Dart SDK 命令的无缝访问:
| 命令 | 描述 |
|---|---|
dart-analyze |
分析 Dart 代码中的错误、警告和格式问题 |
dart-compile |
将 Dart 编译成各种格式(如 exe、AOT/JIT 快照、JavaScript) |
dart-create |
从模板创建新的 Dart 项目 |
dart-doc |
为 Dart 项目生成 API 文档 |
dart-fix |
对 Dart 源代码应用自动修复 |
dart-format |
根据样式指南格式化 Dart 源代码 |
dart-info |
显示已安装 Dart 工具的诊断信息 |
dart-package |
处理包(获取、添加、升级、过时等) |
dart-run |
运行 Dart 程序,并支持传递参数 |
dart-test |
运行测试,并支持过滤和报告选项 |
主要优点
- 智能路径处理:自动将相对路径解析为绝对路径,确保命令无论在哪个工作目录下都能正确运行
- 项目自动检测:识别家目录和工作区等常见位置的 Dart/Flutter 项目
- 跨平台支持:适用于 macOS、Linux 和 Windows
- 零配置:开箱即用,默认设置合理
- MCP 集成:兼容任何 MCP 客户端,包括 Windsurf、Cline 及其他模型上下文协议实现
先决条件
- Node.js:18.x 或更高版本
- Dart SDK:3.0 或更高版本已安装并在您的 PATH 中可用
安装
通过 Smithery 安装
要通过 Smithery 自动为 Claude Desktop 安装 Dart MCP 服务器:
npx -y @smithery/cli install @egyleader/dart-mcp --client claude
使用 npx(推荐)
可以直接使用 npx 运行服务器而无需安装:
npx @egyleader/dart-mcp-server
全局安装
为了更方便地访问,您可以全局安装服务器:
npm install -g @egyleader/dart-mcp-server
然后使用以下命令运行它:
dart-mcp-server
从源代码安装
# Clone the repository
git clone https://github.com/egyleader/dart-mcp-server.git
cd dart-mcp-server
# Install dependencies
npm install
# Build the project
npm run build
# Run the server
node dist/index.js
与 MCP 客户端集成
Windsurf / Codeium IDE 配置
要将此 MCP 服务器与 Windsurf 或 Codeium IDE 一起使用,请将以下内容添加到您的 mcp_config.json 文件中(通常位于 ~/.codeium/windsurf/mcp_config.json):
{
"mcpServers": {
"dart": {
"command": "npx",
"args": [
"-y",
"@egyleader/dart-mcp-server"
]
}
}
}
环境变量
请根据需要配置相关环境变量以优化服务行为或指定自定义设置。
DART_MCP_VERBOSE: 设置为任意值以启用详细日志记录,用于调试
MCP 工具使用示例
以下是服务器提供的 MCP 工具的使用示例。这些示例展示了可以传递给每个工具的参数。
dart-analyze
分析 Dart 代码中的错误、警告和规范:
{
"path": "lib/main.dart",
"options": ["--fatal-infos", "--fatal-warnings"]
}
dart-compile
将 Dart 代码编译为各种格式:
{
"path": "lib/main.dart",
"format": "exe",
"output": "build/app",
"options": ["--verbose"]
}
支持的格式:exe, aot-snapshot, jit-snapshot, kernel, js
dart-create
从模板创建一个新的 Dart 项目:
{
"projectName": "my_awesome_app",
"template": "console",
"output": "projects/my_awesome_app",
"options": ["--force"]
}
关于 projectName 和 output 的说明:
- 如果只提供了
projectName,则将其用作创建项目的目录名称。 - 如果提供了
output,则将其用作创建项目的目录。 - 实际的 Dart 包/项目名称由 Dart CLI 根据最终目录名称推导得出。
支持的模板:console, package, server-shelf, web
dart-doc
为 Dart 项目生成 API 文档:
{
"path": ".",
"output": "doc",
"options": ["--exclude", "lib/generated"]
}
dart-fix
对 Dart 源代码应用自动修复:
{
"path": "lib",
"apply": true,
"options": ["--pedantic"]
}
dart-format
根据样式指南格式化 Dart 源代码:
{
"paths": ["lib/main.dart", "lib/models"],
"setExitIfChanged": true,
"options": ["--line-length=100"]
}
dart-info
显示已安装的 Dart 工具的诊断信息:
{
"options": ["--verbose"]
}
dart-package
处理包(pub 命令):
{
"command": "get",
"workingDir": ".",
"args": ["--offline"]
}
支持的命令:get, upgrade, outdated, add, remove, publish, deps, downgrade, cache, run, global
dart-run
运行 Dart 程序,并支持传递参数:
{
"script": "bin/server.dart",
"workingDir": ".",
"args": ["--port=8080", "--mode=production"]
}
dart-test
运行测试,并支持过滤和报告选项:
{
"path": "test",
"workingDir": ".",
"options": ["--name=login", "--platform=chrome"]
}
许可证
本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。
贡献
欢迎贡献!请随时提交 Pull Request。
工具 API 参考
dart-analyze
分析目录或文件中的 Dart 代码。
{
path?: string; // Directory or file to analyze
options?: string[]; // Additional options for the dart analyze command
}
示例:
{
path: "lib",
options: ["--fatal-infos", "--fatal-warnings"]
}
dart-compile
将 Dart 编译为各种格式。
{
format: 'exe' | 'aot-snapshot' | 'jit-snapshot' | 'kernel' | 'js'; // Output format
path: string; // Path to the Dart file to compile
output?: string; // Output file path
options?: string[]; // Additional compilation options
}
示例:
{
format: "exe",
path: "bin/main.dart",
output: "bin/app"
}
dart-create
创建一个新的 Dart 项目。
{
template: 'console' | 'package' | 'server-shelf' | 'web'; // Project template
projectName: string; // Name of the project to create
output?: string; // Directory where to create the project
options?: string[]; // Additional project creation options
}
注意:
- 如果提供了
output,则项目将在该目录中创建。 - 如果只提供了
projectName,则将其用作目录名称。 - 实际的 Dart 包名称由最终目录名称推导得出。
示例:
{
template: "package",
projectName: "my_dart_library",
output: "projects/my_dart_library"
}
dart-doc
为 Dart 项目生成 API 文档。
{
path?: string; // Directory containing the Dart package to document
output?: string; // Output directory for the generated documentation
options?: string[]; // Additional documentation options
}
示例:
{
path: ".",
output: "doc/api"
}
dart-fix
对 Dart 源代码应用自动修复。
{
path?: string; // Directory or file to apply fixes to
apply?: boolean; // Whether to apply the suggested fixes (default: true)
options?: string[]; // Additional fix options
}
示例:
{
path: "lib",
apply: true,
options: ["--pedantic"]
}
dart-format
惯用方式格式化 Dart 源代码。
{
paths: string[]; // Files or directories to format
setExitIfChanged?: boolean; // Return exit code 1 if there are formatting changes (default: false)
options?: string[]; // Additional format options
}
示例:
{
paths: ["lib", "test"],
setExitIfChanged: true,
options: ["--line-length=80"]
}
dart-info
显示已安装工具的诊断信息。
{
options?: string[]; // Additional info options
}
示例:
{
options: ["--verbose"]
}
dart-package
处理包(pub 命令)。
{
command: 'get' | 'upgrade' | 'outdated' | 'add' | 'remove' | 'publish' | 'deps' | 'downgrade' | 'cache' | 'run' | 'global'; // Pub subcommand
args?: string[]; // Arguments for the pub subcommand
workingDir?: string; // Working directory for the command
}
示例:
// Add a package
{
command: "add",
args: ["rxdart"],
workingDir: "my_project"
}
// Get dependencies
{
command: "get",
workingDir: "my_project"
}
dart-run
运行一个 Dart 程序。
{
script: string; // Path to the Dart script to run
args?: string[]; // Arguments to pass to the script
workingDir?: string; // Working directory for the command
}
示例:
{
script: "bin/main.dart",
args: ["--verbose"],
workingDir: "my_project"
}
dart-test
运行项目的测试。
{
path?: string; // Path to the test file or directory
options?: string[]; // Additional test options
workingDir?: string; // Working directory for the command
}
示例:
{
path: "test",
options: ["--coverage", "--name=auth"],
workingDir: "my_project"
}
开发
# Watch mode for development
pnpm run dev
# Build for production
pnpm run build
错误处理
服务器实现了全面的错误处理:
- 捕获并适当格式化命令执行错误
- 路径解析问题会附带详细的诊断信息进行报告
- 对长时间运行的操作进行超时处理
- 从 Dart 命令中正确传播退出代码
贡献
请参阅 CONTRIBUTING.md 获取详细的贡献指南。
我们的提交格式遵循:
<type>[optional scope]: [JIRA-123(optional)] <description>
示例:
feat(tools): [DART-456] add support for dart test tags
许可证
MIT