Python REPL服务器

一个提供持久化执行状态的Python REPL服务器,用于在MCP客户端中交互式执行Python代码。

  • by MarselScheer
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 MarselScheer/mcp-pyrepl

使用说明

mcp-pyrepl

几乎所有东西都是用vibe编码的https://eca.dev.我不会妥善维护这个仓库。目前,它只是一个游乐场!

一个MCP(模型上下文协议)服务器,为Python REPL提供执行状态。

概述

此MCP服务器允许您执行Python代码,同时保持执行之间的状态。变量和全局变量在服务器进程中保持不变,但不会持久化到磁盘。

特性

  • 使用持久全局状态执行Python代码
  • 三种可用工具: execute, get_globals,以及 reset_globals
  • 状态在服务器进程中持续存在(服务器重新启动时重置)

安装

先决条件

安装依赖项

make install

或者直接使用紫外线:

uv sync

以可编辑模式安装

make install-editable

或者:

uv pip install -e .

运行服务器

开发模式

make run

或者:

uv run python src/main.py

服务器将启动,可以通过MCP客户端连接。

与MCP客户端一起使用

克劳德桌面版

将此添加到您的 claude_desktop_config.json:

{
  "mcpServers": {
    "pyrepl": {
      "command": "uv",
      "args": ["run", "python", "src/main.py"],
      "env": {},
      "cwd": "/path/to/mcp-pyrepl"
    }
  }
}

其他MCP客户端

服务器公开了三个工具:

1.执行

执行Python代码并获取结果。

# Example: Assign a variable
execute(code="x = 5")
# Returns: {"success": true, "result": "5", "globals_keys": ["x"]}

# Example: Use a previously assigned variable
execute(code="x * 2")
# Returns: {"success": true, "result": "10", "globals_keys": ["x"]}

# Example: Import and use a module
execute(code="import math; math.sqrt(16)")
# Returns: {"success": true, "result": "4.0", "globals_keys": ["x", "math"]}

2.获取全局

查看所有当前全局变量。

get_globals()
# Returns: {"x": "5", "math": "<module 'math' from ...>"}

3.重置全局

清除所有全局变量并重新开始。

reset_globals()
# Returns: {"status": "globals reset"}

开发命令

命令 描述
make install 使用uv同步安装依赖项
make install-editable 以可编辑模式安装软件包
make run 运行MCP服务器
make test 运行测试
make lint 运行门楣
make format 格式代码
make all 运行安装、lint、格式化和测试

备注

  • 变量仅在服务器进程中保持不变
  • 服务器重启将重置所有全局变量