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
@@ -62,12 +64,12 @@ _(This example is complete, it can be run "as is" with Python 3.10+ — you'll n
62
64
63
65
**What's happening here?**
64
66
65
-
* The model is receiving the prompt "how many days between 2000-01-01 and 2025-03-18?"
66
-
* The model decides "Oh, I've got this `run_python_code` tool, that will be a good way to answer this question", and writes some python code to calculate the answer.
67
-
* The model returns a tool call
68
-
* PydanticAI sends the tool call to the MCP server using the SSE transport
69
-
* The model is called again with the return value of running the code
70
-
* The model returns the final answer
67
+
- The model is receiving the prompt "how many days between 2000-01-01 and 2025-03-18?"
68
+
- The model decides "Oh, I've got this `run_python_code` tool, that will be a good way to answer this question", and writes some python code to calculate the answer.
69
+
- The model returns a tool call
70
+
- PydanticAI sends the tool call to the MCP server using the SSE transport
71
+
- The model is called again with the return value of running the code
72
+
- The model returns the final answer
71
73
72
74
You can visualise this clearly, and even see the code that's run by adding three lines of code to instrument the example with [logfire](https://logfire.pydantic.dev/docs):
73
75
@@ -87,14 +89,24 @@ Will display as follows:
87
89
The other transport offered by MCP is the [stdio transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio) where the server is run as a subprocess and communicates with the client over `stdin` and `stdout`. In this case, you'd use the [`MCPServerStdio`][pydantic_ai.mcp.MCPServerStdio] class.
88
90
89
91
!!! note
90
-
When using [`MCPServerStdio`][pydantic_ai.mcp.MCPServerStdio] servers, the [`agent.run_mcp_servers()`][pydantic_ai.Agent.run_mcp_servers] context manager is responsible for starting and stopping the server.
91
-
92
+
When using [`MCPServerStdio`][pydantic_ai.mcp.MCPServerStdio] servers, the [`agent.run_mcp_servers()`][pydantic_ai.Agent.run_mcp_servers] context manager is responsible for starting and stopping the server.
92
93
93
94
```python {title="mcp_stdio_client.py" py="3.10"}
94
95
from pydantic_ai import Agent
95
96
from pydantic_ai.mcp import MCPServerStdio
96
97
97
-
server = MCPServerStdio('npx', ['-y', '@pydantic/mcp-run-python', 'stdio'])
Copy file name to clipboardExpand all lines: docs/mcp/run-python.md
+49-22Lines changed: 49 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,47 @@
1
1
# MCP Run Python
2
2
3
-
The **MCP Run Python** package is an MCP server that allows agents to execute Python code in a secure, sandboxed environment. It uses [Pyodide](https://pyodide.org/) to run Python code in a JavaScript environment, isolating execution from the host system.
3
+
The **MCP Run Python** package is an MCP server that allows agents to execute Python code in a secure, sandboxed environment. It uses [Pyodide](https://pyodide.org/) to run Python code in a JavaScript environment with [Deno](https://deno.com/), isolating execution from the host system.
4
4
5
5
## Features
6
6
7
-
***Secure Execution**: Run Python code in a sandboxed WebAssembly environment
8
-
***Package Management**: Automatically detects and installs required dependencies
9
-
***Complete Results**: Captures standard output, standard error, and return values
-**Error Handling**: Provides detailed error reports for debugging
12
12
13
13
## Installation
14
14
15
-
The MCP Run Python server is distributed as an [NPM package](https://www.npmjs.com/package/@pydantic/mcp-run-python) and can be run directly using [`npx`](https://docs.npmjs.com/cli/v8/commands/npx):
15
+
!!! warning "Switch from npx to deno"
16
+
We previously distributed `mcp-run-python` as an `npm` package to use via `npx`.
17
+
We now recommend using `deno` instead as it provides better sandboxing and security.
16
18
17
-
```bash
18
-
npx @pydantic/mcp-run-python [stdio|sse]
19
-
```
20
-
21
-
Where:
19
+
The MCP Run Python server is distributed as a [JSR package](https://jsr.io/@pydantic/mcp-run-python) and can be run directly using [`deno run`](https://deno.com/):
22
20
23
-
*`stdio`: Runs the server with [stdin/stdout transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio) (for subprocess usage)
24
-
*`sse`: Runs the server with [HTTP Server-Sent Events transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) (for remote connections)
-`stdio` runs the server with the [Stdio MCP transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#stdio) — suitable for running the process as a subprocess locally
17
-
- and `sse` runs the server with the [SSE MCP transport](https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse) — running the server as an HTTP server to connect locally or remotely
0 commit comments