Skip to content

扩展概述

MicroAgent 采用插件式扩展架构,支持四种主要扩展类型:工具、技能、通道、插件。

扩展架构

整体架构

加载中...

Applications 层扩展

加载中...

Agent Service 层注册表

加载中...

扩展类型对比

特性工具 (Tool)技能 (Skill)通道 (Channel)插件 (Plugin)
定义方式defineTool()SKILL.mddefineChannel()UserPlugin 接口
主要用途LLM 可调用操作领域知识/提示词消息收发功能扩展
加载时机启动时注册按需/自动加载启动时启动发现后激活
执行主体LLM 决定调用注入系统提示消息总线驱动事件/命令触发
依赖注入BuiltinToolProviderBuiltinSkillProvider直接实例化ExtensionContext
存储位置applications/cli/src/builtin/toolapplications/cli/src/builtin/skillsapplications/cli/src/builtin/channel~/.micro-agent/extensions
适用场景文件操作、命令执行文档处理、时间计算IM 集成、WebhookCLI 命令、事件处理

工具扩展

定义方式

typescript
import { defineTool } from '@micro-agent/sdk';

export const myTool = defineTool({
  name: 'my_tool',
  description: `我的自定义工具。

**使用场景**:
- 场景一描述
- 场景二描述`,
  inputSchema: {
    type: 'object',
    properties: {
      message: { type: 'string', description: '输入消息' },
    },
    required: ['message'],
  },
  examples: [
    { description: '基本用法', input: { message: 'Hello' } },
  ],
  execute: async (input, ctx) => {
    return `处理结果: ${input.message}`;
  },
});

关键特性

  • MCP 兼容: 遵循 Model Context Protocol 的 Tool 原语规范
  • 参数验证: 自动验证 inputSchema,失败返回结构化错误
  • 结构化错误: 提供 ToolErrorType 分类错误

内置工具

工具名功能
read读取文件内容
write创建或覆盖文件
exec执行 Shell 命令
glob文件模式匹配
grep内容正则搜索
edit精确文件编辑
list_directory列出目录内容
todo_write任务列表管理
todo_read读取任务列表
ask_user用户交互提问

技能扩展

定义方式 (SKILL.md)

markdown
---
name: time
description: 时间处理工具 - 获取时间、时区转换、时间差计算
dependencies:
  - bun>=1.0
compatibility: bun
always: true
allowed-tools: []
---

# 时间处理工具

技能内容(Markdown 格式)...

元数据字段

字段类型说明
namestring技能名称(kebab-case)
descriptionstring技能描述
dependenciesstring[]依赖包
alwaysboolean是否自动注入上下文
allowed-toolsstring[]允许使用的工具列表

加载优先级

1. builtin/           (内置技能)
2. ~/.micro-agent/skills/  (用户技能)
3. {workspace}/skills/     (项目技能)

内置技能

技能名功能
time时间处理工具
sysinfo系统信息技能
pdfPDF 文档处理
docxWord 文档处理
xlsxExcel 处理
pptxPowerPoint 处理
doc-coauthoring文档协作技能
skill-creator技能创建工具

通道扩展

定义方式

typescript
import { defineChannel } from '@micro-agent/sdk';

export const myChannel = defineChannel({
  name: 'my-channel' as ChannelType,
  start: async () => { 
    // 初始化连接
  },
  stop: async () => { 
    // 关闭连接
  },
  send: async (msg: OutboundMessage) => { 
    // 发送消息
  },
});

Channel 接口

typescript
interface Channel {
  readonly name: ChannelType;
  readonly isRunning: boolean;
  start(): Promise<void>;
  stop(): Promise<void>;
  send(msg: OutboundMessage): Promise<void>;
}

内置通道

通道名功能
feishu飞书机器人

插件扩展

定义方式

typescript
const myPlugin: UserPlugin = {
  id: 'my-plugin',
  name: '我的插件',
  version: '1.0.0',
  activate: (context: PluginContext) => {
    // 注册命令
    context.registerCommand({
      id: 'cmd',
      name: '命令',
      handler: async () => { /* ... */ },
    });
    // 注册钩子
    context.registerHook({
      event: 'message:received',
      handler: async (data) => { /* ... */ },
    });
  },
  deactivate: () => {
    // 清理资源
  },
};

PluginContext 接口

typescript
interface PluginContext {
  pluginDir: string;
  homeDir: string;
  workspace: string;
  registerCommand: (command: PluginCommand) => void;
  registerHook: (hook: PluginHook) => void;
  log: (level: LogLevel, message: string) => void;
}

钩子系统

钩子类型

钩子触发时机
pre:inbound消息入站前
post:inbound消息入站后
pre:outbound消息出站前
post:outbound消息出站后
pre:tool工具执行前
post:tool工具执行后
pre:llmLLM 调用前
post:llmLLM 调用后

使用方式

typescript
hookSystem.registerHook('pre:tool', async (ctx) => {
  // 处理逻辑
  return { data: ctx.data, stop: false };
}, 100); // 优先级(越小越先执行)

清单文件

扩展通过清单文件描述:

yaml
# extension.yaml
id: my-extension
name: My Extension
version: 1.0.0
type: tool
description: 扩展描述
main: index.ts
dependencies:
  - bun>=1.0

支持的清单文件名(按优先级):

  • extension.yaml
  • extension.yml
  • extension.json
  • package.json(需包含 microAgent 字段)

热重载

typescript
interface HotReloadConfig {
  enabled: boolean;        // 是否启用(默认 true)
  debounceMs: number;      // 防抖延迟(默认 1000ms)
  gracefulTimeout: number; // 优雅等待超时(默认 30000ms)
}

热重载会等待当前活动调用完成后再重载,超时后强制重载。

基于 MIT 许可证开源