Token导航 LogoToken导航TokenDH.com
mcpcodeserver logo
开发工具stdio官方级别未说明来源级核验

mcpcodeserver

MCP Server

github

一个将工具调用转换为TypeScript代码生成的MCP代理服务器,允许LLM通过编写代码而非顺序工具调用来高效执行复杂工作流。

工具数

0

提示词数

0

GitHub Stars

13

资源数

0
代码生成TypeScriptClaudeClaude DesktopClaudeCursorWindsurfClineVS Code

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

zbowling

提供方

zbowling

最后核验

2026/5/17 20:20

运行时

Node.js

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

命令预览

npx github:zbowling/mcpcodeserver --help

详细介绍

MCPCODESERVER(注:这可能是某个特定软件或系统中的服务器名称,根据上下文,“mcpcodeserver”直译为“MCPCODE服务器”,但具体含义需结合实际应用场景来理解)

](https://www.npmjs.com/package/mcpcodeserver) ![MIT licensed](./LICENSE) ![Install MCP Server](https://cursor.com/en/install-mcp?name=mcpcodeserver&config=eyJjb21tYW5kIjoibnB4IiwiYXJncyI6WyIteSIsIm1jcGNvZGVzZXJ2ZXIiLCItLWNvbmZpZyIsIi9wYXRoL3RvL3lvdXIvbWNwLmpzb24iXX0%3D) ![Install in VS Code (npx)](https://insiders.vscode.dev/redirect?url=vscode%3Amcp%2Finstall%3F%7B%22name%22%3A%22mcpcodeserver%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22mcpcodeserver%22%2C%22--config%22%2C%22%2Fpath%2Fto%2Fyour%2Fmcp.json%22%5D%7D)

一个模型上下文协议(MCP)代理服务器,它将工具调用转换为TypeScript代码生成。大型语言模型(LLMs)无需来回进行多次工具调用,而是可以编写调用多个工具的自然TypeScript代码,从而减少令牌开销并利用LLM在代码生成方面的卓越能力。

❌ 无 mcpcodeserver

大型语言模型(LLMs)会进行多次顺序工具调用,消耗令牌,并在处理复杂工作流程时遇到困难:

  • ❌ 大型语言模型(LLM)与工具之间多次往返交互
  • ❌ 复杂的工具调用序列容易出错
  • ❌ 工具之间无法轻松传递数据
  • ❌ 错误处理和控制流有限

✅ 使用mcpcodeserver

大型语言模型(LLMs)能自然地编写调用多个工具的TypeScript代码:

  • ✅ 编写代码以按顺序调用多个工具
  • ✅ 自然地使用变量、循环和条件语句
  • ✅ 使用 try/catch 实现更好的错误处理
  • ✅ 通过合并操作减少令牌使用量
  • ✅ 利用大型语言模型(LLM)强大的代码生成能力

快速入门

  1. 安装 mcpcodeserver 在您的MCP客户端中(请参阅下面的安装部分)
  2. 创建一个 mcp.json 包含您子MCP服务器的配置文件
  3. 开始使用它 - 您的大型语言模型(LLM)现在可以生成并执行调用您工具的TypeScript代码
// Instead of multiple tool calls, write code like this:
const files = await filesystem.list_directory({ path: "/tmp" });
const results = await Promise.all(
  files.map(file => filesystem.read_file({ path: file.path }))
);
return results.filter(content => content.includes("important"));

概述

“mcpcodeserver”可以翻译为“MCPC服务器”或“多协议内容提供服务器”,具体翻译取决于上下文和该术语的具体用途。在没有特定上下文的情况下,“MCPC服务器”是一个较为通用的翻译 是一个独特的MCP服务器,它:

  • 作为MCP客户端,连接到一个或多个子MCP服务器
  • 从子服务器中发现所有工具
  • 为大型语言模型(LLM)客户端提供了三个强大的工具:

1. list_servers - 列出连接到此MCP服务器的所有可用子服务器 1. get_tool_definitions - 返回已发现工具的 TypeScript 类型定义(可选地通过服务器进行过滤) 1. generate_and_execute_code - 生成并执行在沙箱中调用这些工具的TypeScript代码

这种架构使大型语言模型(LLMs)能够通过编写代码来协调复杂的多工具工作流程,而不是按顺序调用工具,这对于现代语言模型来说通常更为高效且自然。

相关工作与研究

这一方法受到了最近研究的启发,研究表明,大型语言模型(LLMs)在生成可执行代码方面表现优于直接调用工具:

  • Cloudflare 代码模式 - 一个类似的实现是将MCP工具转换为TypeScript API,这表明“大型语言模型(LLMs)在编写调用MCP的代码方面比直接调用MCP更擅长。”

这项研究的关键见解是,大型语言模型(LLMs)在真实世界的代码上进行了广泛的训练,但接触合成工具调用格式的机会有限,因此,代码生成成为处理复杂代理工作流程的一种更自然且有效的方法。

为什么要使用这个?

传统工具调用问题

  • 在大型语言模型(LLM)和工具之间进行多次往返操作会消耗代币
  • 大型语言模型(LLMs)在处理复杂的工具调用序列时常常感到困难
  • 每次调用工具都需要理解并格式化JSON模式
  • 数据无法直接在工具之间传递,除非通过大型语言模型(LLM)进行处理

代码生成解决方案

  • 编写TypeScript代码以按顺序调用多个工具
  • 自然地使用变量、循环和条件语句
  • 使用 try/catch 进行更好的错误处理
  • 通过合并操作减少令牌使用量
  • 利用大型语言模型(LLM)强大的代码生成能力

动态工具发现

MCPCodeServer 自动监控子 MCP 服务器上的刀具更换情况,并在刀具被添加、移除或修改时通知父客户端:

  • 自动刷新每30秒检查一次刀具更换情况
  • 实时通知发送 notifications/tools/list_changed 为家长客户提供服务/指导
  • 动态更新工具定义和摘要会自动更新
  • 无需手动刷新父级大型语言模型(LLM)接收到更新其工具知识的通知

这确保了父级大型语言模型(LLM)始终拥有最新的工具定义,而无需人工干预。

服务器过滤

为了减少上下文窗口的使用并提高专注度,mcpcodeserver支持通过特定服务器过滤工具定义:

  • 列出可用服务器使用 list_servers 查看所有连接的子服务器
  • 过滤后的工具定义使用 get_tool_definitions 与……一起;带有 server_names 参数,用于仅从特定服务器获取工具
  • 减少冗长性获取精准的TypeScript定义,同时不超出大型语言模型(LLM)的上下文窗口限制
  • 方法命名空间所有生成的函数均以服务器名称为前缀(例如。, pizzashop_create_pizzafilesystem_read_file)

示例用法:

// List available servers
const servers = await list_servers({});
// Returns: ["pizzashop", "filesystem", "memory"]

// Get all tool definitions
const allTools = await get_tool_definitions({});

// Get only pizzashop tools
const pizzashopTools = await get_tool_definitions({
  server_names: ["pizzashop"]
});

高级MCP功能

当父服务器和子服务器均支持时,mcpcodeserver支持高级MCP协议功能的透传:

  • 引出(信息/反应)子服务器可以在工具执行期间请求用户输入,这些输入会传递给父客户端
  • 列出并汇总所有子服务器的根资源,提供一个统一的可用资源视图
  • 抽样使大型语言模型(LLM)的采样请求能够传递给子服务器,以实现高级AI功能

这些功能会自动向父客户端进行宣传,并在底层子MCP服务器提供支持时无缝工作。

快速入门

立即使用 npx 尝试(无需安装):

# From GitHub
npx github:zbowling/mcpcodeserver --help

# Or when published to npm
npx mcpcodeserver --help

🛠️ 安装

要求

  • Node.js 版本 >= 18.0.0
  • Cursor、Claude Code、VSCode、Windsurf 或其他 MCP 客户端

通过Smithery进行安装

通过(某种方式)自动为任何客户端安装mcpcodeserver “Smithery”可以翻译为“铁匠铺”或“锻造工坊”。这个词汇通常用来指代一个进行金属加工、锻造或修理等工作的场所,类似于中文里的“铁匠铺”或“锻造工坊”

npx -y @smithery/cli@latest install mcpcodeserver --client  --key 

在Cursor中安装

首选 Settings -> Cursor Settings -> MCP -> Add new global MCP server

将以下配置粘贴到你的 Cursor(光标)处 ~/.cursor/mcp.json 文件是推荐的方法。您也可以通过在特定项目中创建来安装 .cursor/mcp.json 在你的项目文件夹中。

Cursor一键安装

![Install MCP Server](https://cursor.com/en/install-mcp?name=mcpcodeserver&config=eyJjb21tYW5kIjoibnB4IiwgImFyZ3MiOiBbIi15IiwgIm1jcGNvZGVzZXJ2ZXIiLCAiLS1jb25maWciLCAiL3BhdGgvdG8veW91ci9tY3AuanNvbiJdfQ%3D%3D)

本地服务器连接(Cursor)

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

光标远程服务器连接(如果您设置了HTTP传输)

{
  "mcpServers": {
    "mcpcodeserver": {
      "url": "http://localhost:3000/mcp"
    }
  }
}

在Claude代码中安装

运行这个命令。参见 克劳德·科德MCP文档 如需更多信息。

克劳德本地服务器连接

claude mcp add mcpcodeserver -- npx -y mcpcodeserver --config /path/to/your/mcp.json

克劳德代码远程服务器连接

claude mcp add --transport http mcpcodeserver http://localhost:3000/mcp

在VSCode中安装

VSCode一键安装

![Install in VS Code (npx)](https://insiders.vscode.dev/redirect?url=vscode%3Amcp%2Finstall%3F%7B%22name%22%3A%22mcpcodeserver%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22mcpcodeserver%22%2C%22--config%22%2C%22%2Fpath%2Fto%2Fyour%2Fmcp.json%22%5D%7D)

VSCode 手动配置

在你的VSCode MCP设置中添加:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

安装在风帆冲浪板上

风帆冲浪一键安装

![Install in Windsurf](https://windsurf.ai/deeplink/mcp-install?name=mcpcodeserver&config=eyJjb21tYW5kIjoibnB4IiwiYXJncyI6WyIteSIsIm1jcGNvZGVzZXJ2ZXIiLCItLWNvbmZpZyIsIi9wYXRoL3RvL3lvdXIvbWNwLmpzb24iXX0%3D)

安装在AI编程助手中

为了 继续克林(Cline,人名或特定名称,具体根据上下文确定),和 RooCode(注:这是一个专有名词或品牌名,直接翻译为中文可能无法准确传达其含义,因此保持原样。如果这是一个特定公司或产品的名称,建议根据其官方中文译名或广泛接受的译法进行翻译。),添加到您的配置中:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

安装在放大器中

在你的终端中运行这个命令。参见 Amp MCP 文档 如需更多信息。

amp mcp add mcpcodeserver -- npx -y mcpcodeserver --config /path/to/your/mcp.json

安装在文本编辑器中

对于 帮助Codium(植物名,通常指某种石莼属的海藻)Zed(泽德,人名或特定称谓,根据上下文可具体翻译为“泽德”、“泽”等,此处保留原音译)Nova(新星),和 Sublime Text(一款文本编辑器)在你的配置中添加:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

在Neovim中安装

在你的 Neovim MCP 配置中添加:

{
  mcpServers = {
    mcpcodeserver = {
      command = "npx",
      args = {"-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"}
    }
  }
}

在Emacs中安装

在你的Emacs MCP配置中添加:

(setq mcp-servers
      '((mcpcodeserver
         :command "npx"
         :args ("-y" "mcpcodeserver" "--config" "/path/to/your/mcp.json"))))

在 JetBrains 集成开发环境(IDE)中安装

为了 IntelliJ IDEAWebStormPyCharm,和 Android Studio添加到您的MCP设置中:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

安装在AI工具中

对于 Codeium(注:这是一个专有名词,可能指某种代码补全工具或平台,直接翻译为“代码助手”或保持原样“Codeium”均可,具体取决于上下文和该词在特定领域的常用译法。)TabnineGitHub Copilot,和 亚马逊CodeWhisperer添加到您的MCP设置中:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

在云端集成开发环境(IDEs)中安装

对于 ReplitCodeSandboxStackBlitzGitPodGitHub Codespaces(GitHub 代码空间)GitLab Web IDE(GitLab网页集成开发环境),以及 Bitbucket Cloud(比特bucket云),在您的MCP设置中添加:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

安装到其他工具中

对于 Xcode船队;舰队Sourcegraph(源代码图形化工具/平台),以及 JetBrains Gateway(杰特布瑞恩斯网关)在您的MCP配置中添加:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

在远程开发环境中安装

对于远程开发环境,您也可以使用HTTP传输:

{
  "mcpServers": {
    "mcpcodeserver": {
      "url": "http://your-server:3000/mcp"
    }
  }
}

配置文件

创建一个 mcp.json 配置文件以定义您的子MCP服务器:

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
      "env": { "DEBUG": "false" }
    },
    "memory": {
      "command": "npx", 
      "args": ["-y", "@modelcontextprotocol/server-memory"]
    },
    "brave-search": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-brave-search"],
      "env": { "BRAVE_API_KEY": "your-api-key" }
    }
  }
}

