feat: 实现@tool协议完整功能 - JavaScript工具执行框架

 核心功能
- 新增ToolProtocol处理器,支持@tool://协议解析
- 实现PromptXToolCommand,统一MCP/CLI工具调用
- 完善ToolExecutor,支持工具实例化和参数验证
- 新增calculator和send-email示例工具

🔧 技术改进
- 优化PackageDiscovery统一资源扫描逻辑
- 增强CrossPlatformFileScanner支持.tool.js文件
- 完善ResourceManager集成ToolProtocol
- 更新MCP工具定义支持promptx_tool

📋 详细变更
Core:
- src/lib/core/resource/protocols/ToolProtocol.js: 新增工具协议处理器
- src/lib/commands/PromptXToolCommand.js: 新增工具命令处理器
- src/lib/tool/ToolExecutor.js: 增强工具执行器兼容性

Discovery:
- src/lib/core/resource/discovery/PackageDiscovery.js: 统一资源扫描
- src/lib/core/resource/discovery/CrossPlatformFileScanner.js: 支持tool文件
- src/lib/core/resource/discovery/ProjectDiscovery.js: 增加tool验证

Integration:
- src/lib/core/resource/resourceManager.js: 集成ToolProtocol
- src/lib/mcp/toolDefinitions.js: 新增promptx_tool定义
- src/lib/commands/MCPServerCommand.js: 支持tool参数转换
- src/bin/promptx.js: 新增tool命令行支持

Tools:
- prompt/tool/calculator.tool.js: 数学计算工具示例
- prompt/tool/send-email.tool.js: 邮件发送工具示例

Registry:
- src/package.registry.json: 自动生成包含2个tool资源

🧪 测试验证
-  @tool://calculator 数学计算: 25 + 37 = 62
-  @tool://send-email 邮件发送演示版本
-  CLI和MCP双模式支持
-  完整的错误处理和执行元数据
-  资源自动发现和注册

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
sean
2025-06-28 14:15:24 +08:00
parent 70093018f8
commit 40e0c01c59
17 changed files with 1725 additions and 646 deletions

View File

@ -0,0 +1,139 @@
/**
* @tool calculator
* @description 数学计算工具
* @version 1.0.0
*/
module.exports = {
/**
* 获取工具元信息
* @returns {Object} 工具元信息
*/
getMetadata() {
return {
name: 'calculator',
description: '提供基础数学计算功能,支持加减乘除运算',
version: '1.0.0',
author: 'PromptX Framework',
category: 'math',
tags: ['calculator', 'math', 'arithmetic']
}
},
/**
* 获取参数Schema
* @returns {Object} JSON Schema定义
*/
getSchema() {
return {
type: 'object',
properties: {
operation: {
type: 'string',
enum: ['add', 'subtract', 'multiply', 'divide'],
description: '数学运算类型'
},
a: {
type: 'number',
description: '第一个操作数'
},
b: {
type: 'number',
description: '第二个操作数'
}
},
required: ['operation', 'a', 'b'],
additionalProperties: false
}
},
/**
* 验证参数
* @param {Object} params - 输入参数
* @returns {boolean} 验证结果
*/
validate(params) {
const { operation, a, b } = params
// 检查必需参数
if (!operation || typeof a !== 'number' || typeof b !== 'number') {
return false
}
// 检查操作类型
const validOperations = ['add', 'subtract', 'multiply', 'divide']
if (!validOperations.includes(operation)) {
return false
}
// 检查除零
if (operation === 'divide' && b === 0) {
return false
}
return true
},
/**
* 执行计算
* @param {Object} params - 计算参数
* @param {string} params.operation - 运算类型 ('add', 'subtract', 'multiply', 'divide')
* @param {number} params.a - 第一个操作数
* @param {number} params.b - 第二个操作数
* @returns {Object} 计算结果
*/
async execute(params) {
const { operation, a, b } = params
let result
let expression
switch (operation) {
case 'add':
result = a + b
expression = `${a} + ${b}`
break
case 'subtract':
result = a - b
expression = `${a} - ${b}`
break
case 'multiply':
result = a * b
expression = `${a} × ${b}`
break
case 'divide':
if (b === 0) {
throw new Error('Division by zero is not allowed')
}
result = a / b
expression = `${a} ÷ ${b}`
break
default:
throw new Error(`Unknown operation: ${operation}`)
}
return {
expression,
result,
operation,
operands: { a, b },
formatted: `${expression} = ${result}`
}
},
/**
* 工具初始化(可选)
*/
async init() {
// 可以在这里进行工具初始化工作
return true
},
/**
* 工具清理(可选)
*/
async cleanup() {
// 可以在这里进行清理工作
return true
}
}