@utaba/ucm-mcp-server
Version:
Universal Context Manager MCP Server - AI-native artifact management
80 lines • 2.97 kB
JavaScript
import { McpError, McpErrorCode, McpErrorHandler } from '../utils/McpErrorHandler.js';
export class McpHandler {
toolRegistry;
logger;
authorId;
constructor(toolRegistry, logger, authorId) {
this.toolRegistry = toolRegistry;
this.logger = logger;
this.authorId = authorId;
}
async handlePing() {
this.logger.debug('MCP-Handler', 'Received ping request');
return {};
}
async handleToolsList() {
this.logger.debug('MCP-Handler', 'Received tools/list request');
try {
const tools = await this.toolRegistry.listTools();
this.logger.info('MCP-Handler', `Returning ${tools.length} available tools`);
return {
tools: tools.map(tool => ({
name: tool.name,
description: tool.description,
inputSchema: tool.inputSchema
}))
};
}
catch (error) {
this.logger.error('MCP-Handler', 'Failed to list tools', '', error);
throw new McpError(McpErrorCode.InternalError, 'Failed to retrieve tool list');
}
}
async handleToolCall(request) {
const { name, arguments: args } = request.params;
this.logger.debug('MCP-Handler', `Received tool call for: ${name}`);
try {
if (!name) {
throw new McpError(McpErrorCode.InvalidParams, 'Tool name is required');
}
const result = await this.toolRegistry.executeTool(name, args || {});
this.logger.info('MCP-Handler', `Tool ${name} executed successfully`);
// Handle string results directly (e.g., markdown content)
if (typeof result === 'string') {
return {
content: [
{
type: 'text',
text: result
}
]
};
}
// For objects/arrays, use JSON.stringify
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2)
}
]
};
}
catch (error) {
this.logger.error('MCP-Handler', `Tool execution failed for ${name}`, '', error);
// Format the error message for Claude Desktop to display properly
const formattedError = McpErrorHandler.formatError(error);
// Instead of throwing, return the error as content so Claude Desktop displays it
return {
content: [
{
type: 'text',
text: `Error: ${formattedError.message}`
}
],
isError: true
};
}
}
}
//# sourceMappingURL=McpHandler.js.map