TypeScript代码执行器

一个安全的TypeScript优先运行时,使LLM能够通过生成的TypeScript API与模型上下文协议(MCP)工具交互,而非原始JSON模式。

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

使用说明

MCP代码执行器

一个安全的TypeScript优先运行时,使LLM能够通过生成的TypeScript API而不是原始JSON模式与模型上下文协议(MCP)工具进行交互。

概述

MCP代码执行器将MCP工具模式转换为惯用的TypeScript API,并在沙盒Deno运行时中执行代理编写的代码。它透明地将API调用路由到MCP服务器,同时返回干净的结果,而不会出现上下文膨胀。

主要特点

  • 类型优先开发:从MCP模式生成强类型TypeScript API
  • 安全执行:在具有受限权限的隔离Deno沙箱中运行不受信任的代码
  • 多服务器支持:通过命名空间管理同时连接到多个MCP服务器
  • 开发者友好:生成的API遵循TypeScript规范,具有全面的JSDoc
  • CLI接口:用于生成API和执行脚本的简单命令

建筑

该系统遵循流水线架构:

MCP Schema Discovery → TypeScript Generation → API Bridge → Sandboxed Execution

核心组件

  • MCP层:处理与MCP服务器的连接和架构发现
  • 产生层:将MCP模式转换为TypeScript API
  • 桥接层:拦截API调用并将其路由到MCP服务器
  • 运行层:在Deno沙盒中安全执行代码

安装

先决条件

  • 主运行时使用Node.js 18+
  • Deno 1.40+用于沙盒代码执行
  • TypeScript 5.4+用于开发

设置

  1. 克隆存储库:
git clone <repository-url>
cd mcp-code-executor
  1. 安装依赖项:
npm install
  1. 构建项目:
npm run build
  1. 安装Deno(如果尚未安装):
# Using curl
curl -fsSL https://deno.land/install.sh | sh

# Using npm
npm install -g @deno/cli

# Using Homebrew (macOS)
brew install deno

用法

CLI命令

生成TypeScript API

# Generate APIs from configured MCP servers
mcp-executor generate

# Generate with custom config
mcp-executor generate --config ./custom-config.json

执行脚本

# Run a TypeScript script in the sandbox
mcp-executor run ./scripts/my-script.ts

# Run with custom execution options
mcp-executor run ./scripts/my-script.ts --timeout 30000 --memory-limit 512

配置

创建配置文件(例如。, mcp-config.json):

{
  "servers": [
    {
      "id": "filesystem",
      "url": "stdio://path/to/filesystem-server",
      "timeout": 10000
    },
    {
      "id": "web-search", 
      "url": "stdio://path/to/search-server",
      "timeout": 15000
    }
  ],
  "generation": {
    "outputDir": "./generated",
    "includeDocumentation": true,
    "namespacePrefix": "MCP"
  },
  "runtime": {
    "timeout": 30000,
    "memoryLimit": 512,
    "permissions": {
      "read": ["./generated"],
      "write": ["./output"],
      "net": false
    }
  }
}

图书馆使用情况

import { MCPExecutor, MCPExecutorConfig } from 'mcp-code-executor';

const config: MCPExecutorConfig = {
  servers: [
    { id: 'fs', url: 'stdio://filesystem-server' }
  ],
  generation: {
    outputDir: './generated'
  }
};

const executor = new MCPExecutor(config);

// Generate APIs
await executor.generateAPIs();

// Execute code
const result = await executor.execute(`
  import { fs } from './generated/fs';
  
  const files = await fs.listFiles('/home/user');
  return files.length;
`);

console.log('Result:', result.result);

发展

项目结构

src/                    # Source code
├── mcp/               # MCP server connections and schema handling
├── generator/         # TypeScript API generation
├── bridge/            # API call interception and routing  
├── runtime/           # Deno sandbox execution
├── types/             # Type definitions and interfaces
├── utils/             # Shared utilities
└── cli/               # Command-line interface

tests/                 # Test files (mirrors src structure)
├── mcp/               # MCP layer tests
├── generator/         # Code generation tests
├── bridge/            # Bridge layer tests
├── runtime/           # Runtime execution tests
└── utils/             # Utility function tests

有关详细的文件夹结构文档,请参阅 文件夹_结构.md.

脚本

# Development with watch mode
npm run dev

# Run tests
npm test

# Run tests once
npm run test:run

# Lint code
npm run lint

# Format code  
npm run format

# Clean build artifacts
npm run clean

测试

该项目使用Vitest进行全面覆盖的测试:

  • 单个组件的单元测试
  • 全管道集成测试
  • 沙盒隔离的安全测试
  • 执行限制的性能测试
# Run all tests
npm test

# Run specific test file
npm test -- src/mcp/registry.test.ts

# Run tests with coverage
npm test -- --coverage

安全

MCP代码执行器通过多层优先考虑安全性:

  • Deno沙盒:受限权限可防止未经授权的系统访问
  • 超时强制:防止无限循环和资源耗尽
  • 内存限制:控制执行期间的资源使用情况
  • 网络隔离:阻止未经授权的网络请求
  • 输入验证:验证所有架构和配置

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 通过测试进行更改
  4. 运行测试套件
  5. 提交拉取请求

许可证

MIT许可证-有关详细信息,请参阅许可证文件。