refactor: 重构MCPOutputAdapter到mcp目录 - 优化代码组织结构
📁 目录重构 - 移动: src/lib/adapters/MCPOutputAdapter.js → src/lib/mcp/MCPOutputAdapter.js - 删除: src/lib/adapters/ 空目录 - 整合: 所有MCP相关组件统一在mcp目录下 🔧 引用更新 Components: - src/lib/commands/MCPServerCommand.js: 更新import路径 - src/lib/commands/MCPStreamableHttpCommand.js: 更新import路径 🎯 重构目标 - 统一MCP相关代码组织 - 提升代码可维护性 - 优化目录结构逻辑 📂 新的mcp目录结构 src/lib/mcp/ ├── MCPOutputAdapter.js # MCP输出适配器 └── toolDefinitions.js # MCP工具定义 ✅ 验证通过 - @tool://calculator 除法功能正常: 84 ÷ 2 = 42 - MCP Server功能完全正常 - 所有引用路径正确更新 💡 架构收益 - MCP组件集中管理 - 代码职责更清晰 - 便于后续MCP功能扩展 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
141
src/lib/mcp/MCPOutputAdapter.js
Normal file
141
src/lib/mcp/MCPOutputAdapter.js
Normal file
@ -0,0 +1,141 @@
|
||||
/**
|
||||
* MCP输出适配器
|
||||
* 负责将PromptX CLI的富文本输出转换为MCP标准JSON格式
|
||||
*
|
||||
* 设计原则:
|
||||
* - 保留所有emoji、markdown、中文字符
|
||||
* - 转换为MCP标准的content数组格式
|
||||
* - 提供统一的错误处理机制
|
||||
*/
|
||||
class MCPOutputAdapter {
|
||||
constructor() {
|
||||
this.version = '1.0.0';
|
||||
}
|
||||
|
||||
/**
|
||||
* 将CLI输出转换为MCP标准格式
|
||||
* @param {any} input - CLI输出(可能是字符串、对象、PouchOutput等)
|
||||
* @returns {object} MCP标准格式的响应
|
||||
*/
|
||||
convertToMCPFormat(input) {
|
||||
try {
|
||||
const text = this.normalizeInput(input);
|
||||
const sanitizedText = this.sanitizeText(text);
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: sanitizedText
|
||||
}
|
||||
]
|
||||
};
|
||||
} catch (error) {
|
||||
return this.handleError(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标准化输入,将各种类型转换为字符串
|
||||
* @param {any} input - 输入数据
|
||||
* @returns {string} 标准化后的字符串
|
||||
*/
|
||||
normalizeInput(input) {
|
||||
// 处理null和undefined
|
||||
if (input === null) return 'null';
|
||||
if (input === undefined) return 'undefined';
|
||||
|
||||
// 处理字符串
|
||||
if (typeof input === 'string') {
|
||||
return input;
|
||||
}
|
||||
|
||||
// 处理有toString方法的对象(如PouchOutput)
|
||||
if (input && typeof input.toString === 'function' && input.toString !== Object.prototype.toString) {
|
||||
return input.toString();
|
||||
}
|
||||
|
||||
// 处理数组和普通对象
|
||||
if (typeof input === 'object') {
|
||||
return JSON.stringify(input, null, 2);
|
||||
}
|
||||
|
||||
// 其他类型直接转换
|
||||
return String(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理文本,确保JSON兼容性但保留所有格式
|
||||
* @param {string} text - 输入文本
|
||||
* @returns {string} 清理后的文本
|
||||
*/
|
||||
sanitizeText(text) {
|
||||
// 对于MCP协议,我们实际上不需要做任何转义
|
||||
// emoji、中文字符、markdown都应该保留
|
||||
// MCP的content格式本身就支持UTF-8字符
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一的错误处理
|
||||
* @param {Error|string} error - 错误对象或错误信息
|
||||
* @returns {object} MCP格式的错误响应
|
||||
*/
|
||||
handleError(error) {
|
||||
const errorMessage = error instanceof Error
|
||||
? error.message
|
||||
: String(error);
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `❌ 执行失败: ${errorMessage}`
|
||||
}
|
||||
],
|
||||
isError: true
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证输出格式是否符合MCP标准
|
||||
* @param {object} output - 要验证的输出
|
||||
* @returns {boolean} 是否符合标准
|
||||
*/
|
||||
validateMCPFormat(output) {
|
||||
if (!output || typeof output !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Array.isArray(output.content)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return output.content.every(item =>
|
||||
item &&
|
||||
typeof item === 'object' &&
|
||||
item.type === 'text' &&
|
||||
typeof item.text === 'string'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建成功响应的快捷方法
|
||||
* @param {string} text - 响应文本
|
||||
* @returns {object} MCP格式响应
|
||||
*/
|
||||
createSuccessResponse(text) {
|
||||
return this.convertToMCPFormat(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建错误响应的快捷方法
|
||||
* @param {string} message - 错误消息
|
||||
* @returns {object} MCP格式错误响应
|
||||
*/
|
||||
createErrorResponse(message) {
|
||||
return this.handleError(message);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { MCPOutputAdapter };
|
||||
Reference in New Issue
Block a user