Skip to main content

python-repl-mcp

CI Release Python Platform MCP License Quality Gate Status OpenSSF Scorecard

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_SESSION for hosts that do not send OpenCode metadata.

Install and run

uv is the standard installer.

uv venv --python 3.14 .venv
uv pip install --python .\.venv\Scripts\python.exe .
.\.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() and getpass() return a structured InputRequired report. 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_execute returns the final expression's result as a bounded repr. result_type names the value's type, result_json carries the parsed JSON value when the result is JSON-serialisable, and result_truncated marks a capped repr. kernel_id identifies the worker; workdir is the chat's bound directory. These fields are optional and appear only when meaningful.
  • repl_run_file resolves relative paths against the worker's bound workdir and restores the previous __file__/sys.argv after 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 a FileError and 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/stderr are captured per cell, bounded to 16,384 characters each; truncated reports a cut snapshot, and capture_complete=false reports 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

Metadata

Release files for python-repl-mcp 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-repl-mcp 0.2.5
File Size Uploaded Provenance Actions
python_repl_mcp-0.2.5.tar.gz 81.6 kB With provenance Details for python_repl_mcp-0.2.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-repl-mcp 0.2.5
File Interpreter ABI Platform Actions
python_repl_mcp-0.2.5-py3-none-any.whl Python 3 none any Details for python_repl_mcp-0.2.5-py3-none-any.whl

Total release size: 126.6 kB

Release files / python_repl_mcp-0.2.5.tar.gz

Download URL python_repl_mcp-0.2.5.tar.gz
Size 81.6 kB
Tags Source
SHA-256 checksum
How to use checksums
440bc6e30f4b7a980331dfd8050e3a1125acfc15911b48f21a51fcb2c7c936e6
BLAKE2b-256 checksum
How to use checksums
0b77235c2acf0bedfe27fd784ad0f879aae2116ecb233f33a6fb05e62c252e01
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 log

Release files / python_repl_mcp-0.2.5-py3-none-any.whl

Download URL python_repl_mcp-0.2.5-py3-none-any.whl
Size 45.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09a0abad6c48884db47b4700ad2cae287c43bdf7c4ea567a264dc53502a1c1ec
BLAKE2b-256 checksum
How to use checksums
1a8847f220bd163ac47e65af7e530583dd65fc51d295971523387bdaf2cb7aa0
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 log

Security for python-repl-mcp 0.2.5

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.

Select from 2 files

Attestation for python_repl_mcp-0.2.5.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 log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page