使用说明

项目简介

WASImancer 是一个基于 Model Context Protocol (MCP) 构建的服务器,它使用 Node.js 开发,并创新性地引入 WebAssembly 插件来增强工具的执行能力。通过 Extism 框架,WASImancer 能够无缝集成 WebAssembly 模块作为插件工具,为大型语言模型 (LLM) 应用提供一个灵活、高性能且安全的上下文服务环境。

主要功能点

  • 资源管理 (Resources): 支持托管和管理各种资源,并通过标准化的 MCP 协议向客户端提供数据访问能力。
  • 工具执行 (Tools): 核心功能是通过 WebAssembly 插件实现工具的注册和执行。利用 WebAssembly 的优势,插件可以以接近原生速度执行,并支持多种编程语言开发,同时保证沙箱安全。插件可以动态加载、卸载和更新。
  • Prompt 模板 (Prompts): 支持定义和渲染 Prompt 模板,允许用户根据需求定制与 LLM 的交互模式。
  • SSE 传输协议: 使用 Server-Sent Events (SSE) 作为默认的客户端-服务器通信协议,实现高效的实时数据传输。
  • Docker 支持: 提供 Docker 镜像和 Docker Compose 配置,方便用户快速部署和运行 WASImancer 服务器。
  • 插件管理 API: 提供 RESTful API 接口,允许用户上传、删除和更新 WebAssembly 插件,实现动态的工具扩展。

安装步骤

  1. 克隆仓库:

    git clone https://github.com/sea-monkeys/WASImancer.git
    cd WASImancer
  2. 安装依赖: 确保你的环境中已安装 Node.js 和 npm。在项目根目录下运行:

    npm install
  3. 启动服务器: 你可以直接使用 Node.js 启动服务器:

    node index.js

    或者使用 Docker Compose 启动 (开发环境):

    docker compose --file compose.dev.yml up --build

    或者使用 Docker 运行 (生产环境,需要预先构建 Docker 镜像或使用提供的镜像 'k33g/wasimancer:0.0.1'):

    docker run --rm -p 3001:3001 \
      -e HTTP_PORT=3001 \
      -e PLUGINS_PATH=./plugins \
      -e PLUGINS_DEFINITION_FILE=plugins.yml \
      -v "$(pwd)/plugins":/app/plugins \
      -e RESOURCES_PATH=./resources \
      -e RESOURCES_DEFINITION_FILE=resources.yml \
      -v "$(pwd)/resources":/app/resources \
      -e PROMPTS_PATH=./prompts \
      -e PROMPTS_DEFINITION_FILE=prompts.yml \
      -v "$(pwd)/prompts":/app/prompts \
      -e UPLOAD_AUTH_TOKEN=wasimancer-rocks \
      -e UPLOAD_PATH=./plugins/bucket \
      k33g/wasimancer:0.0.1

服务器配置 (MCP 客户端配置)

MCP 客户端需要配置以下信息以连接到 WASImancer 服务器。以下是一个 JSON 格式的配置示例,你需要将其配置到你的 MCP 客户端应用中。

{
  "serverName": "wasimancer-server",
  "command": "node",
  "args": ["index.js"]
}

参数注释:

  • 'serverName': 服务器的名称,可以自定义。
  • 'command': 启动 WASImancer 服务器的命令,这里是 'node',假设 'index.js' 文件在当前工作目录。
  • 'args': 启动命令的参数,这里是 'index.js',表示执行 'index.js' 文件。

注意: 实际使用中,如果使用 Docker 部署 WASImancer,MCP 客户端通常需要配置连接到 Docker 容器暴露的端口和地址,而不是直接执行 'node index.js' 命令。对于基于浏览器的 MCP 客户端,通常配置 WASImancer 服务器的 HTTP SSE 端点 URL (例如 'http://localhost:3001/sse')。

基本使用方法

  1. 启动 WASImancer 服务器 (按照上述安装步骤)。
  2. 使用 MCP 客户端连接服务器。 你可以使用 Inspector 项目 或者 '@modelcontextprotocol/sdk' 开发的客户端来测试 WASImancer 服务器。
  3. 探索可用功能。 通过客户端,你可以:
    • 列出服务器提供的工具 (WebAssembly 插件)。
    • 调用工具执行特定任务。
    • 读取服务器托管的资源。
    • 使用预定义的 Prompt 模板与 LLM 交互。
  4. 动态管理插件。 WASImancer 提供了 REST API 用于插件管理,你可以通过 'POST /upload-plugin', 'DELETE /remove-plugin/:name', 'PUT /update-plugin' 等接口动态上传、删除和更新 WebAssembly 插件,扩展服务器的功能。

插件上传示例 (使用 curl)

上传插件需要提供 'wasmFile' 文件和 'pluginData' 元数据,并设置 'Authorization' header。

curl -X POST http://localhost:3001/upload-plugin \
  -H "Authorization: Bearer wasimancer-rocks" \
  -F wasmFile=@./plugins/example-plugin.wasm \
  -F 'pluginData=$(cat <<EOF
{
  "name": "example-plugin",
  "path": "example-plugin.wasm",
  "version": "1.0.0",
  "description": "Example WebAssembly Plugin",
  "functions": [
    {
      "displayName": "exampleTool",
      "function": "example_function",
      "description": "This is an example tool function",
      "arguments": [
        {
          "name": "input",
          "type": "string",
          "description": "Input string for the function"
        }
      ]
    }
  ]
}
EOF
)'

关键词 WebAssembly插件, 工具扩展, 上下文服务, SSE, 动态插件

信息

分类

开发者工具