evolution-api-mcp
Version:
MCP Server for Evolution API v2 - Integrate WhatsApp functionality with Claude Desktop and other MCP clients
191 lines (190 loc) • 5.98 kB
JavaScript
/**
* MCP Tool Registry Implementation
* Manages registration and retrieval of MCP tools for Evolution API
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.mcpToolRegistry = exports.McpToolRegistry = void 0;
const instance_tools_1 = require("./tools/instance-tools");
/**
* Implementation of the MCP tool registry
*/
class McpToolRegistry {
constructor() {
this.tools = new Map();
}
/**
* Register a new tool
*/
registerTool(toolInfo) {
if (this.tools.has(toolInfo.name)) {
throw new Error(`Tool with name '${toolInfo.name}' is already registered`);
}
// Validate tool info
this.validateToolInfo(toolInfo);
this.tools.set(toolInfo.name, { ...toolInfo });
}
/**
* Get all registered tools
*/
getTools() {
return Array.from(this.tools.values());
}
/**
* Get a specific tool by name
*/
getTool(name) {
return this.tools.get(name);
}
/**
* Get tools filtered by controller type
*/
getToolsByController(controller) {
return this.getTools().filter(tool => tool.controller === controller);
}
/**
* Update an existing tool
*/
updateTool(name, updates) {
const existingTool = this.tools.get(name);
if (!existingTool) {
throw new Error(`Tool with name '${name}' not found`);
}
const updatedTool = { ...existingTool, ...updates };
// Validate updated tool info
this.validateToolInfo(updatedTool);
this.tools.set(name, updatedTool);
}
/**
* Remove a tool from the registry
*/
removeTool(name) {
if (!this.tools.has(name)) {
throw new Error(`Tool with name '${name}' not found`);
}
this.tools.delete(name);
}
/**
* Clear all registered tools
*/
clear() {
this.tools.clear();
}
/**
* Get registry statistics
*/
getStats() {
const tools = this.getTools();
const stats = {
total: tools.length,
byController: {},
registered: tools.map(tool => tool.name)
};
// Count by controller
const controllers = ['instance', 'message', 'chat', 'group', 'profile', 'webhook', 'information'];
controllers.forEach(controller => {
stats.byController[controller] = this.getToolsByController(controller).length;
});
return stats;
}
/**
* Check if a tool is registered
*/
hasTool(name) {
return this.tools.has(name);
}
/**
* Get all registered tool names
*/
getToolNames() {
return Array.from(this.tools.keys());
}
/**
* Search tools by name or description
*/
searchTools(query) {
const lowerQuery = query.toLowerCase();
return this.getTools().filter(tool => tool.name.toLowerCase().includes(lowerQuery) ||
tool.description.toLowerCase().includes(lowerQuery));
}
/**
* Validate tool information
*/
validateToolInfo(toolInfo) {
const errors = [];
if (!toolInfo.name || typeof toolInfo.name !== 'string') {
errors.push('Tool name is required and must be a string');
}
if (!toolInfo.description || typeof toolInfo.description !== 'string') {
errors.push('Tool description is required and must be a string');
}
if (!toolInfo.controller) {
errors.push('Tool controller is required');
}
if (!toolInfo.endpoint) {
errors.push('Tool endpoint is required');
}
if (!toolInfo.schema) {
errors.push('Tool schema is required');
}
if (!toolInfo.handler || typeof toolInfo.handler !== 'function') {
errors.push('Tool handler is required and must be a function');
}
// Validate tool name format (should be valid MCP tool name)
if (toolInfo.name && !/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(toolInfo.name)) {
errors.push('Tool name must start with a letter and contain only letters, numbers, underscores, and hyphens');
}
if (errors.length > 0) {
throw new Error(`Invalid tool info: ${errors.join(', ')}`);
}
}
/**
* Register multiple tools at once
*/
registerTools(tools) {
const errors = [];
tools.forEach((tool, index) => {
try {
this.registerTool(tool);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
errors.push(`Tool at index ${index} (${tool.name}): ${errorMessage}`);
}
});
if (errors.length > 0) {
throw new Error(`Failed to register some tools: ${errors.join('; ')}`);
}
}
/**
* Register all instance controller tools
*/
registerInstanceTools(httpClient) {
const instanceTools = new instance_tools_1.InstanceTools(httpClient);
const tools = instanceTools.getAllTools();
this.registerTools(tools);
}
/**
* Export tools configuration for debugging
*/
exportConfig() {
return {
tools: this.getTools().map(tool => ({
name: tool.name,
description: tool.description,
controller: tool.controller,
endpoint: {
name: tool.endpoint.name,
path: tool.endpoint.path,
method: tool.endpoint.method
},
hasHandler: typeof tool.handler === 'function',
hasSchema: !!tool.schema
})),
stats: this.getStats()
};
}
}
exports.McpToolRegistry = McpToolRegistry;
// Export a singleton instance
exports.mcpToolRegistry = new McpToolRegistry();
;