python-repl-mcp 0.2.5__tar.gz
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.
- python_repl_mcp-0.2.5/LICENSE +21 -0
- python_repl_mcp-0.2.5/PKG-INFO +218 -0
- python_repl_mcp-0.2.5/README.md +196 -0
- python_repl_mcp-0.2.5/pyproject.toml +67 -0
- python_repl_mcp-0.2.5/setup.cfg +4 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/__init__.py +5 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/_helper.py +197 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/_winjob.py +175 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/config.py +84 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/errors.py +63 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/fileio.py +130 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/kernel.py +724 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/launcher.py +43 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/opencode.py +195 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/protocol.py +210 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/server.py +318 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/service.py +866 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp/worker.py +549 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp.egg-info/PKG-INFO +218 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp.egg-info/SOURCES.txt +30 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp.egg-info/dependency_links.txt +1 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp.egg-info/entry_points.txt +2 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp.egg-info/requires.txt +13 -0
- python_repl_mcp-0.2.5/src/python_repl_mcp.egg-info/top_level.txt +1 -0
- python_repl_mcp-0.2.5/tests/test_audit_fixes.py +899 -0
- python_repl_mcp-0.2.5/tests/test_e2e.py +888 -0
- python_repl_mcp-0.2.5/tests/test_fault_worker.py +205 -0
- python_repl_mcp-0.2.5/tests/test_opencode.py +194 -0
- python_repl_mcp-0.2.5/tests/test_pooled_routing.py +38 -0
- python_repl_mcp-0.2.5/tests/test_protocol.py +254 -0
- python_repl_mcp-0.2.5/tests/test_semantics.py +298 -0
- python_repl_mcp-0.2.5/tests/test_service.py +2266 -0
|
@@ -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,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
|
+
[](https://github.com/magicprinc/python-mcp/actions/workflows/ci.yml)
|
|
26
|
+
[](https://github.com/magicprinc/python-mcp/releases)
|
|
27
|
+
[](https://www.python.org/)
|
|
28
|
+
[](https://www.microsoft.com/windows)
|
|
29
|
+
[](https://modelcontextprotocol.io/)
|
|
30
|
+
[](LICENSE)
|
|
31
|
+
[](https://sonarcloud.io/summary/new_code?id=magicprinc_python-mcp)
|
|
32
|
+
[](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,196 @@
|
|
|
1
|
+
# python-repl-mcp
|
|
2
|
+
|
|
3
|
+
[](https://github.com/magicprinc/python-mcp/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/magicprinc/python-mcp/releases)
|
|
5
|
+
[](https://www.python.org/)
|
|
6
|
+
[](https://www.microsoft.com/windows)
|
|
7
|
+
[](https://modelcontextprotocol.io/)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](https://sonarcloud.io/summary/new_code?id=magicprinc_python-mcp)
|
|
10
|
+
[](https://scorecard.dev/viewer/?uri=github.com/magicprinc/python-mcp)
|
|
11
|
+
|
|
12
|
+
A Windows MCP stdio server that keeps **one persistent CPython interpreter per
|
|
13
|
+
OpenCode chat**. State, imports and helpers survive between tool calls.
|
|
14
|
+
Connecting to the server does not start a worker.
|
|
15
|
+
|
|
16
|
+
For another agent, start with [HANDOFF.md](HANDOFF.md): install, OpenCode
|
|
17
|
+
configuration, tool rules and known limits. The
|
|
18
|
+
[local completion plan](docs/completion-plan.md) tracks the remaining fixes
|
|
19
|
+
and acceptance on OpenCode, DSH and Command Code.
|
|
20
|
+
|
|
21
|
+
## Tools
|
|
22
|
+
|
|
23
|
+
| Tool | Purpose |
|
|
24
|
+
|---|---|
|
|
25
|
+
| `repl_execute(code, timeout_seconds=30)` | Run one complete Python cell; returns `stdout`, `stderr`, the final value, and a structured error report. |
|
|
26
|
+
| `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. |
|
|
27
|
+
| `repl_reset()` | End the chat's interpreter and wait for a clean replacement to be ready. |
|
|
28
|
+
| `repl_stop()` | End the chat's interpreter without replacement. |
|
|
29
|
+
|
|
30
|
+
Standalone session resolution: OpenCode's `_meta["ai.opencode/sessionID"]` when present;
|
|
31
|
+
otherwise `PYTHON_REPL_MCP_SESSION`; otherwise the per-process `"default"` key.
|
|
32
|
+
There is no session tool argument. On a host without per-call metadata,
|
|
33
|
+
run one server process per chat to isolate state. An environment variable is
|
|
34
|
+
fixed for the lifetime of a process and cannot distinguish chats sharing it.
|
|
35
|
+
|
|
36
|
+
## Requirements
|
|
37
|
+
|
|
38
|
+
- Windows 10/11, CPython 3.14.x (verified on 3.14.10), and `uv`.
|
|
39
|
+
- 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.
|
|
40
|
+
- Optional `PYTHON_REPL_MCP_SESSION` for hosts that do not send OpenCode
|
|
41
|
+
metadata.
|
|
42
|
+
|
|
43
|
+
## Install and run
|
|
44
|
+
|
|
45
|
+
`uv` is the standard installer.
|
|
46
|
+
|
|
47
|
+
```powershell
|
|
48
|
+
uv venv --python 3.14 .venv
|
|
49
|
+
uv pip install --python .\.venv\Scripts\python.exe .
|
|
50
|
+
.\.venv\Scripts\python.exe -P -m python_repl_mcp.server
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
On this machine the local agents share `C:\Z\py\.venv`; install the wheel
|
|
54
|
+
there with `uv pip install --python C:\Z\py\.venv\Scripts\python.exe <wheel>`.
|
|
55
|
+
The dispatcher and workers then use that venv's site-packages for cells.
|
|
56
|
+
|
|
57
|
+
Configure OpenCode to launch the interpreter used to install the package. Replace
|
|
58
|
+
the example path with its absolute path. The server speaks MCP over stdin/stdout
|
|
59
|
+
only. Verified against OpenCode v2.4.2's
|
|
60
|
+
`mcp.servers.<name>` shape:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"mcp": {
|
|
65
|
+
"servers": {
|
|
66
|
+
"python-repl": {
|
|
67
|
+
"type": "local",
|
|
68
|
+
"protocol": "2026-07-28",
|
|
69
|
+
"command": ["D:/work/my-project/.venv/Scripts/python.exe", "-P", "-m", "python_repl_mcp.server"],
|
|
70
|
+
"environment": {
|
|
71
|
+
"PYTHON_REPL_MCP_WORKDIR": "D:/work/my-project"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
For calls made on behalf of a chat, OpenCode supplies
|
|
80
|
+
`_meta["ai.opencode/sessionID"]`. A host that starts a separate server for each
|
|
81
|
+
chat can use the `"default"` key without setting `PYTHON_REPL_MCP_SESSION`.
|
|
82
|
+
|
|
83
|
+
In standalone mode, `PYTHON_REPL_MCP_WORKDIR` selects the working directory for executed cells
|
|
84
|
+
(defaults to the server's current directory). The server's `PYTHONPATH` is
|
|
85
|
+
prepended with the installed package path for the worker; the worker uses the
|
|
86
|
+
server interpreter and its site-packages, with user-site enabled only when the
|
|
87
|
+
environment allows it.
|
|
88
|
+
|
|
89
|
+
### Share one server across OpenCode projects
|
|
90
|
+
|
|
91
|
+
For a local authenticated OpenCode 2.4.3 service, use the same entry, command,
|
|
92
|
+
environment and absolute `cwd` in each project. With `pool: true` and identical
|
|
93
|
+
entries, connected locations share **one** dispatcher process; each chat still
|
|
94
|
+
gets its own worker on first use. The verified setup uses OpenCode's
|
|
95
|
+
[runtime API](https://opencode.ai/docs/api): register `python-repl` in each
|
|
96
|
+
location with `PUT /api/experimental/mcp/python-repl`, wrapping the following
|
|
97
|
+
entry in `{"config": ...}`.
|
|
98
|
+
|
|
99
|
+
```json
|
|
100
|
+
{
|
|
101
|
+
"type": "local",
|
|
102
|
+
"protocol": "2026-07-28",
|
|
103
|
+
"pool": true,
|
|
104
|
+
"disabled": true,
|
|
105
|
+
"codemode": false,
|
|
106
|
+
"cwd": "D:/repl-dispatcher",
|
|
107
|
+
"command": ["D:/repl-dispatcher/.venv/Scripts/python.exe", "-P", "-B", "-m", "python_repl_mcp.server"],
|
|
108
|
+
"environment": {
|
|
109
|
+
"PYTHONNOUSERSITE": "1",
|
|
110
|
+
"PYTHON_REPL_MCP_OPENCODE_SERVER": "http://127.0.0.1:4321",
|
|
111
|
+
"PYTHON_REPL_MCP_OPENCODE_CLI": "C:/path/to/native/opencode.exe"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Use `"auto"` for the desktop background service: the native CLI discovers the
|
|
117
|
+
running service, so the dispatcher does not depend on an ephemeral port after a
|
|
118
|
+
restart. For an explicit endpoint, use the actual loopback address of the
|
|
119
|
+
owning service; that mode requires the dispatcher to inherit the service's
|
|
120
|
+
`OPENCODE_PASSWORD`. In both modes the credential is excluded from the worker
|
|
121
|
+
environment. Keep `disabled: true` and connect the registered entry when the
|
|
122
|
+
project needs Python: `POST /api/experimental/mcp/python-repl/connect` for that
|
|
123
|
+
location. This avoids starting the dispatcher at project open; until Connect,
|
|
124
|
+
the `python-repl` tools are absent from the agent catalog, so tell the agent to
|
|
125
|
+
use them only after the explicit connection.
|
|
126
|
+
|
|
127
|
+
```json
|
|
128
|
+
{
|
|
129
|
+
"environment": {
|
|
130
|
+
"PYTHONNOUSERSITE": "1",
|
|
131
|
+
"PYTHON_REPL_MCP_OPENCODE_SERVER": "auto",
|
|
132
|
+
"PYTHON_REPL_MCP_OPENCODE_CLI": "C:/path/to/native/opencode.exe"
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
In pooled mode each request needs OpenCode's chat metadata. The first
|
|
138
|
+
execution (or explicit reset) binds the worker to the chat directory returned
|
|
139
|
+
by OpenCode. That binding also governs relative `repl_run_file` paths until the
|
|
140
|
+
worker is replaced, even if a cell calls `os.chdir()`. A drive-relative path
|
|
141
|
+
such as `C:script.py` is rejected as ambiguous; use a path relative to the chat
|
|
142
|
+
directory or a fully qualified one. A failed lookup returns
|
|
143
|
+
`SessionDirectoryUnavailable`. `repl_stop()` ends only the calling chat's
|
|
144
|
+
worker; disconnect the MCP entry only when you intend to close the connection.
|
|
145
|
+
If a directory helper cannot confirm its own exit after termination, further
|
|
146
|
+
pooled lookups are refused until the MCP server restarts; running chats and
|
|
147
|
+
`repl_stop()` keep working, while new chats and `repl_reset()` are refused.
|
|
148
|
+
|
|
149
|
+
## Behaviour and limits
|
|
150
|
+
|
|
151
|
+
- `input()` and `getpass()` return a structured `InputRequired` report. Ask in
|
|
152
|
+
chat only for non-secret input. Use an approved local mechanism for secrets;
|
|
153
|
+
do not put them in chat, Python cells or tool arguments.
|
|
154
|
+
- `repl_execute` returns the final expression's `result` as a bounded repr.
|
|
155
|
+
`result_type` names the value's type, `result_json` carries the parsed JSON
|
|
156
|
+
value when the result is JSON-serialisable, and `result_truncated` marks a
|
|
157
|
+
capped repr. `kernel_id` identifies the worker; `workdir` is the chat's bound
|
|
158
|
+
directory. These fields are optional and appear only when meaningful.
|
|
159
|
+
- `repl_run_file` resolves relative paths against the worker's bound workdir and
|
|
160
|
+
restores the previous `__file__`/`sys.argv` after the script. File and argument
|
|
161
|
+
size are checked before a worker starts, with a private frame cap of 2 MiB
|
|
162
|
+
including overhead. The read runs in a short-lived isolated interpreter under
|
|
163
|
+
the 15 s startup budget; expiry is a `FileError` and starts no worker. Files
|
|
164
|
+
written by a script (for example a matplotlib PNG) remain on disk and can be
|
|
165
|
+
opened by the host's browser.
|
|
166
|
+
- `stdout`/`stderr` are captured per cell, bounded to 16,384 characters each;
|
|
167
|
+
`truncated` reports a cut snapshot, and `capture_complete=false` reports
|
|
168
|
+
unavailable output or unconfirmed cleanup of an interrupted cell. Worker stdio
|
|
169
|
+
is UTF-8. Disk use by a surviving child process is not capped.
|
|
170
|
+
- After idle exit or a fatal idle protocol error, the server frees the slot
|
|
171
|
+
once it confirms the worker tree has exited. A capture directory that was
|
|
172
|
+
locked at that moment is retried once by the next `repl_stop` (even for an
|
|
173
|
+
absent chat) or by normal shutdown; a still-locked or overflow path is
|
|
174
|
+
logged and dropped.
|
|
175
|
+
- An uncaught Python exception is a normal execution report; it does not kill
|
|
176
|
+
the interpreter. `os._exit()` kills the worker; the next call starts clean.
|
|
177
|
+
- A timeout terminates the whole worker process tree (Windows Job Object) and
|
|
178
|
+
loses in-memory state.
|
|
179
|
+
- At most 16 chats are owned at once; release one with `repl_stop`.
|
|
180
|
+
- Worker startup hooks must be quiet on stdout. Noisy startup output fails
|
|
181
|
+
closed.
|
|
182
|
+
|
|
183
|
+
## Tests
|
|
184
|
+
|
|
185
|
+
```powershell
|
|
186
|
+
$env:PYTHONPATH = "src"
|
|
187
|
+
python -m pytest
|
|
188
|
+
python -m ruff check src tests
|
|
189
|
+
python -m pyright
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
## References
|
|
194
|
+
|
|
195
|
+
- [MCP-сервер проверки и запуска кода на Python](https://habr.com/ru/articles/967042/)
|
|
196
|
+
— summary and design notes in [docs/references.md](docs/references.md).
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "python-repl-mcp"
|
|
7
|
+
version = "0.2.5"
|
|
8
|
+
description = "Persistent Python REPL MCP server for OpenCode chats"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.14"
|
|
13
|
+
dependencies = ["mcp==2.3.0", "pydantic>=2.14,<3"]
|
|
14
|
+
|
|
15
|
+
[project.optional-dependencies]
|
|
16
|
+
dev = [
|
|
17
|
+
"bandit>=1.9",
|
|
18
|
+
"hypothesis>=6.170",
|
|
19
|
+
"mypy>=2.4",
|
|
20
|
+
"pip-audit>=2.10",
|
|
21
|
+
"psutil>=7.2",
|
|
22
|
+
"pytest>=9.2",
|
|
23
|
+
"pytest-cov>=7.1",
|
|
24
|
+
"pyright>=1.1",
|
|
25
|
+
"ruff>=0.17",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
python-repl-mcp = "python_repl_mcp.server:main"
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.packages.find]
|
|
32
|
+
where = ["src"]
|
|
33
|
+
|
|
34
|
+
[tool.pytest.ini_options]
|
|
35
|
+
testpaths = ["tests"]
|
|
36
|
+
pythonpath = ["src"]
|
|
37
|
+
addopts = "-ra"
|
|
38
|
+
filterwarnings = [
|
|
39
|
+
"error::mcp.shared.exceptions.MCPDeprecationWarning",
|
|
40
|
+
"error::ResourceWarning",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[tool.ruff]
|
|
44
|
+
target-version = "py314"
|
|
45
|
+
line-length = 120
|
|
46
|
+
src = ["src", "tests"]
|
|
47
|
+
|
|
48
|
+
[tool.ruff.lint]
|
|
49
|
+
select = ["E", "F", "W", "I", "UP", "B", "SIM", "RUF", "ASYNC"]
|
|
50
|
+
ignore = ["RUF001", "RUF002", "RUF003"]
|
|
51
|
+
|
|
52
|
+
[tool.pyright]
|
|
53
|
+
include = ["src"]
|
|
54
|
+
typeCheckingMode = "strict"
|
|
55
|
+
pythonVersion = "3.14"
|
|
56
|
+
|
|
57
|
+
[tool.mypy]
|
|
58
|
+
files = ["src"]
|
|
59
|
+
strict = true
|
|
60
|
+
warn_unreachable = true
|
|
61
|
+
|
|
62
|
+
[tool.bandit]
|
|
63
|
+
exclude_dirs = ["tests", "old"]
|
|
64
|
+
|
|
65
|
+
[tool.coverage.run]
|
|
66
|
+
branch = true
|
|
67
|
+
source = ["python_repl_mcp"]
|