开发安装环境

# Install dependencies (using Bun for faster performance)
bun install

# Or with npm
npm install

# Build the project
bun run build

# Test the built server
bun dist/index.js --help

这个项目使用 Bun 来提升性能,但 npm/node 也能正常工作。

🚨 故障排除

模块未找到错误

如果你遇到 ERR_MODULE_NOT_FOUND,试试使用 bunx 而不是 npx:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "bunx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

ESM(欧洲稳定机制)决议问题

对于诸如……之类的错误 Error: Cannot find module,试试这个 --experimental-vm-modules 标志:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "--node-options=--experimental-vm-modules", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

TLS/证书问题

使用 --experimental-fetch 绕过TLS相关问题的标志:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "--node-options=--experimental-fetch", "mcpcodeserver", "--config", "/path/to/your/mcp.json"]
    }
  }
}

通用MCP客户端错误

  1. 尝试添加 @latest 到包名
  2. 使用 bunx 作为……的替代品 npx
  3. 考虑使用 deno 作为另一个选择
  4. 确保您使用的是 Node.js v18 或更高版本以支持原生 fetch 功能

配置问题

  • 确保你的 mcp.json 文件是有效的JSON
  • 检查所有子服务器命令是否都在您的PATH环境中可用
  • 验证子服务器能够独立启动
  • 检查配置文件路径的文件权限

