python-session-mcp
Enables LLM clients to run Python code in a persistent, user-selected interpreter via MCP, with tools for data loading, summaries, regressions, diagnostics, and plotting, while keeping sessions alive between calls and isolating crashes.
README
python-session-mcp
Run Python in a session that stays alive, and expose it to LLM clients over the Model Context Protocol.
Author: Dr Merwan Roudane
What makes it different
The session persists. A DataFrame loaded in one call is still there in the next, so an analysis is built up in steps rather than resent whole each time.
Your interpreter, not the server's. The server may well be installed under
a bare Python with no pandas in it. The interpreter that runs your code is
chosen separately: a conda or Anaconda installation is preferred when one is
present, and PYTHON_MCP_INTERPRETER overrides that.
A crash costs one process. Code runs in a worker, not in the server. Exhaust
memory, call sys.exit, crash a C extension — the worker is replaced and the
server carries on. It also keeps user code away from the server's stdin, which
under MCP is the JSON-RPC stream itself.
Install
pip install python-session-mcp
Library use
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
A final expression is shown the way a REPL would, so df.head() on its own
displays the frame without print().
MCP server use
{
"mcpServers": {
"python": {
"command": "python-session-mcp",
"env": { "PYTHON_MCP_INTERPRETER": "C:\\Users\\you\\anaconda3\\python.exe" }
}
}
}
Tools
Session
| Tool | Purpose |
|---|---|
python_status |
Which interpreter, and which packages it actually has |
reset_namespace |
Forget everything, optionally restarting the interpreter |
Running code
| Tool | Purpose |
|---|---|
run_python |
Main tool. Run code in the persistent session |
list_names |
What is currently defined |
describe_object |
Type, shape, dtypes and a peek at one object |
get_value |
Bring a JSON-representable value back |
Data
| Tool | Purpose |
|---|---|
load_data |
Read .csv, .xlsx, .dta, .parquet, .sav or .json |
save_data |
Write a DataFrame out, creating missing folders |
preview_data |
Shape, dtypes, missing counts and the first rows |
summary_statistics |
Descriptives with skew and kurtosis |
correlation |
Pearson, Spearman or Kendall |
Estimation
| Tool | Purpose |
|---|---|
regression |
OLS, optionally with HC or HAC standard errors |
regression_diagnostics |
Breusch-Godfrey, White and Jarque-Bera in one call |
unit_root |
ADF or KPSS, differencing until stationary |
Charts
| Tool | Purpose |
|---|---|
plot |
line, scatter, hist, box or bar — optionally straight to a file |
save_figure |
Write the open matplotlib figure to a file |
Errors
Failures name the exception and the line of your code, and leave the session intact:
Python error: NameError on line 2: name 'undefined_name' is not defined
Anything printed before the failure is reported with it, since that output is often what explains the failure.
Figures
A plot cannot come back as text. Draw it, then save it:
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
Worth knowing
- The interpreter is separate from the server's. Check
python_statusbefore relying on a package being there. - State is a convenience and a hazard. Names persist, so a stale variable
from an earlier step can quietly feed a later one.
reset_namespacewhen starting something new. run_pythonexecutes whatever it is given, in your environment, with your file access. That is the point of it, and worth being deliberate about.- The final expression is echoed. A long DataFrame will print in full unless you slice it.
Tests
python tests/test_live.py # 41 tests
They cover persistence, error reporting with line numbers, JSON round trips, figure writing, recovery after user code kills the worker outright, and every data and estimation tool against generated data with known coefficients.
Checked against EViews on the same data, the regression agrees to every printed digit -- coefficients, R-squared, Durbin-Watson, and the Breusch-Godfrey and Jarque-Bera statistics alike.
Licence
MIT. Copyright (c) 2026 Merwan Roudane.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。