深度

OpenClaw 插件开发完全指南:从零构建自定义渠道和工具插件

OpenClaw 插件(Plugin)开发完整教程:插件类型(渠道插件/工具插件/Provider插件)、插件的目录结构和 package.json 规范、使用 Plugin SDK 开发自定义消息渠道(实现 onMessage/sendMessage 接口)、开发自定义工具(Tool)的函数签名和参数 Schema、本地插件安装与调试(openclaw plugins install ./local-plugin)、发布到 npm 的规范要求(@openclaw/ 命名空间)、插件的权限声明(capabilities)、社区插件列表(Plugin Bundles)获取,以及常见插件开发错误和调试技巧。

2026/3/254分钟 阅读ClaudeEagle

OpenClaw 的渠道和工具都是插件系统的一部分。 如果内置插件满足不了你的需求,你可以自己开发。

插件类型

渠道插件(Channel Plugin): → 让 OpenClaw 接入新的消息平台 → 例:为公司内部 IM 系统开发接入插件 → 实现:onMessage / sendMessage / start / stop 工具插件(Tool Plugin): → 给 AI 添加新的工具能力 → 例:调用公司 ERP 系统的工具 → 实现:一组 Tool 函数(带参数 Schema) Provider 插件(Model Provider): → 接入新的 AI 模型服务 → 例:私有部署的模型 API → 实现:OpenAI 兼容接口适配

快速开始:安装与管理

bash
# 从 npm 安装官方插件
openclaw plugins install @openclaw/mattermost
openclaw plugins install @openclaw/matrix

# 从本地目录安装(开发中的插件)
openclaw plugins install ./my-custom-plugin

# 列出已安装插件
openclaw plugins list

# 更新所有插件
openclaw plugins update

# 卸载插件
openclaw plugins uninstall @openclaw/mattermost

项目结构(渠道插件)

my-channel-plugin/ package.json ← 插件元数据 index.js ← 入口文件 src/ channel.js ← 渠道实现 config-schema.js ← 配置项 Schema README.md

package.json 规范

json
{
  "name": "@yourscope/openclaw-my-channel",
  "version": "1.0.0",
  "description": "OpenClaw plugin for MyChat",
  "main": "index.js",
  "openclaw": {
    "type": "plugin",
    "pluginType": "channel",
    "channelId": "mychat",
    "capabilities": ["messages", "groups", "reactions"],
    "configSchema": "./src/config-schema.js"
  },
  "peerDependencies": {
    "openclaw": ">=1.0.0"
  }
}

关键字段:

  • openclaw.type:固定为 "plugin"
  • openclaw.pluginType:"channel" / "tool" / "provider"
  • openclaw.channelId:在配置文件中用的渠道名(channels.mychat)
  • openclaw.capabilities:声明支持的能力

渠道插件实现

javascript
// src/channel.js
class MyChatChannel {
  constructor(config, gateway) {
    this.config = config;
    this.gateway = gateway;
    this.client = null;
  }

  // 启动渠道(连接到外部平台)
  async start() {
    this.client = new MyChatClient({
      token: this.config.botToken,
      baseUrl: this.config.baseUrl,
    });

    // 监听消息
    this.client.on('message', (msg) => {
      this.gateway.ingest({
        channel: 'mychat',
        chatId: msg.channelId,
        userId: msg.userId,
        text: msg.text,
        timestamp: msg.ts,
      });
    });

    await this.client.connect();
  }

  // 停止渠道
  async stop() {
    await this.client?.disconnect();
  }

  // 发送消息(Gateway 调用此方法回复用户)
  async sendMessage(target, content) {
    await this.client.send({
      channelId: target.chatId,
      text: content.text,
    });
  }
}

module.exports = { MyChatChannel };

工具插件实现

javascript
// src/tools.js

