MCP Python Interpreter

MCP Python Interpreter

一个模型上下文协议服务器,允许大型语言模型(LLM)与 Python 环境交互,执行代码,并在指定的工作目录中管理文件。 (Alternative, slightly more formal translation): 一个模型上下文协议服务器,它允许大型语言模型 (LLM) 与 Python 环境进行交互,执行代码,并在指定的工作目录下管理文件。

Category
访问服务器

Tools

read_file

Read the content of any file, with size limits for safety. Args: file_path: Path to the file (relative to working directory or absolute) max_size_kb: Maximum file size to read in KB (default: 1024) Returns: str: File content or an error message

write_file

Write content to a file in the working directory or system-wide if allowed. Args: file_path: Path to the file to write (relative to working directory or absolute if system access is enabled) content: Content to write to the file overwrite: Whether to overwrite the file if it exists (default: False) encoding: File encoding (default: utf-8) Returns: str: Status message about the file writing operation

list_directory

List all Python files in a directory or subdirectory. Args: directory_path: Path to directory (relative to working directory or absolute, empty for working directory)

list_python_environments

List all available Python environments (system Python and conda environments).

list_installed_packages

List installed packages for a specific Python environment. Args: environment: Name of the Python environment (default: default if custom path provided, otherwise system)

run_python_code

Execute Python code and return the result. Code runs in the working directory. Args: code: Python code to execute environment: Name of the Python environment to use (default if custom path provided, otherwise system) save_as: Optional filename to save the code before execution (useful for future reference)

install_package

Install a Python package in the specified environment. Args: package_name: Name of the package to install environment: Name of the Python environment (default if custom path provided, otherwise system) upgrade: Whether to upgrade the package if already installed (default: False)

write_python_file

Write content to a Python file in the working directory or system-wide if allowed. Args: file_path: Path to the file to write (relative to working directory or absolute if system access is enabled) content: Content to write to the file overwrite: Whether to overwrite the file if it exists (default: False)

run_python_file

Execute a Python file and return the result. Args: file_path: Path to the Python file to execute (relative to working directory or absolute if system access is enabled) environment: Name of the Python environment to use (default if custom path provided, otherwise system) arguments: List of command-line arguments to pass to the script

README

MCP Python 解释器

一个模型上下文协议 (MCP) 服务器,允许 LLM 与 Python 环境交互、读取和写入文件、执行 Python 代码以及管理开发工作流程。

功能

  • 环境管理: 列出和使用不同的 Python 环境(系统和 conda)
  • 代码执行: 在任何可用环境中运行 Python 代码或脚本
  • 包管理: 列出已安装的包并安装新的包
  • 文件操作:
    • 读取任何类型的文件(文本、源代码、二进制)
    • 写入文本和二进制文件
  • Python 提示: 用于常见 Python 任务的模板,例如函数创建和调试

安装

您可以使用 pip 安装 MCP Python 解释器:

pip install mcp-python-interpreter

或者使用 uv:

uv install mcp-python-interpreter

与 Claude Desktop 一起使用

  1. 安装 Claude Desktop
  2. 打开 Claude Desktop,点击菜单,然后点击设置
  3. 转到开发者选项卡,然后点击“编辑配置”
  4. 将以下内容添加到您的 claude_desktop_config.json
{
  "mcpServers": {
    "mcp-python-interpreter": {
        "command": "uvx",
        "args": [
            "mcp-python-interpreter",
            "--dir",
            "/path/to/your/work/dir",
            "--python-path",
            "/path/to/your/python"
        ],
        "env": {
            "MCP_ALLOW_SYSTEM_ACCESS": 0
        },
    }
  }
}

对于 Windows:

{
  "mcpServers": {
    "python-interpreter": {
      "command": "uvx",
      "args": [
        "mcp-python-interpreter",
        "--dir",
        "C:\\path\\to\\your\\working\\directory",
        "--python-path",
        "/path/to/your/python"
      ],
        "env": {
            "MCP_ALLOW_SYSTEM_ACCESS": 0
        },
    }
  }
}
  1. 重启 Claude Desktop
  2. 您现在应该在聊天界面中看到 MCP 工具图标

--dir 参数是必需的,用于指定所有文件将被保存和执行的位置。 这有助于通过将 MCP 服务器隔离到特定目录来维护安全性。

前提条件

  • 确保您已安装 uv。 如果没有,请使用以下命令安装:
    curl -LsSf https://astral.sh/uv/install.sh | sh
    
  • 对于 Windows:
    powershell -ExecutionPolicy Bypass -Command "iwr -useb https://astral.sh/uv/install.ps1 | iex"
    

可用工具

Python 解释器提供以下工具:

环境和包管理

  • list_python_environments: 列出所有可用的 Python 环境(系统和 conda)
  • list_installed_packages: 列出特定环境中安装的包
  • install_package: 在特定环境中安装 Python 包

代码执行

  • run_python_code: 在特定环境中执行 Python 代码
  • run_python_file: 在特定环境中执行 Python 文件

文件操作

  • read_file: 读取任何文件类型的内容,具有大小和安全限制
    • 支持带有语法高亮的文本文件
    • 显示二进制文件的十六进制表示
  • write_file: 创建或覆盖具有文本或二进制内容的文件
  • write_python_file: 专门创建或覆盖 Python 文件
  • list_directory: 列出目录中的 Python 文件

可用资源

  • python://environments: 列出所有可用的 Python 环境
  • python://packages/{env_name}: 列出特定环境已安装的包
  • python://file/{file_path}: 获取 Python 文件的内容
  • python://directory/{directory_path}: 列出目录中的所有 Python 文件

提示

  • python_function_template: 生成 Python 函数的模板
  • refactor_python_code: 帮助重构 Python 代码
  • debug_python_error: 帮助调试 Python 错误

使用示例

以下是一些您可以要求 Claude 使用此 MCP 服务器执行的操作示例:

  • "显示我系统上所有可用的 Python 环境"
  • "在我的 conda-base 环境中运行此 Python 代码:print('Hello, world!')"
  • "创建一个名为 'hello.py' 的新 Python 文件,其中包含一个说 hello 的函数"
  • "读取我的 'data.json' 文件的内容"
  • "使用这些设置编写一个新的配置文件..."
  • "列出我的系统 Python 环境中安装的所有包"
  • "在我的系统 Python 环境中安装 requests 包"
  • "使用这些参数运行 data_analysis.py:--input=data.csv --output=results.csv"

文件处理能力

MCP Python 解释器现在支持全面的文件操作:

  • 读取最大 1MB 的文本和二进制文件
  • 写入文本和二进制文件
  • 源代码文件的语法高亮
  • 二进制文件的十六进制表示
  • 严格的文件路径安全(仅在工作目录内)

安全注意事项

此 MCP 服务器可以访问您的 Python 环境和文件系统。 主要安全功能包括:

  • 隔离的工作目录
  • 文件大小限制
  • 防止写入工作目录之外
  • 显式覆盖保护

始终谨慎运行您不完全理解的代码或文件操作。

许可证

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选