项目简介

CodeMesh是一个功能强大的MCP服务器,旨在简化对其他MCP服务器的管理和交互。它允许用户编写TypeScript代码来调用和协调多个MCP服务器的工具,从而实现复杂的自动化工作流、数据处理和系统集成。CodeMesh通过提供工具发现、类型生成、代码执行和文档增强等功能,极大地提升了LLM(大语言模型)应用与MCP生态系统的交互效率和智能性。

主要功能点

  • 多服务器编排: 连接并管理多个HTTP或Stdio类型的MCP服务器。
  • TypeScript代码执行: 接受并安全地执行包含MCP工具调用的TypeScript代码,支持复杂的逻辑和数据流。
  • 工具发现: 自动发现所有配置的MCP服务器及其提供的工具。
  • API类型生成: 为LLM客户端提供所选工具的TypeScript API定义,方便代码编写和理解。
  • 工具文档增强: 支持为工具添加自定义的Markdown文档(augmentation),包括输出格式、解析示例等,以提升LLM对工具使用的理解和效果。

安装步骤

  1. 前提条件: 确保您的系统已安装 Node.js (版本18或更高) 和 npm。建议全局安装 'tsx' 以直接运行TypeScript文件:'npm install -g tsx'。

  2. 克隆仓库:

    git clone https://github.com/IDragos94/codemesh.git
  3. 进入项目目录:

    cd codemesh
  4. 安装依赖:

    npm install
  5. 配置CodeMesh需要管理的MCP服务器: 在项目根目录(或您希望运行CodeMesh服务器的目录)下创建一个名为 '.codemesh' 的文件夹。在该文件夹内创建一个 'config.json' 文件,用于定义CodeMesh需要管理的其他MCP服务器。

    示例 '.codemesh/config.json':

    {
      "servers": [
        {
          "id": "weather-server",
          "name": "Weather Server",
          "type": "stdio",
          "command": ["npx", "tsx", "packages/weather-server/src/index.ts"],
          "cwd": "."
        },
        {
          "id": "geocode-server",
          "name": "Geocode Server",
          "type": "stdio",
          "command": ["npx", "tsx", "packages/geocode-server/src/index.ts"],
          "cwd": "."
        }
      ],
      "logging": {
        "enabled": true,
        "level": "info",
        "logDir": ".codemesh/logs"
      }
    }
    • 说明: 'command' 数组中的路径应相对于 'cwd' 运行。上述示例假设 'tsx' 可用且您在 'codemesh' 仓库的根目录运行。
  6. 启动CodeMesh服务器: 在 'codemesh' 仓库的根目录运行:

    npx tsx packages/codemesh-server/src/index.ts

    服务器将通过标准输入输出(stdio)运行,并打印错误日志到标准错误流。

服务器配置(供MCP客户端使用)

以下是一个MCP客户端配置CodeMesh服务器的示例。请根据您的实际安装路径和启动方式进行调整。

[
  {
    "id": "codemesh",
    "name": "CodeMesh Server",
    "type": "stdio",
    "description": "CodeMesh MCP Server: 执行复杂TypeScript代码,管理和编排其他MCP服务器工具。",
    "command": ["npx", "tsx", "packages/codemesh-server/src/index.ts"],
    "cwd": "/path/to/your/codemesh/repository"
  }
]
  • 说明:
    • 'id': CodeMesh服务器的唯一标识符。
    • 'name': CodeMesh服务器的友好名称。
    • 'type': 连接类型,此处为 'stdio'。
    • 'description': 服务器的简要描述。
    • 'command': 启动CodeMesh服务器的命令及其参数。确保 'tsx' 已安装。
    • 'cwd': 启动命令的工作目录,应设置为您的 'codemesh' 仓库的根目录。

基本使用方法

一旦CodeMesh服务器通过MCP客户端连接,您可以使用其提供的工具来管理其他MCP服务器:

  1. 发现工具 ('discover-tools'): 调用 'discover-tools' 工具来列出所有已在 '.codemesh/config.json' 中配置的MCP服务器及其提供的工具。

    • 调用示例: 'discover-tools()'
    • 用途: 返回一份详细的工具列表,显示每个工具的名称、描述以及它所属的服务器,例如 'weatherServer.getAlerts'。
  2. 获取工具API ('get-tool-apis'): 根据 'discover-tools' 的结果,选择您想使用的工具,然后调用 'get-tool-apis' 来获取其TypeScript类型定义和增强文档。

    • 调用示例: 'get-tool-apis(["weatherServer.getAlerts", "geocodeServer.geocode"])'
    • 用途: 返回包含相应服务器对象和方法(如 'WeatherServer' 和 'GeocodeServer')的TypeScript接口定义和详细的JSDoc,LLM可以使用这些定义来理解如何调用工具及其输入/输出结构。
  3. 执行代码 ('execute-code'): 使用 'execute-code' 工具执行TypeScript代码。您的代码可以调用通过 'get-tool-apis' 获取的工具方法。

    • 调用示例:
      execute-code({
        code: '
          // EXPLORING: Checking weather alerts for CA
          const alerts = await weatherServer.getAlerts({ state: "CA" });
          console.log(alerts);
        '
      })
    • 用途: 执行复杂的自动化任务。
    • 重要提示: 在代码中添加 '// EXPLORING' 注释可以触发CodeMesh的增强模式,它会提供详细的输出指导并强制要求创建工具增强文档的流程,以帮助LLM更好地理解未明确输出格式的工具。
  4. 添加工具增强 ('add-augmentation'): 如果发现某个工具的输出格式不清晰或难以解析,您应该使用 'add-augmentation' 工具为其提供详细的Markdown文档。这将改进 'get-tool-apis' 生成的JSDoc,帮助LLM更好地理解工具。

    • 调用示例:
      add-augmentation({
        toolName: "weatherServer.getAlerts",
        markdown: '
          # weatherServer.getAlerts
          ## Output Format
          JSON object with a \'features\' array, where each feature is an alert.
          ### Fields
          - event: string (Type of alert, e.g., 'Winter Storm Warning')
          - severity: string (Extreme, Severe, Moderate, Minor)
          ### Example Output
          \'\'\'json
          { "features": [...] }
          \'\'\'
          ### Parsing Example
          \'\'\'typescript
          const firstAlert = alerts.features[0].properties.event;
          console.log("First alert event:", firstAlert);
          \'\'\'
        '
      })
    • 用途: 通过人工介入提供详细的输出格式、解析示例等,显著提高LLM使用工具的准确性和效率。

信息

分类

开发者工具