// 工具定义(AI 会根据 description 和 parameters 决定何时调用)
const myErpTool = {
  name: "query_erp",
  description: "查询公司 ERP 系统中的订单、库存或客户信息",
  parameters: {
    type: "object",
    properties: {
      queryType: {
        type: "string",
        enum: ["order", "inventory", "customer"],
        description: "查询类型"
      },
      id: {
        type: "string",
        description: "订单号/产品ID/客户ID"
      }
    },
    required: ["queryType", "id"]
  },

  // 实际执行逻辑
  async execute({ queryType, id }, context) {
    const response = await fetch(
      `https://erp.company.com/api/${queryType}/${id}`,
      { headers: { Authorization: `Bearer ${context.config.erpToken}` } }
    );
    const data = await response.json();
    return JSON.stringify(data, null, 2);
  }
};

module.exports = { tools: [myErpTool] };

配置 Schema

javascript
// src/config-schema.js
module.exports = {
  type: "object",
  properties: {
    botToken: {
      type: "string",
      description: "Bot Token from MyChat developer portal",
      secret: true   // 标记为密钥,不会出现在日志中
    },
    baseUrl: {
      type: "string",
      description: "MyChat server URL",
      default: "https://chat.example.com"
    },
    dmPolicy: {
      type: "string",
      enum: ["open", "pairing", "allowlist"],
      default: "pairing"
    }
  },
  required: ["botToken", "baseUrl"]
};

本地开发与调试

bash
# 安装本地插件(软链接,修改立即生效)
openclaw plugins install ./my-channel-plugin --link

# 带详细日志启动
OPENCLAW_LOG_LEVEL=debug openclaw gateway start

# 查看插件加载状态
openclaw plugins list --verbose

发布到 npm

bash
# 登录 npm
npm login

# 测试打包
npm pack --dry-run

# 发布(推荐遵循 @openclaw/ 命名规范)
npm publish --access public

发布后其他用户可以直接安装:

bash
openclaw plugins install @yourscope/openclaw-my-channel

Plugin Bundles(插件包)

多个插件打包分发:

bash
# 安装一个 Bundle(包含多个相关插件)
openclaw plugins install @openclaw/bundle-enterprise

社区插件列表:

docs.openclaw.ai/plugins/community

来源:OpenClaw 官方文档 - docs.openclaw.ai/tools/plugin

相关文章推荐

深度OpenClaw Context Engine 完全指南:四个生命周期钩子如何决定模型看到什么详解 OpenClaw 可插拔上下文引擎架构:Ingest/Assemble/Compact/After turn 四个生命周期钩子的工作原理,systemPromptAddition 动态注入机制,以及如何安装和配置自定义 Context Engine 插件。2026/8/12深度OpenClaw Capability 架构指南:插件边界、共享运行时和供应商解耦OpenClaw Capability Cookbook 官方文档中文整理:什么时候创建 capability、标准开发顺序、core/vendor plugin/feature plugin 分工、provider registry、runtime helper、image generation 示例和架构审查清单。2026/6/4深度OpenClaw 开源生态全景:MIT 协议、插件系统、社区贡献与二次开发指南OpenClaw 开源生态完整介绍:MIT 开源协议含义、GitHub 仓库结构、Skills 插件市场(ClawHub)、社区贡献指南(提 PR/报 Issue)、自定义频道开发、自定义工具(Tool)扩展、本地开发环境搭建,以及如何基于 OpenClaw 打造自己的 AI 助手产品。2026/3/17深度OpenClaw Session ID 生命周期规则:什么时候会开新会话,什么时候延续旧会话详解 OpenClaw sessionKey 与 sessionId 的区别,以及触发新会话的四种情形:手动重置、每日重置、空闲过期、父级分叉保护,附 Session Store 字段说明和 Cron 会话保留策略。2026/8/13深度OpenClaw 计费故障处理机制:余额不足时系统怎么办,Backoff 退避策略详解详解 OpenClaw 账单/额度类故障处理机制:与普通限流超时不同,计费故障采用更长的指数退避(5小时起步翻倍至24小时封顶)并标记禁用,附三类故障处理力度对比表和多账号部署实战建议。2026/8/13深度OpenClaw Model Failover 完全解析:Auth Profile 怎么轮换,为什么你的 OAuth 账号会"莫名其妙"被切走详解 OpenClaw Model Failover 机制:Auth Profile 轮换顺序、Session Stickiness 会话粘性、指数退避冷却规则,解释多账号场景下 OAuth 与 API Key 切换的常见困惑及固定账号的配置方法。2026/8/13