python-session-mcp
python-session-mcp
在保持存活的会话中运行 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: mcp-python-repl
安装
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" }
}
}
}工具
会话
工具 | 用途 |
| 哪个解释器,以及它实际拥有哪些包 |
| 忘记一切,可选地重启解释器 |
运行代码
工具 | 用途 |
| 主要工具。 在持久会话中运行代码 |
| 当前定义了哪些内容 |
| 类型、形状、dtypes 以及对象的一瞥 |
| 带回一个可 JSON 表示的值 |
数据
工具 | 用途 |
| 读取 |
| 写出 DataFrame,并创建缺失的文件夹 |
| 形状、dtypes、缺失计数和前几行 |
| 包含偏度和峰度的描述性统计 |
| 皮尔逊、斯皮尔曼或肯德尔 |
估计
工具 | 用途 |
| OLS,可选 HC 或 HAC 标准误 |
| 一次调用中完成 Breusch-Godfrey、White 和 Jarque-Bera 检验 |
| ADF 或 KPSS,差分直至平稳 |
图表
工具 | 用途 |
| 折线图、散点图、直方图、箱线图或条形图——可选直接保存到文件 |
| 将打开的 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.
This server cannot be deployed
Maintenance
Related MCP Connectors
Multiple MCP tools, persistent graph memory, token-saving data pointers, and more.
MCP server for building and testing AI agents with multi-model experimentation and insights.
Persistent memory and cross-session learning for AI coding assistants (hosted remote MCP).
MCP server for your apps' tools and custom tools, plus hosted AI agents and approval-gated workflows
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables LLMs to execute Python code in isolated sandboxes with file operations and MCP integration, supporting multi-round execution and plot capture.1-
- AlicenseAqualityDmaintenanceA 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.1235 PyPI1MIT
- AlicenseNot gradedqualityDmaintenanceMCP server for secure, session-based Python code execution in Docker containers, enabling LLM applications to run code, manage state, and access files.9MIT
- AlicenseNot gradedqualityBmaintenanceEnables LLM agents to run persistent Python code in a workspace-scoped shared kernel, with tools for execution, background tasks, file operations, and external MCP integrations.AGPL 3.0