MCP-Pyodide模型上下文协议服务器
一个用于通过模型上下文协议(MCP)执行大型语言模型(LLMs)Python代码的Pyodide服务器。
可用工具 (5 个)
该服务在 MCP 协议中暴露的工具,AI 可按需调用
execute-python 2 个参数 需填 1 项
Execute Python code using Pyodide with output capture. When generating images, they will be automatically saved to the output directory instead of being displayed. Images can be accessed from the saved file paths that will be included in the output.
必填参数:code
install-python-packages 1 个参数 需填 1 项
Install Python packages using Pyodide
必填参数:package
get-mount-points
List mounted directories
该工具无需必填参数,直接调用即可
list-mounted-directory 1 个参数 需填 1 项
List contents of a mounted directory
必填参数:mountName
read-image 2 个参数 需填 2 项
Read an image from a mounted directory
必填参数:mountName、imagePath
服务介绍
mcp-pyodide
一个用于模型上下文协议(MCP)的 Pyodide 服务器实现。该服务器使大型语言模型(LLMs)能够通过 MCP 接口执行 Python 代码。
特性
- 使用 Pyodide 为 LLMs 提供 Python 代码执行能力
- 符合 MCP 标准的服务器实现
- 支持 stdio 和 SSE 两种传输模式
- 用 TypeScript 编写的稳健实现
- 可作为命令行工具使用
安装
npm install mcp-pyodide
使用
作为服务器
import { runServer } from "mcp-pyodide";
// Start the server
runServer().catch((error: unknown) => {
console.error("Error starting server:", error);
process.exit(1);
});
作为命令行工具
以 stdio 模式启动(默认):
mcp-pyodide
以 SSE 模式启动:
mcp-pyodide --sse
SSE 模式
当以 SSE 模式运行时,服务器提供以下端点:
- SSE 连接:
http://localhost:3020/sse - 消息处理器:
http://localhost:3020/messages
示例客户端连接:
const eventSource = new EventSource("http://localhost:3020/sse");
eventSource.onmessage = (event) => {
console.log("Received:", JSON.parse(event.data));
};
项目结构
mcp-pyodide/
├── src/
│ ├── formatters/ # Data formatting handlers
│ ├── handlers/ # Request handlers
│ ├── lib/ # Library code
│ ├── tools/ # Utility tools
│ ├── utils/ # Utility functions
│ └── index.ts # Main entry point
├── build/ # Build artifacts
├── pyodide-packages/ # Pyodide-related packages
└── package.json
依赖项
@modelcontextprotocol/sdk: MCP SDK (^1.4.0)pyodide: Python 运行环境 (^0.27.1)arktype: 类型验证库 (^2.0.1)express: 支持 SSE 的 Web 框架cors: 支持 SSE 的 CORS 中间件
开发
要求
- Node.js 18 或更高版本
- npm 9 或更高版本
设置
# Clone the repository
git clone <repository-url>
# Install dependencies
npm install
# Build
npm run build
脚本
npm run build: 编译 TypeScript 并设置执行权限npm start: 以 stdio 模式运行服务器npm run start:sse: 以 SSE 模式运行服务器
环境变量
PYODIDE_CACHE_DIR: Pyodide 缓存目录(默认值:"./cache")PYODIDE_DATA_DIR: 挂载数据目录(默认值:"./data")PORT: SSE 服务器端口(默认值:3020)
许可证
MIT
贡献
- 分叉仓库
- 创建特性分支 (
git checkout -b feature/amazing-feature) - 提交你的更改 (
git commit -am 'Add some amazing feature') - 推送到分支 (
git push origin feature/amazing-feature) - 创建 Pull Request
重要说明
- 该项目正在开发中,API 可能会发生变化
- 在生产环境中使用前请彻底测试
- 出于安全考虑,在执行不受信任的代码时要谨慎
- 使用 SSE 模式时,如需要,请确保正确的 CORS 配置
支持
请使用 Issue 跟踪器报告问题和提问。