使用MCP Inspector进行测试

npx -y @modelcontextprotocol/inspector npx mcpcodeserver --config /path/to/your/mcp.json

💻 开发

CLI 参数

mcpcodeserver 接受以下命令行界面(CLI)标志:

  • `--config

– MCP 配置文件的路径(默认: ./mcp.json`)

  • --transport – 要使用的交通工具(stdio (默认情况下)。请注意,HTTP 传输会自动提供 HTTP 和 SSE 端点
  • --port – 使用时监听的端口 http 运输(默认 3000)
  • --help – 显示帮助信息

使用HTTP传输和8080端口的示例:

npx mcpcodeserver --config /path/to/mcp.json --transport http --port 8080

使用stdio传输的示例:

npx mcpcodeserver --config /path/to/mcp.json --transport stdio

环境变量

你可以使用环境变量来进行配置:

  • MCP_CONFIG_PATH – MCP配置文件的路径(替代方式) --config)
  • MCP_TRANSPORT – 交通方式(替代 --transport)
  • MCP_PORT – HTTP传输的端口号(替代方案为 --port

使用环境变量的示例:

# .env
MCP_CONFIG_PATH=/path/to/your/mcp.json
MCP_TRANSPORT=stdio

使用环境变量的MCP配置示例:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver"],
      "env": {
        "MCP_CONFIG_PATH": "/path/to/your/mcp.json"
      }
    }
  }
}

