克劳德代码控制MCP服务器
   
Claude Code用于代理工作流的自动化和控制。
部分 代理系统 -具有持久内存的24/7自主AI框架。
使用Claude AI进行自主编码工作流的编程任务执行。
概述
Claude代码控制MCP允许通过模型上下文协议执行编程代码任务。它为AI代理和工作流提供了一个桥梁,使用Claude Sonnet 4.5执行编码任务,具有全面的文件跟踪、命令执行和状态监控功能。
特性
- 程序化任务执行:自主执行自然语言编码任务
- 文件操作:读取、写入、编辑和跟踪文件更改
- 代码搜索:在代码库中进行类似Grep的搜索
- 命令执行:运行带有输出捕获的shell命令
- 变更追踪:监控执行过程中的所有文件修改
- 状态监测:实时执行状态和历史
建筑
┌─────────────────────────────────────────────────────────┐
│ MCP Client (Claude Code) │
└────────────────────┬────────────────────────────────────┘
│ MCP Protocol
┌────────────────────▼────────────────────────────────────┐
│ Claude Code Control MCP Server │
│ ┌──────────────┐ ┌───────────────┐ ┌─────────────┐ │
│ │ Server │ │ Executor │ │ Tracker │ │
│ │ (MCP) │ │ (Claude AI) │ │ (Files) │ │
│ └──────────────┘ └───────────────┘ └─────────────┘ │
└────────────────────┬────────────────────────────────────┘
│ Anthropic API
┌────────────────────▼────────────────────────────────────┐
│ Claude Sonnet 4.5 (Anthropic API) │
└─────────────────────────────────────────────────────────┘安装
- 安装依赖项:
cd ${AGENTIC_SYSTEM_PATH:-/opt/agentic}/mcp-servers/claude-code-control-mcp
pip3 install -r requirements.txt- 设置API密钥:
export ANTHROPIC_API_KEY="sk-ant-..."- 使用克劳德代码注册:
添加 ~/.claude.json:
{
"mcpServers": {
"claude-code-control": {
"command": "python3",
"args": [
"${AGENTIC_SYSTEM_PATH:-/opt/agentic}/mcp-servers/claude-code-control-mcp/server.py"
],
"env": {
"ANTHROPIC_API_KEY": "sk-ant-..."
},
"disabled": false
}
}
}- 重新启动Claude代码:
# Exit current session and restartMCP工具
execute_code_task
使用自主工具使用Claude AI执行编码任务。
输入:
{
"task_description": "Add error handling to the API endpoint in server.py",
"working_directory": "${AGENTIC_SYSTEM_PATH:-/opt/agentic}/api",
"context_files": ["server.py", "tests/test_api.py"],
"max_iterations": 20
}输出:
{
"success": true,
"task_description": "Add error handling...",
"working_directory": "${AGENTIC_SYSTEM_PATH:-/opt/agentic}/api",
"iterations": 8,
"tool_uses": [
{
"tool": "read_file",
"input": {"path": "server.py"},
"result": "File content..."
}
],
"file_changes": {
"total_changes": 2,
"files_modified": ["server.py"],
"files_created": [],
"files_deleted": []
},
"duration_seconds": 12.5,
"final_message": "Successfully added error handling..."
}读_贬值
使用glob模式读取和分析多个文件。
输入:
{
"patterns": ["*.py", "src/**/*.ts"],
"working_directory": "${AGENTIC_SYSTEM_PATH:-/opt/agentic}",
"max_files": 50
}输出:
{
"files_read": 12,
"files": [
{
"path": "server.py",
"size": 2048,
"content": "#!/usr/bin/env python3..."
}
]
}搜索代码
在代码库中搜索代码模式。
输入:
{
"query": "async def.*execute",
"working_directory": "${AGENTIC_SYSTEM_PATH:-/opt/agentic}",
"file_pattern": "*.py",
"case_sensitive": true,
"max_results": 100
}输出:
{
"query": "async def.*execute",
"total_matches": 5,
"matches": [
"executor.py:42:async def execute_task(...)",
"server.py:156:async def execute_command(...)"
]
}修改文件
使用批处理操作修改多个文件。
输入:
{
"changes": [
{
"path": "config.py",
"action": "write",
"content": "DEBUG = True\n"
},
{
"path": "server.py",
"action": "edit",
"old_content": "port = 8080",
"new_content": "port = 9000"
}
],
"working_directory": "${AGENTIC_SYSTEM_PATH:-/opt/agentic}"
}输出:
{
"modifications": [
{"path": "config.py", "status": "written"},
{"path": "server.py", "status": "edited"}
],
"file_changes": {
"total_changes": 2,
"files_created": ["config.py"],
"files_modified": ["server.py"]
}
}run_命令
使用输出捕获执行shell命令。
输入:
{
"commands": [
"python3 -m pytest tests/",
"black --check *.py"
],
"working_directory": "${AGENTIC_SYSTEM_PATH:-/opt/agentic}",
"timeout": 30
}输出:
{
"command_results": [
{
"command": "python3 -m pytest tests/",
"exit_code": 0,
"stdout": "===== 5 passed in 2.5s =====",
"stderr": ""
},
{
"command": "black --check *.py",
"exit_code": 0,
"stdout": "All done! ✨",
"stderr": ""
}
]
}获取执行状态
获取当前或最近执行的状态。
输入:
{
"include_history": true
}输出:
{
"current_execution": {
"task_description": "Add error handling...",
"success": true,
"iterations": 8
},
"executor_initialized": true,
"execution_history": [...]
}使用示例
示例1:自动代码重构
# Using from Python with anthropic client
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=4096,
tools=[...], # MCP tools from claude-code-control
messages=[{
"role": "user",
"content": "Refactor the authentication module to use async/await"
}]
)示例2:自主修复Bug
# Using from Claude Code
claude --prompt "Fix the memory leak in server.py using the claude-code-control MCP"示例3:集成测试
# Using from workflow automation
from anthropic import Anthropic
client = Anthropic()
# Execute task
result = await mcp_execute_tool(
"execute_code_task",
{
"task_description": "Add integration tests for the API endpoints",
"working_directory": "${AGENTIC_SYSTEM_PATH:-/opt/agentic}/api",
"context_files": ["server.py", "models.py"]
}
)
print(f"Tests created: {result['file_changes']['files_created']}")集成点
临时工作流
@workflow.defn
class CodeTaskWorkflow:
@workflow.run
async def run(self, task_description: str) -> dict:
# Execute code task via MCP
result = await workflow.execute_activity(
execute_via_mcp,
args=[task_description],
start_to_close_timeout=timedelta(minutes=5)
)
return resultn8n工作流
使用HTTP请求节点调用MCP服务器:
{
"method": "POST",
"url": "http://localhost:PORT/execute",
"body": {
"tool": "execute_code_task",
"arguments": {
"task_description": "{{$json.task}}"
}
}
}智能代理
from intelligent_agents.sdk_agents import ClaudeAgent
agent = ClaudeAgent()
# Agent uses MCP tools automatically
result = agent.execute(
"Refactor authentication to use OAuth2",
mcp_tools=["claude-code-control"]
)文件跟踪
MCP服务器跟踪执行过程中的所有文件更改:
- 已创建文件:写入新文件
- 已修改的文件:现有文件已更改
- 已删除的文件:文件已删除
- 读取文件:用于读取的文件
变更包括:
- 文件哈希(SHA256)
- 尺寸变化
- 时间戳
- 线路变更统计数据(如果可用)
错误处理
服务器优雅地处理错误:
- 工具执行错误:返回错误详细信息
- 文件操作错误:已记录并返回给呼叫者
- 命令执行错误:已捕获退出代码和stderr
- API错误:随上下文传播的人类API错误
安全考虑
- API密钥管理:商店
ANTHROPIC_API_KEY安全地 - 文件访问:仅限于指定的工作目录
- 命令执行:Shell命令以用户权限运行
- 输入验证:执行前验证所有输入
- 输出消毒:从输出中过滤敏感数据
演出
- 典型任务执行:5-30秒
- 文件 操作:每个文件小于1秒
- 代码搜索:对于大型代码库,\<5秒
- 内存使用:执行过程中约50-100MB
故障排除
服务器无法启动
# Check API key
echo $ANTHROPIC_API_KEY
# Test server directly
python3 server.py
# Check logs
tail -f ~/.claude/logs/mcp-servers.log工具执行失败
# Verify dependencies
pip3 list | grep anthropic
pip3 list | grep mcp
# Test executor
python3 test_executor.py文件跟踪问题
# Check permissions
ls -la ${AGENTIC_SYSTEM_PATH:-/opt/agentic}/
# Verify working directory
pwd发展
运行测试
cd ${AGENTIC_SYSTEM_PATH:-/opt/agentic}/mcp-servers/claude-code-control-mcp
python3 test_server.py添加新工具
- 将工具定义添加到
list_tools() - 在中实现处理程序
call_tool() - 更新文档
- 添加测试
调试
启用调试日志记录:
logging.basicConfig(level=logging.DEBUG)路线图
- \[\]批量任务执行
- \[\]任务排队和调度
- \[\]与增强型内存MCP集成
- \[\]代码分析工具(复杂性、覆盖率)
- \[\]Git集成(提交、推送、PR创建)
- \[\]支持多文件重构
- \[\]流媒体执行更新
- \[\]用于监视的Web UI
贡献
欢迎投稿!需要改进的地方:
- 其他工具实现
- 性能优化
- 增强的错误处理
- 文档改进
- 测试覆盖率
许可证
代理系统项目的一部分。请参阅主许可证文件。
支持
对于问题和疑问:
- 检查日志:
~/.claude/logs/ - 审查MCP文件:https://modelcontextprotocol.io
- API无烟煤文件:https://docs.anthropic.com
相关文件
______________________________________________________________________
MCP生态系统的一部分
该服务器与其他MCP服务器集成,以实现全面的AGI功能:
| 服务器 | 用途 |
|---|---|
| 增强记忆mcp | 语义搜索的四层持久记忆 |
| 代理运行时mcp | 持久任务队列和目标分解 |
| agi mcp | 使用21个工具进行完整的AGI编排 |
| 集群执行mcp | 跨节点的分布式任务路由 |
| 节点聊天mcp | 节点间AI通信 |
| 恩伯·麦克普 | 仅生产政策执行 |
看 代理系统oss 对于完整的框架。
