freature: 支持mcp 协议

This commit is contained in:
7.
2025-06-06 16:16:12 +08:00
parent 8d34022d31
commit 11824a5ff3
12 changed files with 3598 additions and 154 deletions

View File

@ -48,9 +48,10 @@ class PouchCLI {
* 执行命令
* @param {string} commandName - 命令名称
* @param {Array} args - 命令参数
* @param {boolean} silent - 静默模式不输出到console用于MCP
* @returns {Promise<PouchOutput>} 执行结果
*/
async execute (commandName, args = []) {
async execute (commandName, args = [], silent = false) {
// 确保已初始化
if (!this.initialized) {
await this.initialize()
@ -65,16 +66,22 @@ class PouchCLI {
// 通过状态机执行命令
const result = await this.stateMachine.transition(commandName, args)
// 如果结果有 toString 方法,打印人类可读格式
if (result && result.toString && typeof result.toString === 'function') {
console.log(result.toString())
} else {
console.log(JSON.stringify(result, null, 2))
// 只在非静默模式下输出避免干扰MCP协议
if (!silent) {
// 如果结果有 toString 方法,打印人类可读格式
if (result && result.toString && typeof result.toString === 'function') {
console.log(result.toString())
} else {
console.log(JSON.stringify(result, null, 2))
}
}
return result
} catch (error) {
console.error(`执行命令出错: ${error.message}`)
// 错误输出始终使用stderr不干扰MCP协议
if (!silent) {
console.error(`执行命令出错: ${error.message}`)
}
throw error
}
}