注: 当同时提供命令行接口(CLI)标志和环境变量时,CLI标志具有优先权。

本地开发配置

对于本地开发,您可以直接运行TypeScript源代码:

{
  "mcpServers": {
    "mcpcodeserver": {
      "command": "npx",
      "args": ["tsx", "/path/to/mcpcodeserver/src/index.ts", "--config", "/path/to/your/mcp.json"]
    }
  }
}

运行模式

标准I/O模式(默认)

服务器默认以标准输入输出(stdio)模式运行,这非常适合与Claude Desktop等MCP客户端集成:

# Run in stdio mode
npx mcpcodeserver --config mcp.json

# Or with custom config path
npx mcpcodeserver --config /path/to/your/mcp.json

HTTP 模式

为了调试、测试或与基于网页的MCP客户端进行集成,您可以以HTTP模式运行服务器:

# Run in HTTP mode on default port 3000
npx mcpcodeserver --http --config mcp.json

# Run on custom port and host
npx mcpcodeserver --http --port 8080 --host 0.0.0.0 --config mcp.json

当以HTTP模式运行时,服务器将可通过以下地址访问:

  • 服务器URL: http://localhost:3000/mcp (或您自定义的主机:端口)
  • MCP 检测器使用 npx @modelcontextprotocol/inspector http://localhost:3000/mcp 进行调试和测试

