功能介绍
工具发现机制是 OpenClaw Agent 系统的核心能力之一。它允许 Agent 在运行时动态发现可用工具、理解工具能力、智能匹配任务需求,并自动加载执行。这种机制使 Agent 不再依赖硬编码的工具列表,而是能够像人类一样"找到"合适的工具来完成任务。
核心价值
工具发现让 Agent 具备了真正的自主性——面对新任务时,它能自动搜索并选择最佳工具组合,而无需人工预先配置。
工具发现让 Agent 具备了真正的自主性——面对新任务时,它能自动搜索并选择最佳工具组合,而无需人工预先配置。
工具发现的三层架构
1. 本地注册层
- Skill 扫描:自动扫描 SKILL.md 文件,提取工具描述和参数
- MCP 注册:从 MCP 服务器动态获取可用工具列表
- 能力标注:为每个工具标注能力标签(如 data-analysis, code-generation)
2. 远程发现层
- ClawHub 搜索:从 ClawHub 技能市场搜索并安装新工具
- 社区索引:查询社区贡献的工具注册表
- 版本检查:自动检测工具更新和新版本
3. 智能匹配层
- 语义匹配:基于任务描述匹配最相关的工具
- 参数推断:根据上下文推断工具参数
- 链式组合:自动发现工具间的依赖和组合关系
使用方法
配置工具发现
// OpenClaw 工具发现配置
// 在 SOUL.md 或 agent 配置中定义
module.exports = {
toolDiscovery: {
// 本地扫描路径
scanPaths: [
'~/.openclaw/skills/',
'~/.local/share/pnpm/global/*/node_modules/openclaw/extensions/'
],
// 远程发现源
remoteSources: {
clawhub: {
enabled: true,
autoInstall: false, // 需要人工确认安装
searchEndpoint: 'https://clawhub.io/api/v1/skills/search'
},
community: {
enabled: true,
registryUrl: 'https://registry.openclaw.dev/tools'
}
},
// 智能匹配配置
matching: {
minRelevanceScore: 0.6,
maxToolsPerTask: 5,
preferInstalled: true
}
}
};
实现工具发现管理器
// OpenClaw 工具发现管理器
class ToolDiscoveryManager {
constructor(config) {
this.localTools = new Map();
this.remoteTools = new Map();
this.capabilityIndex = new Map();
this.config = config;
}
// 扫描本地工具
async scanLocalTools() {
const skillFiles = await this.findSkillFiles();
for (const file of skillFiles) {
const skillData = await this.parseSkillFile(file);
this.registerTool(skillData);
}
return this.localTools.size;
}
// 解析 SKILL.md 文件
async parseSkillFile(filePath) {
const content = await readFile(filePath, 'utf-8');
// 提取元数据
const name = extractBetween(content, '# ', '\n');
const description = extractBetween(content, 'description', '\n');
const capabilities = this.extractCapabilities(content);
return {
name,
description,
capabilities,
source: 'local',
filePath,
version: '1.0.0'
};
}
// 注册工具到能力索引
registerTool(tool) {
this.localTools.set(tool.name, tool);
// 建立能力到工具的映射
for (const cap of tool.capabilities) {
if (!this.capabilityIndex.has(cap)) {
this.capabilityIndex.set(cap, new Set());
}
this.capabilityIndex.get(cap).add(tool.name);
}
}
// 根据任务需求发现工具
async discoverTools(taskDescription) {
const taskCapabilities = await this.analyzeTaskCapabilities(taskDescription);
const candidates = new Map();
for (const cap of taskCapabilities) {
const tools = this.capabilityIndex.get(cap);
if (tools) {
for (const toolName of tools) {
const score = candidates.get(toolName) || 0;
candidates.set(toolName, score + 1);
}
}
}
// 按匹配度排序
return Array.from(candidates.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, this.config.matching.maxToolsPerTask)
.map(([name, score]) => ({
tool: this.localTools.get(name),
relevanceScore: score / taskCapabilities.length
}));
}
}
最佳实践
- 丰富的能力标注:为每个工具提供详细的能力描述,提高匹配精度
- 分层发现:优先使用本地工具,再考虑远程发现
- 安全审查:远程工具安装前进行安全审查
- 缓存机制:缓存发现结果,减少重复搜索
- 反馈循环:记录工具使用效果,优化未来匹配
安全注意
启用自动安装远程工具时,务必配置安全审查机制,防止恶意工具注入。
启用自动安装远程工具时,务必配置安全审查机制,防止恶意工具注入。