Skip to main content
Glama
merwanroudane

python-session-mcp

python-session-mcp

Licence Python

在保持存活的会话中运行 Python,并通过模型上下文协议将其暴露给 LLM 客户端。

作者:Dr Merwan Roudane

它有何不同

会话持久存在。 一次调用中加载的 DataFrame 在下一次调用中仍然存在,因此分析可以逐步构建,而不是每次都整体重新发送。

你的解释器,而非服务器的。 服务器可能安装在没有 pandas 的裸 Python 下。运行你的代码的解释器是单独选择的:当存在 conda 或 Anaconda 安装时,会优先使用它们,而 PYTHON_MCP_INTERPRETER 会覆盖该选择。

一次崩溃只损失一个进程。 代码在 worker 中运行,而不是在服务器中。耗尽内存、调用 sys.exit、崩溃 C 扩展——worker 会被替换,服务器继续运行。这也使你的代码远离服务器的 stdin,而在 MCP 下,stdin 就是 JSON-RPC 流本身。

Related MCP server: LLM Python Code Sandbox

安装

pip install python-session-mcp

库使用

from python_mcp import PythonRunner

with PythonRunner() as py:
    py.run("import pandas as pd, statsmodels.api as sm")
    py.run("df = pd.read_csv('macro.csv')")
    print(py.run("df.describe()"))

    py.run("m = sm.OLS(df['y'], sm.add_constant(df[['x','z']])).fit()")
    print(py.run("m.summary()"))
    print(py.value("m.params.to_dict()"))    # a real Python dict

最终表达式会像 REPL 那样显示,因此单独的 df.head() 就会显示数据框,而无需 print()

MCP 服务器使用

{
  "mcpServers": {
    "python": {
      "command": "python-session-mcp",
      "env": { "PYTHON_MCP_INTERPRETER": "C:\\Users\\you\\anaconda3\\python.exe" }
    }
  }
}

工具

会话

工具

用途

python_status

哪个解释器,以及它实际拥有哪些包

reset_namespace

忘记一切,可选地重启解释器

运行代码

工具

用途

run_python

主要工具。 在持久会话中运行代码

list_names

当前定义了哪些内容

describe_object

类型、形状、dtypes 以及对象的一瞥

get_value

带回一个可 JSON 表示的值

数据

工具

用途

load_data

读取 .csv.xlsx.dta.parquet.sav.json

save_data

写出 DataFrame,并创建缺失的文件夹

preview_data

形状、dtypes、缺失计数和前几行

summary_statistics

包含偏度和峰度的描述性统计

correlation

皮尔逊、斯皮尔曼或肯德尔

估计

工具

用途

regression

OLS,可选 HC 或 HAC 标准误

regression_diagnostics

一次调用中完成 Breusch-Godfrey、White 和 Jarque-Bera 检验

unit_root

ADF 或 KPSS,差分直至平稳

图表

工具

用途

plot

折线图、散点图、直方图、箱线图或条形图——可选直接保存到文件

save_figure

将打开的 matplotlib 图形写入文件

错误

失败会指出异常和你的代码的行号,并保持会话不变:

Python error: NameError on line 2: name 'undefined_name' is not defined

失败前打印的任何内容都会随失败一起报告,因为那些输出往往能解释失败的原因。

图形

图形无法以文本形式返回。先绘制,然后保存:

py.run("import matplotlib; matplotlib.use('Agg')")
py.run("import matplotlib.pyplot as plt; plt.plot(df['x'], df['y'], 'o')")
py.save_figure("figures/scatter.png")     # missing folders are created

值得了解

  • 解释器与服务器的是分开的。 在依赖某个包存在之前,请检查 python_status

  • 状态既是便利也是风险。 名称会持久存在,因此早期步骤中的过时变量可能会悄悄影响后续步骤。开始新任务时使用 reset_namespace

  • run_python 会执行它收到的任何内容,在你的环境中,使用你的文件访问权限。这正是它的意义所在,值得慎重对待。

  • 最终表达式会被回显。 除非你进行切片,否则长 DataFrame 会完整打印。

测试

python tests/test_live.py     # 41 tests

它们涵盖了持久性、带行号的错误报告、JSON 往返、图形写入、用户代码直接杀死工作进程后的恢复,以及针对已知系数生成的数据对每个数据和估计工具的测试。

在与 EViews 相同的数据上进行核对,回归结果与每个打印的数字一致——系数、R 平方、Durbin-Watson 统计量,以及 Breusch-Godfrey 和 Jarque-Bera 统计量。

许可证

MIT. Copyright (c) 2026 Merwan Roudane.

A
license - permissive license
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    Provides persistent IPython shell sessions per conversation with DataFrame-centric architecture, enabling stateful data analysis, CLI tool execution, and integration of external MCP servers within the same workspace context.
    23
    Apache 2.0
  • A
    license
    A
    quality
    D
    maintenance
    A production-grade MCP server providing a persistent Python REPL with multi-session support, sandboxing, and timeout protection, enabling LLM agents to execute Python code across multiple turns with variables that persist between calls.
    12
    1
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    MCP server for secure, session-based Python code execution in Docker containers, enabling LLM applications to run code, manage state, and access files.
    9
    MIT

View all related MCP servers

Related MCP Connectors

  • Persistent memory and cross-session learning for AI coding assistants (hosted remote MCP).

  • MCP server providing access to the Scorecard API to evaluate and optimize LLM systems.

  • MCP server exposing the Backtest360 engine API as tools for AI agents.

View all MCP Connectors

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/merwanroudane/mcp_python'

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