MCP Inspector 集成

MCP Inspector 是一款强大的工具,用于调试和测试 MCP 服务器。在 HTTP 模式下运行时,您可以使用它来:

  • 检查可用的工具及其架构
  • 测试工具交互式调用
  • 调试资源访问和提示信息
  • 实时监控通知
# Start the server in HTTP mode
npx mcpcodeserver --http --config mcp.json

# In another terminal, start the MCP Inspector
npx @modelcontextprotocol/inspector http://localhost:3000/mcp

# Or use the shorthand script (includes all example servers)
npm run inspector

检查器将在您的浏览器中打开,并提供一个完整的界面,供您探索和测试您的MCP服务器。

注:这个(或:该) npm run inspector 命令使用 mcp-test.json 其中包括8台MCP服务器(共67个工具)来自 官方示例,包括TypeScript(npx) 和 Python (uvx基于(某种技术或系统)的服务器。

配置

创建一个 mcp.json 文件用于定义要连接到哪个子MCP服务器。此文件遵循标准的MCP客户端配置格式:

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
      "env": {
        "DEBUG": "false"
      }
    },
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "your-token-here"
      }
    },
    "weather": {
      "url": "http://localhost:3000/mcp",
      "transport": "sse"
    }
  }
}

配置选项

每个服务器条目支持:

对于stdio传输:

  • command (必填)- 要执行的命令(例如,“node”,“python”,“npx”)
  • args (可选)- 要传递给命令的参数数组
  • env (可选)- 子进程的环境变量

对于HTTP/SSE传输:

  • url (必需)- HTTP终端节点URL
  • transport - 设置为“sse”以启用服务器发送事件(Server-Sent Events)

用法

启动服务器

# Use default config (./mcp.json)
mcpcodeserver

# Use custom config location
mcpcodeserver --config /path/to/custom-mcp.json

# Show help
mcpcodeserver --help

用作MCP服务器

在您的MCP客户端中配置mcpcodeserver(如Claude Desktop、Claude Code、Cline等):

使用 npx(推荐 - 无需安装):

{
  "mcpServers": {
    "codeserver": {
      "command": "npx",
      "args": ["-y", "mcpcodeserver", "--config", "/path/to/mcp.json"]
    }
  }
}

来自GitHub(立即可用):

{
  "mcpServers": {
    "codeserver": {
      "command": "npx",
      "args": ["-y", "github:zbowling/mcpcodeserver", "--config", "/path/to/mcp.json"]
    }
  }
}

使用其他包管理器:

// yarn
{ "command": "yarn", "args": ["dlx", "mcpcodeserver", "--config", "/path/to/mcp.json"] }

// pnpm
{ "command": "pnpm", "args": ["dlx", "mcpcodeserver", "--config", "/path/to/mcp.json"] }

// bun
{ "command": "bunx", "args": ["mcpcodeserver", "--config", "/path/to/mcp.json"] }

