一个实现模型上下文协议(MCP)服务器的工具集,专为Python开发设计,使像Claude这样的AI助手能够有效地处理Python代码和项目。
MCP Python 工具箱实现了MCP服务器,通过标准化接口赋予Claude执行Python开发任务的能力。它使Claude能够:
FileOperations)CodeAnalyzer)ProjectManager)requirements.txt安装pyproject.toml安装requirements.txtCodeExecutor)git clone https://github.com/gianlucamazza/mcp_python_toolbox.git
cd mcp_python_toolbox
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# 或
.venv\Scripts\activate # Windows
pip install -e ".[dev]"
最简单的启动服务器的方式是使用CLI:
# 使用当前目录作为工作空间启动
python -m mcp_python_toolbox
# 或指定工作空间目录
python -m mcp_python_toolbox --workspace /path/to/your/project
Claude Desktop可以自动启动和管理MCP Python工具箱服务器。以下是配置方法:
"python-toolbox": {
"command": "/Users/username/path/to/mcp_python_toolbox/.venv/bin/python",
"args": [
"-m",
"mcp_python_toolbox",
"--workspace",
"/Users/username/path/to/workspace"
],
"env": {
"PYTHONPATH": "/Users/username/path/to/mcp_python_toolbox/src",
"PATH": "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"VIRTUAL_ENV": "/Users/username/path/to/mcp_python_toolbox/.venv",
"PYTHONHOME": ""
}
}
from mcp_python_toolbox import PythonToolboxServer
server = PythonToolboxServer(workspace_root="/path/to/your/project")
server.setup()
server.run()
from mcp_python_toolbox.core import FileOperations
file_ops = FileOperations(workspace_root="/path/to/project")
# 读取文件内容
content = file_ops.read_file("src/example.py")
# 读取特定行
lines = file_ops.read_file("src/example.py", start_line=11, end_line=20)
# 写入文件
file_ops.write_file("output.txt", "Hello, World!")
# 追加到文件
file_ops.write_file("log.txt", "New entry\n", mode='a')
# 列出目录内容
contents = file_ops.list_directory("src")
for item in contents:
print(f"{item['name']} - {item['type']} - {item['size']} 字节")
from mcp_python_toolbox.core import CodeAnalyzer
analyzer = CodeAnalyzer(workspace_root="/path/to/project")
# 分析Python文件结构
analysis = analyzer.parse_python_file("src/example.py")
print(f"找到 {len(analysis['functions'])} 个函数")
print(f"找到 {len(analysis['classes'])} 个类")
# 格式化代码
formatted = analyzer.format_code(code, style='black')
# 检查代码
issues = analyzer.lint_code("src/example.py")
for issue in issues:
print(f"第 {issue['line']} 行:{issue['message']}")
from mcp_python_toolbox.core import ProjectManager
pm = ProjectManager(workspace_root="/path/to/project")
# 创建虚拟环境
pm.create_virtual_environment()
# 安装依赖项
pm.install_dependencies() # 从 requirements.txt 或 pyproject.toml
pm.install_dependencies("requirements-dev.txt") # 从特定文件
# 检查冲突
conflicts = pm.check_dependency_conflicts()
if conflicts:
print("发现依赖项冲突:")
for conflict in conflicts:
print(f"{conflict['package']} 需要 {conflict['requires']}")
# 更新包
pm.update_package("requests") # 更新到最新版本
pm.update_package("flask", version="2.0.0") # 更新到特定版本
from mcp_python_toolbox.core import CodeExecutor
executor = CodeExecutor(workspace_root="/path/to/project")
code = '''
def greet(name):
return f"Hello, {name}!"
print(greet("World"))
'''
result = executor.execute_code(code)
print(f"输出:{result['stdout']}")
print(f"错误:{result['stderr']}")
print(f"退出码:{result['exit_code']}")
pytest
mypy src/mcp_python_toolbox
pylint src/mcp_python_toolbox
black src/mcp_python_toolbox
git checkout -b feature/amazing-feature)git commit -m '添加一些惊人的功能')git push origin feature/amazing-feature)本项目采用MIT许可证 - 查看LICENSE文件获取详情。