Skip to main content
Glama
MCP Python Interpreter

MCP Python Interpreter

by yzfly

MCP Python解释器

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

特征

  • 环境管理:列出并使用不同的 Python 环境(系统和 conda)

  • 代码执行:在任何可用环境中运行 Python 代码或脚本

  • 软件包管理:列出已安装的软件包并安装新的软件包

  • 文件操作:

    • 读取任何类型的文件(文本、源代码、二进制)

    • 写入文本和二进制文件

  • Python 提示符:用于常见 Python 任务(如函数创建和调试)的模板

Related MCP server: MCP Python Toolbox

安装

您可以使用 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桌面

  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 环境中安装请求包”

  • “使用以下参数运行 data_analysis.py:--input=data.csv --output=results.csv”

文件处理能力

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

  • 读取最大 1MB 的文本和二进制文件

  • 写入文本和二进制文件

  • 源代码文件的语法高亮

  • 二进制文件的十六进制表示

  • 严格的文件路径安全(仅在工作目录内)

安全注意事项

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

  • 隔离工作目录

  • 文件大小限制

  • 防止在工作目录之外写入

  • 显式覆盖保护

在运行您不完全理解的代码或文件操作时务必保持谨慎。

执照

麻省理工学院

Available Tools

10 tools
clear_sessionC
Clear a REPL session's state and history.

Args:
    session_id: Session ID to clear

Inputs: session_id. Returns: result

install_packageB
Install a Python package in the specified environment.

Args:
    package_name: Name of the package to install
    environment: Name of the Python environment
    upgrade: Whether to upgrade if already installed
    timeout: Maximum execution time in seconds

Inputs: package_name (required), timeout, upgrade, environment. Returns: result

list_directoryB
List all Python files in a directory.

Args:
    directory_path: Path to directory (empty for working directory)

Inputs: directory_path. Returns: result

list_installed_packagesC
List installed packages for a specific Python environment.

Args:
    environment: Name of the Python environment

Inputs: environment. Returns: result

list_python_environmentsB

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

Inputs: no named parameters. Returns: result

list_sessionsB

List all active REPL sessions.

Inputs: no named parameters. Returns: result

read_fileA
Read the content of any file, with size limits for safety.

Args:
    file_path: Path to the file
    max_size_kb: Maximum file size to read in KB

Inputs: file_path (required), max_size_kb. Returns: result

run_python_codeA
Execute Python code with flexible execution modes.

Args:
    code: Python code to execute
    execution_mode: Execution mode - "inline" (default, fast, in-process) or "subprocess" (isolated)
    session_id: Session ID for inline mode to maintain state across executions
    environment: Python environment name (only for subprocess mode)
    save_as: Optional filename to save the code before execution (within --dir by default)
    timeout: Maximum execution time in seconds (only enforced for subprocess mode)
    overwrite: Allow replacing an existing save_as file (default: False)

Returns:
    Execution result with output

Execution modes:
- "inline" (default): Executes code in the current process. Fast and reliable,
  maintains session state. Use for most code execution tasks.
- "subprocess": Executes code in a separate Python process. Use when you need
  environment isolation or a different Python environment.

Inputs: code (required), save_as, timeout, overwrite, +3 more. Returns: result

run_python_fileB
Execute a Python file (always uses subprocess for file execution).

Args:
    file_path: Path to the Python file to execute
    environment: Name of the Python environment to use
    arguments: List of command-line arguments to pass to the script
    timeout: Maximum execution time in seconds (default: 300)

Inputs: file_path (required), timeout, arguments, environment. Returns: result

write_fileB
Write content to a file.

Args:
    file_path: Path to the file to write
    content: Content to write
    overwrite: Whether to overwrite if exists

Inputs: content (required), file_path (required), overwrite. Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.5/5.0

    Scored across 10 tools

    Disambiguation4/5

    Most tools target clearly distinct resources or actions (execution, files, environments, packages, sessions). The main potential confusion is between run_python_code and run_python_file, but the descriptions make the inline-code vs file-execution split explicit, so an agent can select correctly.

    Naming Consistency5/5

    All ten tools follow a consistent snake_case verb_noun pattern: list_*, run_*, install_*, clear_*, read_*, write_*. No mixed conventions or vague single-word names.

    Tool Count5/5

    Ten tools is a well-scoped surface for a Python interpreter server, covering execution, file I/O, environments, packages, and sessions without redundancy. Each tool earns its place.

    Completeness4/5

    Core lifecycle is covered: execute code/files, read/write files, list/install packages, list environments and sessions, and clear sessions. Minor gaps exist (no uninstall_package, no file delete, no environment creation), but agents can work around these via run_python_code.

    Maintenance

    ActivitySlowing
    ResponsivenessLimited data

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      A Model Context Protocol server that allows LLMs to execute shell commands and receive their output in a controlled manner.
      7
      MIT
    • A
      license
      Not graded
      quality
      F
      maintenance
      A Model Context Protocol server that enables AI assistants like Claude to perform Python development tasks through file operations, code analysis, project management, and safe code execution.
      9
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      A Model Context Protocol (MCP) server that allows AI models to safely access and interact with local file systems, enabling reading file contents, listing directories, and retrieving file metadata.
      10
      MIT
    • F
      license
      A
      quality
      Not graded
      maintenance
      A Model Context Protocol server that allows LLMs to interact with Python environments, enabling code execution, file operations, package management, and development workflows.
      9
      -