You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-54Lines changed: 29 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@ where:
47
47
-`example` will run a minimal Python script using `numpy`, useful for checking that the package is working, for the code
48
48
to run successfully, you'll need to install `numpy` using `uvx mcp-run-python --deps numpy example`
49
49
50
-
## Usage in codes
50
+
## Usage in codes as an MCP server
51
51
52
52
```bash
53
53
pip install mcp-run-python
@@ -87,14 +87,40 @@ if __name__ == '__main__':
87
87
As well as returning the args needed to run `mcp_run_python`, `deno_args_prepare` installs the dependencies
88
88
so they can be used by the server.
89
89
90
+
## Usage in code with `code_sandbox`
91
+
92
+
`mcp-run-python` includes a helper function `code_sandbox` to allow you to easily run code in a sandbox.
93
+
94
+
```py
95
+
from mcp_run_python import code_sandbox
96
+
97
+
code ="""
98
+
import numpy
99
+
a = numpy.array([1, 2, 3])
100
+
print(a)
101
+
a
102
+
"""
103
+
104
+
asyncdefmain():
105
+
asyncwith code_sandbox(dependencies=['numpy']) as sandbox:
106
+
result =await sandbox.eval(code)
107
+
print(result)
108
+
109
+
110
+
if__name__=='__main__':
111
+
import asyncio
112
+
113
+
asyncio.run(main())
114
+
```
115
+
116
+
Under the hood, `code_sandbox` runs an MCP server using `stdio`. You can run multiple code blocks with a single sandbox.
117
+
90
118
## Logging
91
119
92
120
MCP Run Python supports emitting stdout and stderr from the python execution as [MCP logging messages](https://github.com/modelcontextprotocol/specification/blob/eb4abdf2bb91e0d5afd94510741eadd416982350/docs/specification/draft/server/utilities/logging.md?plain=1).
93
121
94
122
For logs to be emitted you must set the logging level when connecting to the server. By default, the log level is set to the highest level, `emergency`.
95
123
96
-
Currently, it's not possible to demonstrate this due to a bug in the Python MCP Client, see [modelcontextprotocol/python-sdk#201](https://github.com/modelcontextprotocol/python-sdk/issues/201#issuecomment-2727663121).
97
-
98
124
## Dependencies
99
125
100
126
`mcp_run_python` uses a two step process to install dependencies while avoiding any risk that sandboxed code can
@@ -104,54 +130,3 @@ edit the filesystem.
104
130
*`deno` is then run with read-only permissions to the `node_modules` directory to run untrusted code.
105
131
106
132
Dependencies must be provided when initializing the server so they can be installed in the first step.
107
-
108
-
Here's an example of manually running code with `mcp-run-python`:
109
-
110
-
```python
111
-
from mcp import ClientSession, StdioServerParameters
112
-
from mcp.client.stdio import stdio_client
113
-
114
-
from mcp_run_python import deno_args_prepare
115
-
116
-
code ="""
117
-
import numpy
118
-
a = numpy.array([1, 2, 3])
119
-
print(a)
120
-
a
121
-
"""
122
-
server_params = StdioServerParameters(
123
-
command='deno',
124
-
args=deno_args_prepare('stdio', deps=['numpy'])
125
-
)
126
-
127
-
128
-
asyncdefmain():
129
-
asyncwith stdio_client(server_params) as (read, write):
0 commit comments