python-repl-mcp
A Windows MCP stdio server that keeps one persistent CPython interpreter per OpenCode chat. State, imports and helpers survive between tool calls. Connecting to the server does not start a worker.
For another agent, start with HANDOFF.md: install, OpenCode configuration, tool rules and known limits. The local completion plan tracks the remaining fixes and acceptance on OpenCode, DSH and Command Code.
Tools
| Tool | Purpose |
|---|---|
repl_execute(code, timeout_seconds=30) |
Run one complete Python cell; returns stdout, stderr, the final value, and a structured error report. |
repl_run_file(file_path, args=[], timeout_seconds=30) |
Run a .py file in the same persistent namespace with __file__ and sys.argv; useful for AtlaWire workflows and scripts that save plots to disk. |
repl_reset() |
End the chat's interpreter and wait for a clean replacement to be ready. |
repl_stop() |
End the chat's interpreter without replacement. |
Standalone session resolution: OpenCode's _meta["ai.opencode/sessionID"] when present;
otherwise PYTHON_REPL_MCP_SESSION; otherwise the per-process "default" key.
There is no session tool argument. On a host without per-call metadata,
run one server process per chat to isolate state. An environment variable is
fixed for the lifetime of a process and cannot distinguish chats sharing it.
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_SESSIONfor hosts that do not send OpenCode metadata.
Install and run
uv is the standard installer. The published package is python-repl-mcp:
uv venv --python 3.14 .venv
uv pip install --python .\.venv\Scripts\python.exe python-repl-mcp
.\.venv\Scripts\python.exe -P -m python_repl_mcp.server
Without a package index, the wheel attached to a GitHub release installs the same way:
uv pip install https://github.com/magicprinc/python-mcp/releases/download/v0.2.6/python_repl_mcp-0.2.6-py3-none-any.whl
From a checkout of this repository, install the source tree with -e:
uv venv --python 3.14 .venv
uv pip install --python .\.venv\Scripts\python.exe -e .
.\.venv\Scripts\python.exe -P -m python_repl_mcp.server
On this machine the local agents share C:\Z\py\.venv; install the wheel
there with uv pip install --python C:\Z\py\.venv\Scripts\python.exe <wheel>.
The dispatcher and workers then use that venv's site-packages for cells.
Configure OpenCode to launch the interpreter used to install the package. Replace
the example path with its absolute path. The server speaks MCP over stdin/stdout
only. Verified against OpenCode v2.4.2's
mcp.servers.<name> shape:
{
"mcp": {
"servers": {
"python-repl": {
"type": "local",
"protocol": "2026-07-28",
"command": ["D:/work/my-project/.venv/Scripts/python.exe", "-P", "-m", "python_repl_mcp.server"],
"environment": {
"PYTHON_REPL_MCP_WORKDIR": "D:/work/my-project"
}
}
}
}
}
For calls made on behalf of a chat, OpenCode supplies
_meta["ai.opencode/sessionID"]. A host that starts a separate server for each
chat can use the "default" key without setting PYTHON_REPL_MCP_SESSION.
In standalone mode, PYTHON_REPL_MCP_WORKDIR selects the working directory for executed cells
(defaults to the server's current directory). The server's PYTHONPATH is
prepended with the installed package path for the worker; the worker uses the
server interpreter and its site-packages, with user-site enabled only when the
environment allows it.
Share one server across OpenCode projects
For a local authenticated OpenCode 2.4.3 service, use the same entry, command,
environment and absolute cwd in each project. With pool: true and identical
entries, connected locations share one dispatcher process; each chat still
gets its own worker on first use. The verified setup uses OpenCode's
runtime API: register python-repl in each
location with PUT /api/experimental/mcp/python-repl, wrapping the following
entry in {"config": ...}.
{
"type": "local",
"protocol": "2026-07-28",
"pool": true,
"disabled": true,
"codemode": false,
"cwd": "D:/repl-dispatcher",
"command": ["D:/repl-dispatcher/.venv/Scripts/python.exe", "-P", "-B", "-m", "python_repl_mcp.server"],
"environment": {
"PYTHONNOUSERSITE": "1",
"PYTHON_REPL_MCP_OPENCODE_SERVER": "http://127.0.0.1:4321",
"PYTHON_REPL_MCP_OPENCODE_CLI": "C:/path/to/native/opencode.exe"
}
}
Use "auto" for the desktop background service: the native CLI discovers the
running service, so the dispatcher does not depend on an ephemeral port after a
restart. For an explicit endpoint, use the actual loopback address of the
owning service; that mode requires the dispatcher to inherit the service's
OPENCODE_PASSWORD. In both modes the credential is excluded from the worker
environment. Keep disabled: true and connect the registered entry when the
project needs Python: POST /api/experimental/mcp/python-repl/connect for that
location. This avoids starting the dispatcher at project open; until Connect,
the python-repl tools are absent from the agent catalog, so tell the agent to
use them only after the explicit connection.
{
"environment": {
"PYTHONNOUSERSITE": "1",
"PYTHON_REPL_MCP_OPENCODE_SERVER": "auto",
"PYTHON_REPL_MCP_OPENCODE_CLI": "C:/path/to/native/opencode.exe"
}
}
In pooled mode each request needs OpenCode's chat metadata. The first
execution (or explicit reset) binds the worker to the chat directory returned
by OpenCode. That binding also governs relative repl_run_file paths until the
worker is replaced, even if a cell calls os.chdir(). A drive-relative path
such as C:script.py is rejected as ambiguous; use a path relative to the chat
directory or a fully qualified one. A failed lookup returns
SessionDirectoryUnavailable. repl_stop() ends only the calling chat's
worker; disconnect the MCP entry only when you intend to close the connection.
If a directory helper cannot confirm its own exit after termination, further
pooled lookups are refused until the MCP server restarts; running chats and
repl_stop() keep working, while new chats and repl_reset() are refused.
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 chat's bound directory. These fields are optional and appear only when meaningful.repl_run_fileresolves relative paths against the worker's bound workdir and restores the previous__file__/sys.argvafter the script. File and argument size are checked before a worker starts, 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 aFileErrorand starts no worker. Files written by a script (for example a matplotlib PNG) remain on disk and can be opened by the host's browser.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 or a fatal idle protocol error, the server frees the slot
once it confirms the worker tree has exited. A capture directory that was
locked at that moment is retried once by the next
repl_stop(even for an absent chat) or by normal shutdown; a still-locked or overflow 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; the next call starts clean. - A timeout terminates the whole worker process tree (Windows Job Object) and loses in-memory state.
- At most 16 chats 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.2.6
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.2.6.tar.gz | 82.3 kB | With provenance | Details for python_repl_mcp-0.2.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_repl_mcp-0.2.6-py3-none-any.whl | Python 3 | none | any | Details for python_repl_mcp-0.2.6-py3-none-any.whl |
Total release size: 127.7 kB
Release files / python_repl_mcp-0.2.6.tar.gz
| Download URL | python_repl_mcp-0.2.6.tar.gz |
|---|---|
| Size | 82.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ad5f799f0ad958637feb5f7630f3a6f867aa52185567345f5a4dc8d3ed0b8072
|
|
BLAKE2b-256 checksum How to use checksums |
ae7a03fa23286047758733ae946d5d656070e2329ac2399f8f383df744a70443
|
| 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.2.6-py3-none-any.whl
| Download URL | python_repl_mcp-0.2.6-py3-none-any.whl |
|---|---|
| Size | 45.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
255a332cfc1ae623a8e93e1d0323f75d92fbb9eaaa137fd98c2f2278462a3ecb
|
|
BLAKE2b-256 checksum How to use checksums |
05b68a6d451ac2841938c0847096cda382e1d691a31425d44432513eea6fae08
|
| 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.2.6
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.2.6.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.