python-repl-mcp
A Windows MCP stdio server that keeps one persistent CPython interpreter per
explicit session. State, imports and helpers survive between tool calls that
name the same session. Connecting to the server does not start a worker:
repl_open_session starts one and returns the opaque session_uid that every
other tool must pass.
This page describes the 0.3.0 explicit-session API. The candidate is vetted locally before publication. HANDOFF.md and HANDOFF-test-report.md are historical 0.2.6-era evidence and document the removed per-chat routing.
Tools
| Tool | Purpose |
|---|---|
repl_open_session(workdir) |
Start a ready interpreter in an existing absolute directory; returns session_uid, workdir and kernel_id. |
repl_execute(session_uid, code, timeout_seconds=30) |
Run one complete Python cell in that session; returns stdout, stderr, the final value, and a structured error report. |
repl_run_file(session_uid, file_path, args=[], timeout_seconds=30) |
Run a .py file in the same persistent namespace with __file__ and sys.argv; relative paths resolve against the directory the session was opened with. |
repl_reset(session_uid) |
Replace that session's interpreter with a clean one; the UID and bound directory stay the same. |
repl_stop(session_uid) |
End that session's interpreter; the UID becomes invalid. |
Session model
One stdio flow works for every host; no host metadata is required.
- Call
repl_open_sessionwith an existing absolute directory. Each call mints a new 128-bitsecrets.token_urlsafe(16)UID and starts a ready worker with that directory as its cwd. Two opens of the same absolute folder are two independent interpreters with the same cwd, never one shared worker. - Keep the UID in the chat's own context and pass it as
session_uidto every later call. There is no chat key,_metafield, environment variable, default session or "pick a chat" argument; the UID is the only selector. repl_resetkeeps the UID and the bound directory but replaces the kernel, so the namespace is clean and relative paths keep resolving the same way.repl_stopretires the UID once the worker exit is confirmed. Idle expiry, a cell timeout, worker death and a fatal protocol fault also retire it.- An empty, unknown, stopped or expired UID gets the same tool error:
InvalidSession: Open a session with repl_open_session first.The reply never echoes the UID. - If the chat loses the UID (compaction, restart), open a new session for a clean interpreter. The server writes no UID files and no host environment variable holds one.
- A UID is valid only while its stdio dispatcher process lives. If the host restarts the dispatcher, old UIDs are invalid. A host that keeps one dispatcher alive across a chat restart keeps old UIDs valid until stop, idle expiry or worker loss; the client should still discard handles and open anew.
- A dispatcher owns at most 16 live sessions by default;
repl_stopfrees a slot.PYTHON_REPL_MCP_IDLE_TIMEOUTsets the idle window in seconds (default1800);0explicitly disables idle expiry. The window starts as soon as a worker is ready, even before its first cell. Rejected calls do not extend it, and an executing cell is not idle.
Requirements
- Windows 10/11, CPython 3.14.x (verified on 3.14.10), and
uv. - Use the interpreter or venv that launches the server; its site-packages are available to executed cells. User-site imports are available only when enabled.
- Optional
PYTHON_REPL_MCP_IDLE_TIMEOUT, in seconds: default1800,0disables.
Install and run
For candidate testing, install the local 0.3.0 wheel in a disposable
environment. Published 0.2.6 artifacts use the previous per-chat API.
uv venv --python C:\Z\python\python.exe <candidate-venv>
uv pip install --python <candidate-venv>\Scripts\python.exe <candidate-wheel>
See native host acceptance for the DSH and Command Code smoke tests. The dispatcher and its workers use the interpreter selected in the host entry.
Configure the host to launch the interpreter used to install the package. Replace
the example paths with their absolute values. The server speaks MCP over
stdin/stdout only. OpenCode v2.4.2 uses the mcp.servers.<name> shape; keep the
entry disabled: true and connect it explicitly so no dispatcher starts when a
project opens:
{
"mcp": {
"servers": {
"python-repl": {
"type": "local",
"protocol": "2026-07-28",
"pool": true,
"disabled": true,
"command": ["<candidate-venv>/Scripts/python.exe", "-P", "-B", "-m", "python_repl_mcp.server"],
"environment": {
"PYTHONNOUSERSITE": "1"
},
"cwd": "<dispatcher-directory>"
}
}
}
}
Keep disabled: true and connect the registered entry when the project needs
Python: POST /api/experimental/mcp/python-repl/connect for that location. Until
Connect, the python-repl tools are absent from the agent catalog. With
pool: true and identical entries, connected locations share one dispatcher
process; each chat still opens its own UID-addressed session, so no metadata or
per-chat process is needed.
DSH and Command Code call the same five tools over stdio and need no metadata field. The agent itself carries the returned UID between turns.
Behaviour and limits
input()andgetpass()return a structuredInputRequiredreport. Ask in chat only for non-secret input. Use an approved local mechanism for secrets; do not put them in chat, Python cells or tool arguments.repl_executereturns the final expression'sresultas a bounded repr.result_typenames the value's type,result_jsoncarries the parsed JSON value when the result is JSON-serialisable, andresult_truncatedmarks a capped repr.kernel_ididentifies the worker;workdiris the session's bound directory.repl_open_sessionalso returns thesession_uid. These fields are optional and appear only when meaningful.- The interpreter starts in the session's bound directory; an intentional
os.chdir()changes its cwd for subsequent cells.repl_run_fileresolves an ordinary relative path against the bound directory and rejects a Windows drive-relative (C:script.py) or root-relative path as ambiguous. It restores the previous__file__/sys.argvafter the script. File and argument size are checked before dispatch, with a private frame cap of 2 MiB including overhead. The read runs in a short-lived isolated interpreter under the 15 s startup budget; expiry is aFileError. Files written by a script (for example a matplotlib PNG) remain on disk. stdout/stderrare captured per cell, bounded to 16,384 characters each;truncatedreports a cut snapshot, andcapture_complete=falsereports unavailable output or unconfirmed cleanup of an interrupted cell. Worker stdio is UTF-8. Disk use by a surviving child process is not capped.- After idle exit, a timeout or a fatal protocol error, the server frees the slot
once it confirms the worker tree has exited, and the old UID is invalid. A
capture directory that was locked at that moment is retried once by the next
acknowledged
repl_stop(of any live session) or by normal shutdown; a still-locked path is logged and dropped. - An uncaught Python exception is a normal execution report; it does not kill
the interpreter.
os._exit()kills the worker and retires the UID; open a new session for a clean interpreter. - A timeout terminates the whole worker process tree (Windows Job Object), loses in-memory state and retires the UID.
- At most 16 sessions are owned at once; release one with
repl_stop. - Worker startup hooks must be quiet on stdout. Noisy startup output fails closed.
Tests
$env:PYTHONPATH = "src"
python -m pytest
python -m ruff check src tests
python -m pyright
References
- MCP-сервер проверки и запуска кода на Python — summary and design notes in docs/references.md.
Metadata
Release files for python-repl-mcp 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python_repl_mcp-0.3.0.tar.gz | 81.7 kB | With provenance | Details for python_repl_mcp-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_repl_mcp-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for python_repl_mcp-0.3.0-py3-none-any.whl |
Total release size: 125.7 kB
Release files / python_repl_mcp-0.3.0.tar.gz
| Download URL | python_repl_mcp-0.3.0.tar.gz |
|---|---|
| Size | 81.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5ed1186c105a29c23cddfbb0878accfb5e5c1909dbeebacf10ff8975fa4e2353
|
|
BLAKE2b-256 checksum How to use checksums |
61796fec49cee1a804bba0e479f8b293af230952209ead0a5af839dd0afac056
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logRelease files / python_repl_mcp-0.3.0-py3-none-any.whl
| Download URL | python_repl_mcp-0.3.0-py3-none-any.whl |
|---|---|
| Size | 44.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bcd470bbae286648e2e14a25dd060a80a5440873f3b628530c66bddfcad383c8
|
|
BLAKE2b-256 checksum How to use checksums |
3b67845a96712bcb5a371af347006bfd1ac5c92955dcfe81a94c53700e7a7e1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logSecurity for python-repl-mcp 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for python_repl_mcp-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.