
基于DSpy的代码执行MCP服务器原型实现。“带MCP的代码执行”架构结合了大型语言模型在代码生成方面的优势与模型......
概览
基于DSpy的代码执行MCP服务器原型实现。“带MCP的代码执行”架构结合了大型语言模型在代码生成方面的优势与用于工具集成的模型上下文协议。该系统使AI代理能够编写在隔离沙盒中运行的Python代码,同时无缝调用外部MCP工具。 复制示例环境文件并配置密钥: 配置您的MCP服务器时mcp_servers.json: 通过运行调试执行脚本验证你的设置。该脚本模拟MCP客户端,连接服务器,并运行测试任务以确保代理和工具正常工作。 运行脚本前:1. 配置你想在 mcp_servers.json 中交互的 MCP 服务器。2. 通过编辑 scripts/debug_executor.py 中的任务变量,定义你希望代理执行的具体任务。 默认情况下,系统使用一个(LocalPythonExecutor)程序,在与服务器相同的进程中运行代码。
概述(AI 翻译)
基于DSpy的代码执行MCP服务器原型实现。“带MCP的代码执行”架构结合了大型语言模型在代码生成方面的优势与用于工具集成的模型上下文协议。该系统使AI代理能够编写在隔离沙盒中运行的Python代码,同时无缝调用外部MCP工具。
快速启动
1.安装
需要 Python 3.11+ 和 Node.js 20+。
2.配置
复制示例环境文件并配置密钥:
在mcp_servers.json配置你的MCP服务器:
3.服务器运行
启动代码执行MCP服务器:
4.验证
通过运行调试执行脚本验证你的设置。该脚本模拟MCP客户端,连接服务器,并运行测试任务以确保代理和工具正常工作。
在运行脚本之前:
- 配置你想在
mcp_servers.json交互的MCP服务器。 - 通过编辑
task中的变量,定义你希望代理执行的具体任务。
开发指令
| 指挥 | 描述 |
|---|---|
pytest |
运行所有测试 |
| 汪汪,检查。 |
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
MCP Code Mode
Prototype implementation for the Code Execution MCP Server with DSpy. The “Code Execution with MCP” architecture combines the strengths of Large Language Models at code generation with the Model Context Protocol for tool integration. This system enables an AI agent to write Python code that runs in an isolated sandbox while seamlessly calling external MCP tools.
Quick Start
1. Installation
Requires Python 3.11+ and Node.js 20+.
# Create virtual environment
python3.11 -m venv .venv
source .venv/bin/activate
# Install dependencies
pip install -e .[dev]
# Install Node.js dependencies for reference servers
npm install -g npm@latest
2. Configuration
Copy the example environment file and configure secrets:
cp .env.example .env
Configure your MCP servers in mcp_servers.json:
{
"servers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/your-working-folder"],
"description": "Local file system operations"
}
}
}
3. Running the Server
Launch the Code Execution MCP server:
python -m mcp_code_mode.executor_server
4. Verification
Verify your setup by running the debug executor script. This script simulates an MCP client, connects to the server, and runs a test task to ensure the agent and tools are working correctly.
Before running the script:
- Configure the MCP servers you want to interact with in
mcp_servers.json. - Define the specific task you want the agent to perform by editing the
taskvariable inscripts/debug_executor.py.
python scripts/debug_executor.py
Development Commands
| Command | Description |
|---|---|
pytest |
Run all tests |
ruff check . |
Lint the codebase |
black . |
Format the codebase |
mypy src |
Type check the source |
python scripts/test_dspy_sandbox.py |
Sanity check the sandbox |
python scripts/debug_executor.py |
Integration test with mock client |
Execution Environment & Guardrails
By default, the system uses a Local Python Executor (LocalPythonExecutor) which runs code in the same process as the server. This is necessary because the strict Pyodide sandbox has limitations with network I/O, preventing it from calling back to other MCP tools in some environments.
Guardrails
Even with the local executor, the system enforces policies before code execution:
- Limits: 8k characters / 400 lines max.
- Imports: Allowlist only (
json,math,re,datetime, etc.). - Tokens: Disallows potentially dangerous tokens (
subprocess,exec,eval).
Violations return a POLICY_VIOLATION error.
Note: You can force the use of the Pyodide sandbox by setting
MCP_EXECUTOR=pyodide, but this may break tool calls depending on your environment.
Architecture
Overview
┌─────────────────────────────────────────────────────────────┐
安装
npx -y @modelcontextprotocol/server-filesystem /your-working-folder