Skip to main content
Glama

MCP Python 工具箱

模型上下文协议 (MCP) 服务器为 Python 开发提供了一套全面的工具,使像 Claude 这样的 AI 助手能够有效地使用 Python 代码和项目。

概述

MCP Python Toolbox 实现了一个模型上下文协议服务器,使 Claude 能够通过标准化接口执行 Python 开发任务。它使 Claude 能够:

  • 在工作区内读取、写入和管理文件

  • 分析、格式化和 lint Python 代码

  • 管理虚拟环境和依赖项

  • 安全地执行 Python 代码

Related MCP server: Hass-MCP

特征

文件操作( FileOperations

  • 工作区目录中的安全文件操作

  • 路径验证以防止工作区外未经授权的访问

  • 使用特定于行的操作来读取和写入文件

  • 创建和删除文件和目录

  • 列出目录内容及其详细元数据(大小、类型、修改时间)

  • 写入文件时自动创建父目录

代码分析( CodeAnalyzer

  • 使用 AST 解析和分析 Python 代码结构

  • 提取有关以下内容的详细信息:

    • 导入语句及其别名

    • 带有参数和装饰器的函数定义

    • 具有基类和方法的类定义

    • 全局变量赋值

  • 使用以下格式代码:

    • 黑色(默认)

    • PEP8(使用 autopep8)

  • 使用 Pylint 进行全面的代码检查,并提供详细的报告

项目管理( ProjectManager

  • 使用 pip 支持创建和管理虚拟环境

  • 灵活的依赖管理:

    • 从 requirements.txt 安装

    • 从 pyproject.toml 安装

    • 支持特定软件包版本

  • 高级依赖关系处理:

    • 检查包之间的版本冲突

    • 列出所有已安装的软件包及其版本

    • 将软件包更新至特定版本

    • 从当前环境生成 requirements.txt

代码执行( CodeExecutor

  • 在受控环境中执行 Python 代码

  • 使用项目的虚拟环境来实现一致的依赖关系

  • 代码执行的临时文件管理

  • 捕获 stdout、stderr 和退出代码

  • 支持自定义工作目录

安装

  1. 克隆存储库:

git clone https://github.com/gianlucamazza/mcp_python_toolbox.git cd mcp_python_toolbox
  1. 创建并激活虚拟环境:

python -m venv .venv source .venv/bin/activate # Linux/Mac # or .venv\Scripts\activate # Windows
  1. 以开发模式安装包:

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 Toolbox 服务器。配置方法如下:

  1. 按照上述说明安装并设置 MCP Python 工具箱

  2. 在 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": "" } }
  1. 自定义路径以匹配您的环境

  2. Claude Desktop 将在需要时自动启动 MCP 服务器

  3. 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

贡献

  1. 分叉存储库

  2. 创建你的功能分支( git checkout -b feature/amazing-feature

  3. 提交您的更改( git commit -m 'Add some amazing feature'

  4. 推送到分支( git push origin feature/amazing-feature

  5. 打开拉取请求

执照

该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。

致谢

  • 实现模型上下文协议规范

  • 采用现代 Python 开发工具和最佳实践构建

  • 使用行业标准格式化(Black)和 linting(Pylint)工具

-
security - not tested
F
license - not found
-
quality - not tested

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gianlucamazza/mcp_python_toolbox'

If you have feedback or need assistance with the MCP directory API, please join our Discord server