MCP运行Python
MCP Run Python

原始链接: https://github.com/pydantic/pydantic-ai/tree/main/mcp-run-python

`@pydantic/mcp-run-python` 包允许在使用 Deno 的沙盒化 Pyodide 环境中安全执行 Python 代码,从而将代码与主机操作系统隔离。它专为模型上下文协议 (MCP) 设计,并兼容 PydanticAI 等工具。 安装需要 Deno。执行涉及使用 `deno run` 命令以及特定标志(`-N`,`-R=node_modules`,`-W=node_modules`,`--node-modules-dir=auto`)来管理网络访问和 Pyodide 的包缓存的本地 `node_modules`。 服务器支持 `stdio`(子进程)、`sse`(HTTP 服务器)和 `warmup`(预缓存)传输方法。`warmup` 选项预加载 Python 标准库以提高性能。 当与 PydanticAI 一起使用时,它通过 `MCPServerStdio` 集成,允许 `Agent` 安全地执行 Python 代码以执行计算等任务,如日期差示例所示。可以使用 `logfire` 等库来检测 MCP 进程和 PydanticAI,以增强监控和调试。

Hacker News 最新 | 往期 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 MCP 运行 Python (github.com/pydantic) 16 分,来自 xrd,2 小时前 | 隐藏 | 往期 | 收藏 | 2 条评论 mountainriver 5 分钟前 | 下一条 [–] 酷! 回复 turnsout 13 分钟前 | 上一条 [–] 哇,使用需谨慎 回复 加入我们,参加 6 月 16-17 日在旧金山举办的 AI 初创公司学校! 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:

原文

Model Context Protocol server to run Python code in a sandbox.

The code is executed using Pyodide in Deno and is therefore isolated from the rest of the operating system.

See https://ai.pydantic.dev/mcp/run-python/ for complete documentation.

The server can be run with deno installed using:

deno run \
  -N -R=node_modules -W=node_modules --node-modules-dir=auto \
  jsr:@pydantic/mcp-run-python [stdio|sse|warmup]

where:

  • -N -R=node_modules -W=node_modules (alias of --allow-net --allow-read=node_modules --allow-write=node_modules) allows network access and read+write access to ./node_modules. These are required so pyodide can download and cache the Python standard library and packages
  • --node-modules-dir=auto tells deno to use a local node_modules directory
  • stdio runs the server with the Stdio MCP transport — suitable for running the process as a subprocess locally
  • sse runs the server with the SSE MCP transport — running the server as an HTTP server to connect locally or remotely
  • warmup will run a minimal Python script to download and cache the Python standard library. This is also useful to check the server is running correctly.

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

from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio

import logfire

logfire.configure()
logfire.instrument_mcp()
logfire.instrument_pydantic_ai()

server = MCPServerStdio('deno',
    args=[
        'run',
        '-N',
        '-R=node_modules',
        '-W=node_modules',
        '--node-modules-dir=auto',
        'jsr:@pydantic/mcp-run-python',
        'stdio',
    ])
agent = Agent('claude-3-5-haiku-latest', mcp_servers=[server])


async def main():
    async with agent.run_mcp_servers():
        result = await agent.run('How many days between 2000-01-01 and 2025-03-18?')
    print(result.output)
    #> There are 9,208 days between January 1, 2000, and March 18, 2025.w

if __name__ == '__main__':
    import asyncio
    asyncio.run(main())
联系我们 contact @ memedata.com