python-repl-mcp 0.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,218 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-repl-mcp
3
+ Version: 0.2.5
4
+ Summary: Persistent Python REPL MCP server for OpenCode chats
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.14
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: mcp==2.3.0
10
+ Requires-Dist: pydantic<3,>=2.14
11
+ Provides-Extra: dev
12
+ Requires-Dist: bandit>=1.9; extra == "dev"
13
+ Requires-Dist: hypothesis>=6.170; extra == "dev"
14
+ Requires-Dist: mypy>=2.4; extra == "dev"
15
+ Requires-Dist: pip-audit>=2.10; extra == "dev"
16
+ Requires-Dist: psutil>=7.2; extra == "dev"
17
+ Requires-Dist: pytest>=9.2; extra == "dev"
18
+ Requires-Dist: pytest-cov>=7.1; extra == "dev"
19
+ Requires-Dist: pyright>=1.1; extra == "dev"
20
+ Requires-Dist: ruff>=0.17; extra == "dev"
21
+ Dynamic: license-file
22
+
23
+ # python-repl-mcp
24
+
25
+ [![CI](https://github.com/magicprinc/python-mcp/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/magicprinc/python-mcp/actions/workflows/ci.yml)
26
+ [![Release](https://img.shields.io/github/v/release/magicprinc/python-mcp?sort=semver)](https://github.com/magicprinc/python-mcp/releases)
27
+ [![Python](https://img.shields.io/badge/python-3.14%2B-3776AB?logo=python&logoColor=white)](https://www.python.org/)
28
+ [![Platform](https://img.shields.io/badge/platform-Windows-0078D4?logo=windows&logoColor=white)](https://www.microsoft.com/windows)
29
+ [![MCP](https://img.shields.io/badge/MCP-stdio-6E56CF)](https://modelcontextprotocol.io/)
30
+ [![License](https://img.shields.io/github/license/magicprinc/python-mcp)](LICENSE)
31
+ [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=magicprinc_python-mcp&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=magicprinc_python-mcp)
32
+ [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/magicprinc/python-mcp/badge)](https://scorecard.dev/viewer/?uri=github.com/magicprinc/python-mcp)
33
+
34
+ A Windows MCP stdio server that keeps **one persistent CPython interpreter per
35
+ OpenCode chat**. State, imports and helpers survive between tool calls.
36
+ Connecting to the server does not start a worker.
37
+
38
+ For another agent, start with [HANDOFF.md](HANDOFF.md): install, OpenCode
39
+ configuration, tool rules and known limits. The
40
+ [local completion plan](docs/completion-plan.md) tracks the remaining fixes
41
+ and acceptance on OpenCode, DSH and Command Code.
42
+
43
+ ## Tools
44
+
45
+ | Tool | Purpose |
46
+ |---|---|
47
+ | `repl_execute(code, timeout_seconds=30)` | Run one complete Python cell; returns `stdout`, `stderr`, the final value, and a structured error report. |
48
+ | `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. |
49
+ | `repl_reset()` | End the chat's interpreter and wait for a clean replacement to be ready. |
50
+ | `repl_stop()` | End the chat's interpreter without replacement. |
51
+
52
+ Standalone session resolution: OpenCode's `_meta["ai.opencode/sessionID"]` when present;
53
+ otherwise `PYTHON_REPL_MCP_SESSION`; otherwise the per-process `"default"` key.
54
+ There is no session tool argument. On a host without per-call metadata,
55
+ run one server process per chat to isolate state. An environment variable is
56
+ fixed for the lifetime of a process and cannot distinguish chats sharing it.
57
+
58
+ ## Requirements
59
+
60
+ - Windows 10/11, CPython 3.14.x (verified on 3.14.10), and `uv`.
61
+ - 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.
62
+ - Optional `PYTHON_REPL_MCP_SESSION` for hosts that do not send OpenCode
63
+ metadata.
64
+
65
+ ## Install and run
66
+
67
+ `uv` is the standard installer.
68
+
69
+ ```powershell
70
+ uv venv --python 3.14 .venv
71
+ uv pip install --python .\.venv\Scripts\python.exe .
72
+ .\.venv\Scripts\python.exe -P -m python_repl_mcp.server
73
+ ```
74
+
75
+ On this machine the local agents share `C:\Z\py\.venv`; install the wheel
76
+ there with `uv pip install --python C:\Z\py\.venv\Scripts\python.exe <wheel>`.
77
+ The dispatcher and workers then use that venv's site-packages for cells.
78
+
79
+ Configure OpenCode to launch the interpreter used to install the package. Replace
80
+ the example path with its absolute path. The server speaks MCP over stdin/stdout
81
+ only. Verified against OpenCode v2.4.2's
82
+ `mcp.servers.<name>` shape:
83
+
84
+ ```json
85
+ {
86
+ "mcp": {
87
+ "servers": {
88
+ "python-repl": {
89
+ "type": "local",
90
+ "protocol": "2026-07-28",
91
+ "command": ["D:/work/my-project/.venv/Scripts/python.exe", "-P", "-m", "python_repl_mcp.server"],
92
+ "environment": {
93
+ "PYTHON_REPL_MCP_WORKDIR": "D:/work/my-project"
94
+ }
95
+ }
96
+ }
97
+ }
98
+ }
99
+ ```
100
+
101
+ For calls made on behalf of a chat, OpenCode supplies
102
+ `_meta["ai.opencode/sessionID"]`. A host that starts a separate server for each
103
+ chat can use the `"default"` key without setting `PYTHON_REPL_MCP_SESSION`.
104
+
105
+ In standalone mode, `PYTHON_REPL_MCP_WORKDIR` selects the working directory for executed cells
106
+ (defaults to the server's current directory). The server's `PYTHONPATH` is
107
+ prepended with the installed package path for the worker; the worker uses the
108
+ server interpreter and its site-packages, with user-site enabled only when the
109
+ environment allows it.
110
+
111
+ ### Share one server across OpenCode projects
112
+
113
+ For a local authenticated OpenCode 2.4.3 service, use the same entry, command,
114
+ environment and absolute `cwd` in each project. With `pool: true` and identical
115
+ entries, connected locations share **one** dispatcher process; each chat still
116
+ gets its own worker on first use. The verified setup uses OpenCode's
117
+ [runtime API](https://opencode.ai/docs/api): register `python-repl` in each
118
+ location with `PUT /api/experimental/mcp/python-repl`, wrapping the following
119
+ entry in `{"config": ...}`.
120
+
121
+ ```json
122
+ {
123
+ "type": "local",
124
+ "protocol": "2026-07-28",
125
+ "pool": true,
126
+ "disabled": true,
127
+ "codemode": false,
128
+ "cwd": "D:/repl-dispatcher",
129
+ "command": ["D:/repl-dispatcher/.venv/Scripts/python.exe", "-P", "-B", "-m", "python_repl_mcp.server"],
130
+ "environment": {
131
+ "PYTHONNOUSERSITE": "1",
132
+ "PYTHON_REPL_MCP_OPENCODE_SERVER": "http://127.0.0.1:4321",
133
+ "PYTHON_REPL_MCP_OPENCODE_CLI": "C:/path/to/native/opencode.exe"
134
+ }
135
+ }
136
+ ```
137
+
138
+ Use `"auto"` for the desktop background service: the native CLI discovers the
139
+ running service, so the dispatcher does not depend on an ephemeral port after a
140
+ restart. For an explicit endpoint, use the actual loopback address of the
141
+ owning service; that mode requires the dispatcher to inherit the service's
142
+ `OPENCODE_PASSWORD`. In both modes the credential is excluded from the worker
143
+ environment. Keep `disabled: true` and connect the registered entry when the
144
+ project needs Python: `POST /api/experimental/mcp/python-repl/connect` for that
145
+ location. This avoids starting the dispatcher at project open; until Connect,
146
+ the `python-repl` tools are absent from the agent catalog, so tell the agent to
147
+ use them only after the explicit connection.
148
+
149
+ ```json
150
+ {
151
+ "environment": {
152
+ "PYTHONNOUSERSITE": "1",
153
+ "PYTHON_REPL_MCP_OPENCODE_SERVER": "auto",
154
+ "PYTHON_REPL_MCP_OPENCODE_CLI": "C:/path/to/native/opencode.exe"
155
+ }
156
+ }
157
+ ```
158
+
159
+ In pooled mode each request needs OpenCode's chat metadata. The first
160
+ execution (or explicit reset) binds the worker to the chat directory returned
161
+ by OpenCode. That binding also governs relative `repl_run_file` paths until the
162
+ worker is replaced, even if a cell calls `os.chdir()`. A drive-relative path
163
+ such as `C:script.py` is rejected as ambiguous; use a path relative to the chat
164
+ directory or a fully qualified one. A failed lookup returns
165
+ `SessionDirectoryUnavailable`. `repl_stop()` ends only the calling chat's
166
+ worker; disconnect the MCP entry only when you intend to close the connection.
167
+ If a directory helper cannot confirm its own exit after termination, further
168
+ pooled lookups are refused until the MCP server restarts; running chats and
169
+ `repl_stop()` keep working, while new chats and `repl_reset()` are refused.
170
+
171
+ ## Behaviour and limits
172
+
173
+ - `input()` and `getpass()` return a structured `InputRequired` report. Ask in
174
+ chat only for non-secret input. Use an approved local mechanism for secrets;
175
+ do not put them in chat, Python cells or tool arguments.
176
+ - `repl_execute` returns the final expression's `result` as a bounded repr.
177
+ `result_type` names the value's type, `result_json` carries the parsed JSON
178
+ value when the result is JSON-serialisable, and `result_truncated` marks a
179
+ capped repr. `kernel_id` identifies the worker; `workdir` is the chat's bound
180
+ directory. These fields are optional and appear only when meaningful.
181
+ - `repl_run_file` resolves relative paths against the worker's bound workdir and
182
+ restores the previous `__file__`/`sys.argv` after the script. File and argument
183
+ size are checked before a worker starts, with a private frame cap of 2 MiB
184
+ including overhead. The read runs in a short-lived isolated interpreter under
185
+ the 15 s startup budget; expiry is a `FileError` and starts no worker. Files
186
+ written by a script (for example a matplotlib PNG) remain on disk and can be
187
+ opened by the host's browser.
188
+ - `stdout`/`stderr` are captured per cell, bounded to 16,384 characters each;
189
+ `truncated` reports a cut snapshot, and `capture_complete=false` reports
190
+ unavailable output or unconfirmed cleanup of an interrupted cell. Worker stdio
191
+ is UTF-8. Disk use by a surviving child process is not capped.
192
+ - After idle exit or a fatal idle protocol error, the server frees the slot
193
+ once it confirms the worker tree has exited. A capture directory that was
194
+ locked at that moment is retried once by the next `repl_stop` (even for an
195
+ absent chat) or by normal shutdown; a still-locked or overflow path is
196
+ logged and dropped.
197
+ - An uncaught Python exception is a normal execution report; it does not kill
198
+ the interpreter. `os._exit()` kills the worker; the next call starts clean.
199
+ - A timeout terminates the whole worker process tree (Windows Job Object) and
200
+ loses in-memory state.
201
+ - At most 16 chats are owned at once; release one with `repl_stop`.
202
+ - Worker startup hooks must be quiet on stdout. Noisy startup output fails
203
+ closed.
204
+
205
+ ## Tests
206
+
207
+ ```powershell
208
+ $env:PYTHONPATH = "src"
209
+ python -m pytest
210
+ python -m ruff check src tests
211
+ python -m pyright
212
+ ```
213
+
214
+
215
+ ## References
216
+
217
+ - [MCP-сервер проверки и запуска кода на Python](https://habr.com/ru/articles/967042/)
218
+ — summary and design notes in [docs/references.md](docs/references.md).
@@ -0,0 +1,19 @@
1
+ python_repl_mcp/__init__.py,sha256=F1fjqs_Erur4PQCbShEAUgKLlBoerZfAL8_BVLl2KvA,126
2
+ python_repl_mcp/_helper.py,sha256=-086Dzy8mLgeB6Ak-EyXqbgohmlzqsCbr7q9PPtcUiU,6984
3
+ python_repl_mcp/_winjob.py,sha256=4zWrt7RzRpIpozDvPBTqGHnex6jKDX7eCzZLm-csYQU,6170
4
+ python_repl_mcp/config.py,sha256=6XyBM7Ltps0udLdHoCQTNVHbhC29txvmFWjK3c_7biE,3137
5
+ python_repl_mcp/errors.py,sha256=YREf4aps4s3SWfC9VqFVlEEwxY2Zku219ElDFErgaR8,1291
6
+ python_repl_mcp/fileio.py,sha256=AMO2YAsevSN33oSKtstIiFePOHWVdS4IvJZnBMKsZGg,4212
7
+ python_repl_mcp/kernel.py,sha256=fzmRKcogzg2fQdxrlLOMSA1oL1gkfj3nAFknffM621E,27576
8
+ python_repl_mcp/launcher.py,sha256=mU4kGoTQ1l4Oz6QMfo0T-K-VaPhU4gGbCnRr6DSBU3s,1423
9
+ python_repl_mcp/opencode.py,sha256=UD8Vox98iU48QUcDaRt638WcuCwOMQdC4709slCxwII,7251
10
+ python_repl_mcp/protocol.py,sha256=F1IYBFruCGBaR0tPS28wyOIfeZ6GDJzT7rWqV8xdIa0,7209
11
+ python_repl_mcp/server.py,sha256=fjBkCvSsy07hNPZPoswVT4u3_e1y2c8jUdLtA9rprXg,10942
12
+ python_repl_mcp/service.py,sha256=zRyLXIlxoIWFGoJCJ1tq1CuaWsrnpTu8Ri5kolqh9ME,36779
13
+ python_repl_mcp/worker.py,sha256=Ofsr0p2lPX13DGeDWU67G-FVA58tA-TUqWjLHN-0BDw,18255
14
+ python_repl_mcp-0.2.5.dist-info/licenses/LICENSE,sha256=uYVs3r7XgloZeK8bdo27ThhCxS0Gg9bgTArIpUeyd2M,1081
15
+ python_repl_mcp-0.2.5.dist-info/METADATA,sha256=86lLwAY8U3S79XdoG7L_x07gc0oSigsW0ZKmgsBpJ-0,10383
16
+ python_repl_mcp-0.2.5.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
17
+ python_repl_mcp-0.2.5.dist-info/entry_points.txt,sha256=GMfrkiZyHVBivPkfKSLnK_WI9dxH05_HaXY_tLrhe3E,64
18
+ python_repl_mcp-0.2.5.dist-info/top_level.txt,sha256=9J2SU4y1GIs6LJq1ZyKp32DxKvlO2r1HXugta2SQlzM,16
19
+ python_repl_mcp-0.2.5.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ python-repl-mcp = python_repl_mcp.server:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andrej Fink (magicprinc)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ python_repl_mcp