MCP Python工具箱
一个模型上下文协议(MCP)服务器,为Python开发提供了一套全面的工具,使像Claude这样的AI助手能够有效地处理Python代码和项目。
概述
MCP Python Toolbox实现了一个模型上下文协议服务器,使Claude能够通过标准化的接口执行Python开发任务。它使克劳德能够:
- 在工作区中读取、写入和管理文件
- 分析、格式化和lint Python代码
- 管理虚拟环境和依赖关系
- 安全执行Python代码
特性
文件操作(FileOperations)
- 工作区目录中的安全文件操作
- 路径验证,防止在工作区外进行未经授权的访问
- 使用特定行操作读取和写入文件
- 创建和删除文件和目录
- 列出目录内容和详细的元数据(大小、类型、修改时间)
- 写入文件时自动创建父目录
代码分析(CodeAnalyzer)
- 使用AST解析和分析Python代码结构
- 提取以下详细信息:
- 导入语句及其别名 - 带有参数和装饰器的函数定义 - 带有基类和方法的类定义 - 全局变量赋值
- 使用以下方式格式化代码:
- 黑色(默认) - PEP8(使用autopep8)
- 使用Pylint进行全面的代码过滤,并提供详细的报告
项目管理(ProjectManager)
- 使用pip支持创建和管理虚拟环境
- 灵活的依赖关系管理:
- 从requirements.txt安装 - 从pyproject.toml安装 - 支持特定的软件包版本
- 高级依赖关系处理:
- 检查包之间的版本冲突 - 列出所有已安装的软件包及其版本 - 将软件包更新到特定版本 - 从当前环境生成requirements.txt
代码执行(CodeExecutor)
- 在受控环境中执行Python代码
- 使用项目的虚拟环境实现一致的依赖关系
- 代码执行的临时文件管理
- 捕获stdout、stderr和退出代码
- 支持自定义工作目录
安装
- 克隆存储库:
git clone https://github.com/gianlucamazza/mcp_python_toolbox.git
cd mcp_python_toolbox- 创建并激活虚拟环境:
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# or
.venv\Scripts\activate # Windows- 在开发模式下安装软件包:
pip install -e ".[dev]"用法
作为CLI工具运行
启动服务器的最简单方法是使用CLI:
# Start with current directory as workspace
python -m mcp_python_toolbox
# Or specify a workspace directory
python -m mcp_python_toolbox --workspace /path/to/your/project使用Claude Desktop进行设置
Claude Desktop可以自动启动和管理MCP Python工具箱服务器。以下是如何配置它:
- 如上所述安装和设置MCP Python工具箱
- 在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": ""
}
}- 自定义路径以匹配您的环境
- Claude Desktop将在需要时自动启动MCP服务器
- Claude现在可以通过MCP接口访问Python开发工具
程序化使用
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")
# Read file contents
content = file_ops.read_file("src/example.py")
# Read specific lines
lines = file_ops.read_file("src/example.py", start_line=10, end_line=20)
# Write to file
file_ops.write_file("output.txt", "Hello, World!")
# Append to file
file_ops.write_file("log.txt", "New entry\n", mode='a')
# List directory contents
contents = file_ops.list_directory("src")
for item in contents:
print(f"{item['name']} - {item['type']} - {item['size']} bytes")代码分析
from mcp_python_toolbox.core import CodeAnalyzer
analyzer = CodeAnalyzer(workspace_root="/path/to/project")
# Analyze Python file structure
analysis = analyzer.parse_python_file("src/example.py")
print(f"Found {len(analysis['functions'])} functions")
print(f"Found {len(analysis['classes'])} classes")
# Format code
formatted = analyzer.format_code(code, style='black')
# Lint code
issues = analyzer.lint_code("src/example.py")
for issue in issues:
print(f"Line {issue['line']}: {issue['message']}")项目管理
from mcp_python_toolbox.core import ProjectManager
pm = ProjectManager(workspace_root="/path/to/project")
# Create virtual environment
pm.create_virtual_environment()
# Install dependencies
pm.install_dependencies() # from requirements.txt or pyproject.toml
pm.install_dependencies("requirements-dev.txt") # from specific file
# Check for conflicts
conflicts = pm.check_dependency_conflicts()
if conflicts:
print("Found dependency conflicts:")
for conflict in conflicts:
print(f"{conflict['package']} requires {conflict['requires']}")
# Update packages
pm.update_package("requests") # to latest
pm.update_package("flask", version="2.0.0") # to specific version代码执行
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"Output: {result['stdout']}")
print(f"Errors: {result['stderr']}")
print(f"Exit code: {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 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
致谢
- 实施 模型上下文协议 规格
- 使用现代Python开发工具和最佳实践构建
- 使用行业标准格式化(黑色)和linting(Pylint)工具