看见 示例/例子 如需更多配置示例和MCP客户端特定设置,请参阅。

工具1:获取工具定义

这个工具会返回从子服务器中发现的所有工具的TypeScript类型定义。

输入:

  • include_examples (可选布尔值) - 是否包含使用示例

示例:

// Call the tool (in your MCP client)
get_tool_definitions({ include_examples: true })

输出: 返回包含接口和函数声明的TypeScript代码:

/**
 * Auto-generated TypeScript definitions for MCP tools
 */

interface ToolResult {
  content: Array;
  isError?: boolean;
}

/**
 * Read contents of a file
 * Server: filesystem
 * Tool: read_file
 */
interface ReadFileParams {
  path: string;
}

declare function filesystem_read_file(params: ReadFileParams): Promise;

// ... more tool definitions

工具2:生成并执行代码

这个工具在一个沙箱环境中执行TypeScript代码,并可以访问所有已发现的工具函数。

输入:

  • code (必需的字符串)- 要执行的 TypeScript/JavaScript 代码
  • timeout (可选数字)- 最大执行时间(毫秒)(默认值:30000,最大值:300000)

示例:

// Call the tool with TypeScript code
generate_and_execute_code({
  code: `
    // Read multiple files and combine them
    const file1 = await filesystem_read_file({ path: "/tmp/file1.txt" });
    const file2 = await filesystem_read_file({ path: "/tmp/file2.txt" });

    const text1 = file1.content[0].text;
    const text2 = file2.content[0].text;

    console.log("File 1 length:", text1.length);
    console.log("File 2 length:", text2.length);

    return {
      combined: text1 + text2,
      totalLength: text1.length + text2.length
    };
  `
})

输出:

=== Console Output ===
File 1 length: 42
File 2 length: 38

=== Result ===
{
  "combined": "...",
  "totalLength": 80
}

沙盒环境

TypeScript 执行沙盒提供:

可用:

  • 所有发现的工具函数(作为异步函数)
  • 控制台方法: console.log()console.error()console.warn()console.info()
  • 基本的JavaScript全局变量: MathJSONDateArrayObjectStringNumberBoolean
  • Promise 和 async/await 支持
  • 使用 try/catch 进行错误处理
  • 定时器: setTimeoutsetIntervalclearTimeoutclearInterval

不可用:

  • Node.js 模块(fs、http、child_process 等)
  • 文件系统访问(除通过MCP工具外)
  • 网络访问(通过MCP工具除外)
  • 处理信息

安全提示: 这不是一个完全安全的沙盒。虚拟机环境提供了隔离,但并非万无一失。请仅执行受信任的代码。

错误处理

沙箱中的错误会被捕获并连同堆栈跟踪信息一起返回:

generate_and_execute_code({
  code: `
    try {
      const result = await filesystem_read_file({ path: "/nonexistent" });
      return result;
    } catch (error) {
      console.error("Failed to read file:", error.message);
      throw error; // Re-throw to surface to parent
    }
  `
})

使用Claude代码进行测试

想尝试使用 Claude Code 配合 mcpcodeserver?使用一键设置:

./setup-claude-code-test.sh

这将构建项目、安装测试依赖项,并向您准确展示需要添加到Claude代码配置中的内容。请参阅 使用Claude进行测试.md 以获取详细说明。

发展

# Install dependencies
bun install

# Build the project
bun run build

# Watch mode for development
bun run dev

# Run the server
bun start

# Run tests
bun test                # All tests
bun run test:unit       # Unit tests only
bun run test:integration # Integration tests (requires Python)

# Code quality
bun run lint            # Check linting
bun run format          # Format code
bun run typecheck       # Type checking

项目结构

看见 AGENTS.md 翻译为中文是:“代理列表/说明.md” 或者 “代理文档.md”,具体翻译可能根据上下文有所调整,但基本意思是这是一个关于代理(或代理商)的文档或说明文件 用于详细项目结构和组件文档。

用例

多文件操作

不要通过大型语言模型(LLM)多次调用工具,而是编写代码:

