Skip to content

Commit 27eac30

Browse files
authored
move prepare_env.py to Python file in mcp-run-python (#1461)
1 parent aae9022 commit 27eac30

File tree

9 files changed

+52
-44
lines changed

9 files changed

+52
-44
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,24 +240,11 @@ jobs:
240240
with:
241241
deno-version: v2.x
242242

243-
- run: |
244-
deno fmt
245-
deno lint
246-
deno check src
247-
working-directory: mcp-run-python
243+
- run: make lint-js
248244

249245
- run: uv run --package mcp-run-python pytest mcp-run-python -v
250246

251-
# check warmup works
252-
- name: warmup
253-
run: |
254-
deno run \
255-
-N \
256-
-R=node_modules \
257-
-W=node_modules \
258-
--node-modules-dir=auto \
259-
src/main.ts \
260-
warmup
247+
- run: deno task dev warmup
261248
working-directory: mcp-run-python
262249

263250
# https://github.com/marketplace/actions/alls-green#why used for branch protection checks

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ testcov: test ## Run tests and generate a coverage report
8080

8181
.PHONY: test-mrp
8282
test-mrp: ## Build and tests of mcp-run-python
83-
cd mcp-run-python && npm run prepare
83+
cd mcp-run-python && deno task build
8484
uv run --package mcp-run-python pytest mcp-run-python -v
8585

8686
.PHONY: update-examples

mcp-run-python/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
src/prepareEnvCode.ts

mcp-run-python/README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
[Model Context Protocol](https://modelcontextprotocol.io/) server to run Python code in a sandbox.
44

5-
The code is executed using [Pyodide](https://pyodide.org) in [Deno](https://deno.com/) and is therefore
6-
isolated from the rest of the operating system.
5+
The code is executed using [Pyodide](https://pyodide.org) in [Deno](https://deno.com/) and is therefore isolated from
6+
the rest of the operating system.
77

88
**See <https://ai.pydantic.dev/mcp/run-python/> for complete documentation.**
99

@@ -17,20 +17,18 @@ deno run \
1717

1818
where:
1919

20-
- `-N -R=node_modules -W=node_modules` (alias of
21-
`--allow-net --allow-read=node_modules --allow-write=node_modules`) allows
22-
network access and read+write access to `./node_modules`. These are required
23-
so pyodide can download and cache the Python standard library and packages
20+
- `-N -R=node_modules -W=node_modules` (alias of `--allow-net --allow-read=node_modules --allow-write=node_modules`)
21+
allows network access and read+write access to `./node_modules`. These are required so pyodide can download and cache
22+
the Python standard library and packages
2423
- `--node-modules-dir=auto` tells deno to use a local `node_modules` directory
2524
- `stdio` runs the server with the
26-
[Stdio MCP transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio)
27-
suitable for running the process as a subprocess locally
25+
[Stdio MCP transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio)
26+
suitable for running the process as a subprocess locally
2827
- `sse` runs the server with the
29-
[SSE MCP transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse)
30-
— running the server as an HTTP server to connect locally or remotely
31-
- `warmup` will run a minimal Python script to download and cache the Python
32-
standard library. This is also useful to check the server is running
33-
correctly.
28+
[SSE MCP transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse)
29+
running the server as an HTTP server to connect locally or remotely
30+
- `warmup` will run a minimal Python script to download and cache the Python standard library. This is also useful to
31+
check the server is running correctly.
3432

3533
Here's an example of using `@pydantic/mcp-run-python` with PydanticAI:
3634

mcp-run-python/build.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// inline src/prepare_env.py into src/prepareEnvCode.js
2+
import * as path from '@std/path'
3+
4+
if (!import.meta.dirname) {
5+
throw new Error('import.meta.dirname is not defined, unable to load prepare_env.py')
6+
}
7+
const src = path.join(import.meta.dirname, 'src/prepare_env.py')
8+
const dst = path.join(import.meta.dirname, 'src/prepareEnvCode.ts')
9+
10+
let pythonCode = await Deno.readTextFile(src)
11+
pythonCode = pythonCode.replace(/\\/g, '\\\\')
12+
const jsCode = `\
13+
// DO NOT EDIT THIS FILE DIRECTLY, INSTEAD RUN "deno run build"
14+
export const preparePythonCode = \`${pythonCode}\`
15+
`
16+
await Deno.writeTextFile(dst, jsCode)

mcp-run-python/deno.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
{
22
"name": "@pydantic/mcp-run-python",
3-
"version": "0.0.11",
3+
"version": "0.0.13",
44
"license": "MIT",
55
"nodeModulesDir": "auto",
66
"exports": {
77
".": "./src/main.ts"
88
},
99
"tasks": {
10-
"lint-format": "deno fmt && deno lint && deno check src"
10+
"build": "deno run -R=. -W=src build.ts",
11+
"lint-format": "deno task build && deno fmt && deno lint && deno check src && deno publish --dry-run --allow-dirty",
12+
"dev": "deno task build && deno run -N -R=node_modules -W=node_modules src/main.ts",
13+
"build-publish": "deno task build && deno publish"
1114
},
1215
"imports": {
1316
"@modelcontextprotocol/sdk": "npm:@modelcontextprotocol/sdk@^1.8.0",
1417
"@std/cli": "jsr:@std/cli@^1.0.15",
18+
"@std/path": "jsr:@std/path@^1.0.8",
1519
"pyodide": "npm:pyodide@^0.27.4",
1620
"zod": "npm:zod@^3.24.2"
1721
},
@@ -20,17 +24,15 @@
2024
"semiColons": false,
2125
"singleQuote": true,
2226
"include": [
23-
"src/"
27+
"."
2428
]
2529
},
26-
"compilerOptions": {
27-
"types": [
28-
"node"
29-
],
30-
"lib": [
31-
"ESNext",
32-
"deno.ns",
33-
"dom" // console needs this
30+
"publish": {
31+
"include": [
32+
"src/*.ts",
33+
"src/prepareEnvCode.ts", // required to override gitignore
34+
"README.md",
35+
"deno.json"
3436
]
3537
}
3638
}

mcp-run-python/deno.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mcp-run-python/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { z } from 'zod'
1111

1212
import { asXml, runCode } from './runCode.ts'
1313

14-
const VERSION = '0.0.11'
14+
const VERSION = '0.0.13'
1515

1616
export async function main() {
1717
const { args } = Deno

mcp-run-python/src/prepareEnvCode.ts renamed to mcp-run-python/src/prepare_env.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export const preparePythonCode = `
21
"""Logic for installing dependencies in Pyodide.
32
43
Mostly taken from https://github.com/pydantic/pydantic.run/blob/main/src/frontend/src/prepare_env.py
@@ -162,7 +161,7 @@ def _read_pep723_metadata(code: str) -> dict[str, Any]:
162161
Copied from https://packaging.python.org/en/latest/specifications/inline-script-metadata/#reference-implementation
163162
"""
164163
name = 'script'
165-
magic_comment_regex = r'(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\\s(?P<content>(^#(| .*)$\\s)+)^# ///$'
164+
magic_comment_regex = r'(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$'
166165
matches = list(filter(lambda m: m.group('type') == name, re.finditer(magic_comment_regex, code)))
167166
if len(matches) > 1:
168167
raise ValueError(f'Multiple {name} blocks found')
@@ -199,4 +198,3 @@ def _find_imports_to_install(imports: list[str]) -> Iterable[str]:
199198
yield package_name
200199
elif '.' not in module:
201200
yield module
202-
`

0 commit comments

Comments
 (0)