Aider代码助手
允许 Claude Code 将人工智能编码任务卸载到 Aider,从而降低费用并使对特定模型处理特定编码任务的控制更加灵活。
服务介绍
Aider MCP 服务器 - 实验性
用于将AI编码工作卸载到Aider的模型上下文协议服务器,以提高开发效率和灵活性。
概述
该服务器允许Claude Code将AI编码任务卸载给最佳开源AI编码助手Aider。通过将某些编码任务委托给Aider,我们可以降低成本,控制我们的编码模型,并以更协调的方式操作Claude Code来审查和修订代码。
设置
- 克隆仓库:
git clone https://github.com/disler/aider-mcp-server.git
- 安装依赖项:
uv sync
- 创建您的环境文件:
cp .env.sample .env
- 在
.env文件中配置您的API密钥(或使用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",
"<path to this project>",
"run",
"aider-mcp-server",
"--editor-model",
"gpt-4o",
"--current-working-dir",
"<path to your project>"
],
"env": {
"GEMINI_API_KEY": "<your gemini api key>",
"OPENAI_API_KEY": "<your openai api key>",
"ANTHROPIC_API_KEY": "<your 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 "<path to the aider mcp server project>" \
run aider-mcp-server \
--editor-model "gemini/gemini-2.5-pro-exp-03-25" \
--current-working-dir "<path to your project>"
使用gemini-2.5-pro-preview-03-25添加
claude mcp add aider-mcp-server -s local \
-- \
uv --directory "<path to the aider mcp server project>" \
run aider-mcp-server \
--editor-model "gemini/gemini-2.5-pro-preview-03-25" \
--current-working-dir "<path to your project>"
使用quasar-alpha添加
claude mcp add aider-mcp-server -s local \
-- \
uv --directory "<path to the aider mcp server project>" \
run aider-mcp-server \
--editor-model "openrouter/openrouter/quasar-alpha" \
--current-working-dir "<path to your project>"
使用llama4-maverick-instruct-basic添加
claude mcp add aider-mcp-server -s local \
-- \
uv --directory "<path to the aider mcp server project>" \
run aider-mcp-server \
--editor-model "fireworks_ai/accounts/fireworks/models/llama4-maverick-instruct-basic" \
--current-working-dir "<path to your project>"
使用方法
此MCP服务器提供了以下功能:
-
将AI编码任务卸载到Aider:
- 接收提示和文件路径
- 使用Aider实现请求的更改
- 返回成功或失败
-
列出可用模型:
- 提供与子字符串匹配的模型列表
- 有助于发现支持的模型
可用工具
此MCP服务器公开了以下工具:
1. aider_ai_code
此工具允许您基于提供的提示和指定的文件运行Aider执行AI编码任务。
参数:
ai_coding_prompt(字符串,必需):AI 编码任务的自然语言指令。relative_editable_files(字符串列表,必需):Aider 允许修改的文件路径列表(相对于current_working_dir)。如果文件不存在,则会创建该文件。relative_readonly_files(字符串列表,可选):Aider 可以读取上下文但不能修改的文件路径列表(相对于current_working_dir)。默认为空列表[]。model(字符串,可选):Aider 用于生成代码的主要 AI 模型。默认为"gemini/gemini-2.5-pro-exp-03-25"。你可以使用list_models工具来查找其他可用模型。editor_model(字符串,可选):Aider 在编辑/优化代码时应使用的 AI 模型,特别是在使用架构模式时。如果没有提供,则根据 Aider 的内部逻辑可能会使用主要model。默认为None。
示例用法(在 MCP 请求中):
Claude 代码提示:
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, diff}
success: 布尔值 - 操作是否成功。diff: 字符串 - 对文件所做的更改的差异。
2. list_models
此工具列出了 Aider 支持的与给定子字符串匹配的可用 AI 模型。
参数:
substring(字符串,必需):在可用模型名称中搜索的子字符串。
示例用法(在 MCP 请求中):
Claude 代码提示:
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 tool
src/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目录结构相匹配的测试,确保每个组件(特别是atoms)按预期工作。