const files = ["/tmp/a.txt", "/tmp/b.txt", "/tmp/c.txt"];
const contents = await Promise.all(
  files.map(path => filesystem_read_file({ path }))
);
return contents.map(r => r.content[0].text);

数据转换

在工具调用之间处理数据,无需大型语言模型(LLM)介入:

const data = await api_fetch({ url: "https://api.example.com/data" });
const json = JSON.parse(data.content[0].text);
const filtered = json.items.filter(item => item.active);
return filtered.length;

条件逻辑

根据工具结果做出决策:

const exists = await filesystem_read_file({ path: "/tmp/config.json" });
if (exists.isError) {
  console.log("Config doesn't exist, using defaults");
  return { source: "defaults" };
} else {
  return { source: "file", config: JSON.parse(exists.content[0].text) };
}

错误恢复

优雅地处理错误,而不中止整个工作流程:

const results = [];
for (const path of ["/tmp/a.txt", "/tmp/b.txt", "/tmp/c.txt"]) {
  try {
    const content = await filesystem_read_file({ path });
    results.push({ path, success: true, data: content });
  } catch (error) {
    results.push({ path, success: false, error: error.message });
  }
}
return results;

上游MCP服务器集成

mcpcodeserver可以与官方上游MCP服务器集成 模型上下文协议服务器仓库这使您能够在使用自定义工具的同时,部署真实、可投入生产的MCP服务器。

支持的上游服务器

  • 文件系统文件系统操作(读取、写入、列出目录)
  • 记忆内存中的键值存储
  • SQLiteSQLite数据库操作
  • github(注:这是一个专有名词,通常直接音译为“吉布斯”,但在中文语境中,人们也常直接使用“GitHub”这一英文原名,不加翻译。)GitHub API 集成
  • 勇敢搜索网络搜索功能
  • 获取HTTP请求功能

示例配置

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
    },
    "memory": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-memory"]
    },
    "sqlite": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-sqlite", "--db-path", "/tmp/test.db"]
    }
  }
}

测试上游集成

该项目包括对上游服务器集成的全面测试:

# Run upstream servers integration tests
bun tests/integration/run-upstream-tests.ts

# Or manually test with upstream config
npx mcpcodeserver --config tests/integration/upstream-test-config.json

跨服务器工作流

借助上游服务器,您可以创建强大的跨服务器工作流:

// Store database query results in memory and write to file
const queryResult = await sqlite_execute_sql({
  sql: "SELECT COUNT(*) as count FROM users"
});
const count = queryResult.content[0].text;

await memory_create({
  key: "user-count",
  value: count
});

await filesystem_write_file({
  path: "/tmp/user-count.txt",
  content: `Total users: ${count}`
});

局限性

  • 执行超时:最长5分钟(可配置,默认30秒)
  • 内存:受限于 Node.js 虚拟机上下文
  • 执行之间无持久状态
  • 无法要求/导入外部模块
  • 不是一个安全沙盒——不要运行不可信的代码

贡献

欢迎贡献!本项目采用以下技术构建:

  • TypeScript 5.7及以上版本
  • Node.js 18及以上版本
  • MCP TypeScript SDK 1.20+(版本)
  • 用于验证的Zod

\CONTRIBUTING.md\ 翻译成中文是:“贡献指南.md” 或 “如何贡献.md”(具体翻译可能根据上下文略有调整,但基本意思是指向一个说明如何为项目做贡献的Markdown文件) 请参阅详细的贡献指南。

支持

如果你觉得这个项目对你有帮助,不妨请我喝杯咖啡!

![Buy Me A Coffee](https://buymeacoffee.com/zbowling)

许可证

麻省理工学院(MIT)

资源

目录标签

目录标签

代码生成TypeScriptClaude本地部署MCP协议工具编排LLM效率

支持客户端

Claude DesktopClaudeCursorWindsurfClineVS Code

接入字段

传输方式(transport,传输协议)

stdio

鉴权方式(authType,认证方式)

token

运行时(runtime,运行环境)

Node.js

来源包(packageName,安装包名)

github

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

stdiotoken部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

来源信息

继续浏览同类 MCP