mushcode-mcp-server
Version:
A specialized Model Context Protocol server for MUSHCODE development assistance. Provides AI-powered code generation, validation, optimization, and examples for MUD development.
118 lines • 3.3 kB
JavaScript
import { ValidationError } from '../utils/errors.js';
export class MushcodeToolRegistry {
tools = new Map();
handlers = new Map();
/**
* Register a tool definition
*/
registerTool(tool) {
if (!tool.name || typeof tool.name !== 'string') {
throw new ValidationError('Tool name is required and must be a string');
}
if (!tool.description || typeof tool.description !== 'string') {
throw new ValidationError('Tool description is required and must be a string');
}
this.tools.set(tool.name, tool);
}
/**
* Register a tool handler function
*/
registerToolHandler(name, handler) {
if (!name || typeof name !== 'string') {
throw new ValidationError('Tool name is required and must be a string');
}
if (typeof handler !== 'function') {
throw new ValidationError('Tool handler must be a function');
}
this.handlers.set(name, handler);
}
/**
* 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 a tool handler by name
*/
getToolHandler(name) {
return this.handlers.get(name);
}
/**
* Check if a tool is registered
*/
hasTool(name) {
return this.tools.has(name);
}
/**
* Check if a tool handler is registered
*/
hasToolHandler(name) {
return this.handlers.has(name);
}
/**
* Get the names of all registered tools
*/
getToolNames() {
return Array.from(this.tools.keys());
}
/**
* Get the count of registered tools
*/
getToolCount() {
return this.tools.size;
}
/**
* Unregister a tool and its handler
*/
unregisterTool(name) {
const toolRemoved = this.tools.delete(name);
const handlerRemoved = this.handlers.delete(name);
return toolRemoved || handlerRemoved;
}
/**
* Clear all tools and handlers
*/
clear() {
this.tools.clear();
this.handlers.clear();
}
/**
* Execute a tool with the given arguments
*/
async callTool(name, args = {}) {
const tool = this.getTool(name);
if (!tool) {
throw new Error(`Tool '${name}' not found`);
}
const handler = this.getToolHandler(name);
if (!handler) {
throw new Error(`No handler registered for tool '${name}'`);
}
return await handler(args);
}
/**
* Validate that all registered tools have corresponding handlers
*/
validateRegistry() {
const errors = [];
for (const toolName of this.tools.keys()) {
if (!this.handlers.has(toolName)) {
errors.push(`Tool '${toolName}' has no registered handler`);
}
}
for (const handlerName of this.handlers.keys()) {
if (!this.tools.has(handlerName)) {
errors.push(`Handler '${handlerName}' has no corresponding tool definition`);
}
}
return errors;
}
}
//# sourceMappingURL=registry.js.map