MCP服务器助手-实验
模型上下文协议服务器,用于将AI编码工作卸载到Aider,提高开发效率和灵活性。
概述
该服务器允许Claude Code将AI编码任务卸载给Aider,最好的开源AI编码助手。通过将某些编码任务委托给Aider,我们可以降低成本,控制我们的编码模型,并以更协调的方式操作Claude Code来审查和修改代码。
设置
- 克隆存储库:
git clone https://github.com/disler/aider-mcp-server.git- 安装依赖项:
uv sync- 创建环境文件:
cp .env.sample .env- 在中配置API密钥
.env文件(或使用mcpServers的“env”部分)中包含您要在aider中使用的模型所需的api密钥:
GEMINI_API_KEY=your_gemini_api_key_here
OPENAI_API_KEY=your_openai_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
...see .env.sample for more- 复制并填写
.mcp.json进入项目的根目录并更新--directory指向此项目的根目录和--current-working-dir指向项目的根源。
{
"mcpServers": {
"aider-mcp-server": {
"type": "stdio",
"command": "uv",
"args": [
"--directory",
"
",
"run",
"aider-mcp-server",
"--editor-model",
"gpt-4o",
"--current-working-dir",
"
"
],
"env": {
"GEMINI_API_KEY": "",
"OPENAI_API_KEY": "",
"ANTHROPIC_API_KEY": "",
...see .env.sample for more
}
}
}
}测试
使用gemini-2.5-pro-exp-03-25进行测试
要运行所有测试,请执行以下操作:
uv run pytest要运行特定测试,请执行以下操作:
# Test listing models
uv run pytest src/aider_mcp_server/tests/atoms/tools/test_aider_list_models.py
# Test AI coding
uv run pytest src/aider_mcp_server/tests/atoms/tools/test_aider_ai_code.py注:AI编码测试需要Gemini模型的有效API密钥。确保将其设置在您的 .env 在运行测试之前,请先打开文件。
将此MCP服务器添加到Claude Code
使用添加 gemini-2.5-pro-exp-03-25
claude mcp add aider-mcp-server -s local \
-- \
uv --directory "
" \
run aider-mcp-server \
--editor-model "gemini/gemini-2.5-pro-exp-03-25" \
--current-working-dir "
"使用添加 gemini-2.5-pro-preview-03-25
claude mcp add aider-mcp-server -s local \
-- \
uv --directory "
" \
run aider-mcp-server \
--editor-model "gemini/gemini-2.5-pro-preview-03-25" \
--current-working-dir "
"使用添加 quasar-alpha
claude mcp add aider-mcp-server -s local \
-- \
uv --directory "
" \
run aider-mcp-server \
--editor-model "openrouter/openrouter/quasar-alpha" \
--current-working-dir "
"使用添加 llama4-maverick-instruct-basic
claude mcp add aider-mcp-server -s local \
-- \
uv --directory "
" \
run aider-mcp-server \
--editor-model "fireworks_ai/accounts/fireworks/models/llama4-maverick-instruct-basic" \
--current-working-dir "
"用法
此MCP服务器提供以下功能:
- 将AI编码任务卸载到Aider:
- 获取提示和文件路径 - 使用Aider实现请求的更改 - 返回成功或失败
- 列出可用型号:
- 提供与子字符串匹配的模型列表 - 有助于发现支持的模型
可用工具
此MCP服务器公开了以下工具:
1. aider_ai_code
此工具允许您运行Aider,根据提供的提示和指定的文件执行AI编码任务。
参数:
ai_coding_prompt(string,必填):AI编码任务的自然语言指令。relative_editable_files(字符串列表,必填):文件路径列表(相对于current_working_dir)允许助手修改。如果文件不存在,它将被创建。relative_readonly_files(字符串列表,可选):文件路径列表(相对于current_working_dir)Aider可以读取上下文,但不能修改。默认为空列表[].model(字符串,可选):Aider应使用的主要AI模型来生成代码。默认为"gemini/gemini-2.5-pro-exp-03-25"。您可以使用list_models该工具用于查找其他可用型号。editor_model(字符串,可选):AI模型助手应用于编辑/细化代码,特别是在使用架构师模式时。如果没有提供,主要model可能会根据Aider的内部逻辑使用。默认为None.
示例用法(在MCP请求中):
克劳德代码提示:
Use the Aider AI Code tool to: Refactor the calculate_sum function in calculator.py to handle potential TypeError exceptions.结果:
{
"name": "aider_ai_code",
"parameters": {
"ai_coding_prompt": "Refactor the calculate_sum function in calculator.py to handle potential TypeError exceptions.",
"relative_editable_files": ["src/calculator.py"],
"relative_readonly_files": ["docs/requirements.txt"],
"model": "openai/gpt-4o"
}
}退货:
- 一句简单的格言:{成功,差异}
- success:boolean-操作是否成功。 - diff:string-对文件所做更改的差异。
2. list_models
此工具列出了Aider支持的与给定子字符串匹配的可用AI模型。
参数:
substring(string,必填):在可用模型的名称中搜索的子字符串。
示例用法(在MCP请求中):
克劳德代码提示:
Use the Aider List Models tool to: List models that contain the substring "gemini".结果:
{
"name": "list_models",
"parameters": {
"substring": "gemini"
}
}退货:
- 与提供的子字符串匹配的模型名称字符串列表。示例:
["gemini/gemini-1.5-flash", "gemini/gemini-1.5-pro", "gemini/gemini-pro"]
建筑
服务器的结构如下:
- 服务器层:处理MCP协议通信
- 原子层:独立、纯功能组件
- 工具:特定功能(AI编码、列出模型) - 有用:常量和辅助函数 - 数据类型:使用Pydantic的类型定义
所有组件都经过了彻底的可靠性测试。
代码库结构
该项目分为以下主要目录和文件:
.
├── ai_docs # Documentation related to AI models and examples
│ ├── just-prompt-example-mcp-server.xml
│ └── programmable-aider-documentation.md
├── pyproject.toml # Project metadata and dependencies
├── README.md # This file
├── specs # Specification documents
│ └── init-aider-mcp-exp.md
├── src # Source code directory
│ └── aider_mcp_server # Main package for the server
│ ├── __init__.py # Package initializer
│ ├── __main__.py # Main entry point for the server executable
│ ├── atoms # Core, reusable components (pure functions)
│ │ ├── __init__.py
│ │ ├── data_types.py # Pydantic models for data structures
│ │ ├── logging.py # Custom logging setup
│ │ ├── tools # Individual tool implementations
│ │ │ ├── __init__.py
│ │ │ ├── aider_ai_code.py # Logic for the aider_ai_code tool
│ │ │ └── aider_list_models.py # Logic for the list_models tool
│ │ └── utils.py # Utility functions and constants (like default models)
│ ├── server.py # MCP server logic, tool registration, request handling
│ └── tests # Unit and integration tests
│ ├── __init__.py
│ └── atoms # Tests for the atoms layer
│ ├── __init__.py
│ ├── test_logging.py # Tests for logging
│ └── tools # Tests for the tools
│ ├── __init__.py
│ ├── test_aider_ai_code.py # Tests for AI coding tool
│ └── test_aider_list_models.py # Tests for model listing toolsrc/aider_mcp_server:包含主要应用程序代码。
- atoms:掌握基本构件。这些被设计为具有最小依赖性的纯函数或简单类。 - tools:这里的每个文件都实现了特定MCP工具的核心逻辑(aider_ai_code, list_models). - utils.py:包含共享常量,如默认模型名称。 - data_types.py:为请求/响应结构定义Pydantic模型,确保数据验证。 - logging.py:为控制台和文件输出设置一致的日志记录格式。 - server.py:编排MCP服务器。它初始化服务器,注册在 atoms/tools 目录,处理传入的请求,将它们路由到适当的工具逻辑,并根据MCP协议发回响应。 - __main__.py:提供命令行界面入口点(aider-mcp-server),解析参数,如 --editor-model 并启动中定义的服务器 server.py. - tests:包含反映结构的测试 src 目录,确保每个组件(尤其是原子)按预期工作。
