代码执行服务

一个基于MCP协议的代码执行服务,允许AI模型在安全环境中运行用户提供的代码并返回执行结果。

  • by CharlieKerfoot
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 CharlieKerfoot/code-execution-mcp

使用说明

代码执行器MCP

模型上下文协议(MCP)服务器,允许AI模型运行代码。用户用指定语言的代码提示模型,模型将执行它,显示stdout、stderr和退出代码。

实施

服务器使用 活塞 api在安全的远程环境中执行代码,然后将输出返回给AI模型。

可以找到有关MCP服务器以及如何实现它们的信息 这里.

安装

该MCP使用并测试了 Claude桌面版,但它应该适用于其他LLM和AI工具。本指南仅涵盖Claude Desktop。

  1. 克隆仓库

  2. 安装依赖项

npm i
  1. 跑
npm run build
  1. 安装Claude for Desktop后,打开文件 ~/Library/Application Support/Claude/claude_desktop_config.json

  2. 粘贴以下JSON

{
  "mcpServers": {
    "code_execution": {
      "command": "node",
      "args": ["/ABSOLUTE/PATH/TO/PARENT/FOLDER/code-execution-mcp/build/index.js"]
    }
  }
}
  1. 保存文件并启动Claude。

提示栏下应该有一个滑块图标。单击时,代码执行MCP应该是可切换的。

用法

尝试使用MCP,并给出以下提示

run this python code:
import random

print("Random Numbers:", [random.randint(1, 100) for _ in range(5)])
print("Random Choice:", random.choice(['apple', 'banana', 'cherry', 'date']))
print("Random Float:", round(random.random() * 100, 2))

for i in range(3):
    print(f"Line {i+1}: {'*' * random.randint(3, 10)}")

print("Done!")

或

Generate random C code (that prints to the stdout) and run it