Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | /** * V3 MCP Tool Registry * * High-performance tool management with: * - Fast O(1) lookup using Map * - Category-based organization * - Tool validation on registration * - Dynamic registration/unregistration * - Caching for frequently used tools * * Performance Targets: * - Tool registration: <10ms * - Tool lookup: <1ms * - Tool validation: <5ms */ import { EventEmitter } from 'events'; import { MCPTool, JSONSchema, ToolHandler, ToolContext, ToolCallResult, ToolRegistrationOptions, ILogger, } from './types.js'; /** * Tool metadata for enhanced lookup */ interface ToolMetadata { tool: MCPTool; registeredAt: Date; callCount: number; lastCalled?: Date; avgExecutionTime: number; errorCount: number; } /** * Tool search options */ interface ToolSearchOptions { category?: string; tags?: string[]; deprecated?: boolean; cacheable?: boolean; } /** * Tool Registry * * Manages registration, lookup, and execution of MCP tools */ export class ToolRegistry extends EventEmitter { private readonly tools: Map<string, ToolMetadata> = new Map(); private readonly categoryIndex: Map<string, Set<string>> = new Map(); private readonly tagIndex: Map<string, Set<string>> = new Map(); private defaultContext?: ToolContext; // Performance tracking private totalRegistrations = 0; private totalLookups = 0; private totalExecutions = 0; constructor(private readonly logger: ILogger) { super(); } /** * Register a tool */ register(tool: MCPTool, options: ToolRegistrationOptions = {}): boolean { const startTime = performance.now(); // Check for existing tool if (this.tools.has(tool.name) && !options.override) { this.logger.warn('Tool already registered', { name: tool.name }); return false; } // Validate tool if requested if (options.validate !== false) { const validation = this.validateTool(tool); if (!validation.valid) { this.logger.error('Tool validation failed', { name: tool.name, errors: validation.errors, }); return false; } } // Create metadata const metadata: ToolMetadata = { tool, registeredAt: new Date(), callCount: 0, avgExecutionTime: 0, errorCount: 0, }; // Register tool this.tools.set(tool.name, metadata); this.totalRegistrations++; // Update category index if (tool.category) { if (!this.categoryIndex.has(tool.category)) { this.categoryIndex.set(tool.category, new Set()); } this.categoryIndex.get(tool.category)!.add(tool.name); } // Update tag index if (tool.tags) { for (const tag of tool.tags) { if (!this.tagIndex.has(tag)) { this.tagIndex.set(tag, new Set()); } this.tagIndex.get(tag)!.add(tool.name); } } const duration = performance.now() - startTime; this.logger.debug('Tool registered', { name: tool.name, category: tool.category, duration: `${duration.toFixed(2)}ms`, }); this.emit('tool:registered', tool.name); return true; } /** * Register multiple tools at once */ registerBatch(tools: MCPTool[], options: ToolRegistrationOptions = {}): { registered: number; failed: string[]; } { const startTime = performance.now(); const failed: string[] = []; let registered = 0; for (const tool of tools) { if (this.register(tool, options)) { registered++; } else { failed.push(tool.name); } } const duration = performance.now() - startTime; this.logger.info('Batch registration complete', { total: tools.length, registered, failed: failed.length, duration: `${duration.toFixed(2)}ms`, }); return { registered, failed }; } /** * Unregister a tool */ unregister(name: string): boolean { const metadata = this.tools.get(name); if (!metadata) { return false; } // Remove from category index if (metadata.tool.category) { const categoryTools = this.categoryIndex.get(metadata.tool.category); categoryTools?.delete(name); if (categoryTools?.size === 0) { this.categoryIndex.delete(metadata.tool.category); } } // Remove from tag index if (metadata.tool.tags) { for (const tag of metadata.tool.tags) { const tagTools = this.tagIndex.get(tag); tagTools?.delete(name); if (tagTools?.size === 0) { this.tagIndex.delete(tag); } } } this.tools.delete(name); this.logger.debug('Tool unregistered', { name }); this.emit('tool:unregistered', name); return true; } /** * Get a tool by name */ getTool(name: string): MCPTool | undefined { this.totalLookups++; return this.tools.get(name)?.tool; } /** * Check if a tool exists */ hasTool(name: string): boolean { return this.tools.has(name); } /** * Get tool count */ getToolCount(): number { return this.tools.size; } /** * Get all tool names */ getToolNames(): string[] { return Array.from(this.tools.keys()); } /** * List all tools with metadata */ listTools(): Array<{ name: string; description: string; category?: string; tags?: string[]; deprecated?: boolean; }> { return Array.from(this.tools.values()).map(({ tool }) => ({ name: tool.name, description: tool.description, category: tool.category, tags: tool.tags, deprecated: tool.deprecated, })); } /** * Search tools by criteria */ search(options: ToolSearchOptions): MCPTool[] { let results: Set<string> | undefined; // Filter by category if (options.category) { const categoryTools = this.categoryIndex.get(options.category); if (!categoryTools) return []; results = new Set(categoryTools); } // Filter by tags (intersection) if (options.tags && options.tags.length > 0) { for (const tag of options.tags) { const tagTools = this.tagIndex.get(tag); if (!tagTools) return []; if (results) { results = new Set([...results].filter((name) => tagTools.has(name))); } else { results = new Set(tagTools); } } } // Get all tools if no filters applied if (!results) { results = new Set(this.tools.keys()); } // Convert to tools and apply additional filters const tools: MCPTool[] = []; for (const name of results) { const metadata = this.tools.get(name); if (!metadata) continue; const tool = metadata.tool; // Filter by deprecated status if (options.deprecated !== undefined && tool.deprecated !== options.deprecated) { continue; } // Filter by cacheable status if (options.cacheable !== undefined && tool.cacheable !== options.cacheable) { continue; } tools.push(tool); } return tools; } /** * Get tools by category */ getByCategory(category: string): MCPTool[] { const toolNames = this.categoryIndex.get(category); if (!toolNames) return []; return Array.from(toolNames) .map((name) => this.tools.get(name)?.tool) .filter((tool): tool is MCPTool => tool !== undefined); } /** * Get tools by tag */ getByTag(tag: string): MCPTool[] { const toolNames = this.tagIndex.get(tag); if (!toolNames) return []; return Array.from(toolNames) .map((name) => this.tools.get(name)?.tool) .filter((tool): tool is MCPTool => tool !== undefined); } /** * Get all categories */ getCategories(): string[] { return Array.from(this.categoryIndex.keys()); } /** * Get all tags */ getTags(): string[] { return Array.from(this.tagIndex.keys()); } /** * Execute a tool */ async execute( name: string, input: Record<string, unknown>, context?: ToolContext ): Promise<ToolCallResult> { const startTime = performance.now(); const metadata = this.tools.get(name); if (!metadata) { return { content: [{ type: 'text', text: `Tool not found: ${name}` }], isError: true, }; } // Build execution context with required sessionId const execContext: ToolContext = { sessionId: context?.sessionId || this.defaultContext?.sessionId || 'default-session', ...this.defaultContext, ...context, }; this.totalExecutions++; metadata.callCount++; metadata.lastCalled = new Date(); try { this.emit('tool:called', { name, input }); const result = await metadata.tool.handler(input, execContext); const duration = performance.now() - startTime; this.updateAverageExecutionTime(metadata, duration); this.logger.debug('Tool executed', { name, duration: `${duration.toFixed(2)}ms`, success: true, }); this.emit('tool:completed', { name, duration, success: true }); // Format result return { content: [{ type: 'text', text: typeof result === 'string' ? result : JSON.stringify(result, null, 2), }], isError: false, }; } catch (error) { const duration = performance.now() - startTime; metadata.errorCount++; this.logger.error('Tool execution failed', { name, error }); this.emit('tool:error', { name, error, duration }); return { content: [{ type: 'text', text: `Error: ${error instanceof Error ? error.message : String(error)}`, }], isError: true, }; } } /** * Set default execution context */ setDefaultContext(context: ToolContext): void { this.defaultContext = context; } /** * Get tool metadata */ getMetadata(name: string): ToolMetadata | undefined { return this.tools.get(name); } /** * Get registry statistics */ getStats(): { totalTools: number; totalCategories: number; totalTags: number; totalRegistrations: number; totalLookups: number; totalExecutions: number; topTools: Array<{ name: string; calls: number }>; } { // Get top 10 most used tools const topTools = Array.from(this.tools.entries()) .map(([name, metadata]) => ({ name, calls: metadata.callCount })) .sort((a, b) => b.calls - a.calls) .slice(0, 10); return { totalTools: this.tools.size, totalCategories: this.categoryIndex.size, totalTags: this.tagIndex.size, totalRegistrations: this.totalRegistrations, totalLookups: this.totalLookups, totalExecutions: this.totalExecutions, topTools, }; } /** * Validate a tool definition */ validateTool(tool: MCPTool): { valid: boolean; errors: string[] } { const errors: string[] = []; if (!tool.name || typeof tool.name !== 'string') { errors.push('Tool name is required and must be a string'); } else if (!/^[a-zA-Z][a-zA-Z0-9_/:-]*$/.test(tool.name)) { errors.push('Tool name must start with a letter and contain only alphanumeric characters, underscores, slashes, colons, and hyphens'); } if (!tool.description || typeof tool.description !== 'string') { errors.push('Tool description is required and must be a string'); } if (!tool.inputSchema || typeof tool.inputSchema !== 'object') { errors.push('Tool inputSchema is required and must be an object'); } else { const schemaErrors = this.validateSchema(tool.inputSchema); errors.push(...schemaErrors); } if (typeof tool.handler !== 'function') { errors.push('Tool handler is required and must be a function'); } return { valid: errors.length === 0, errors, }; } /** * Validate JSON Schema */ private validateSchema(schema: JSONSchema, path = ''): string[] { const errors: string[] = []; if (!schema.type) { errors.push(`${path || 'schema'}: type is required`); } if (schema.type === 'object' && schema.properties) { for (const [key, propSchema] of Object.entries(schema.properties)) { const propPath = path ? `${path}.${key}` : key; errors.push(...this.validateSchema(propSchema, propPath)); } } if (schema.type === 'array' && schema.items) { errors.push(...this.validateSchema(schema.items, `${path}[]`)); } return errors; } /** * Update average execution time */ private updateAverageExecutionTime(metadata: ToolMetadata, duration: number): void { const n = metadata.callCount; metadata.avgExecutionTime = ((metadata.avgExecutionTime * (n - 1)) + duration) / n; } /** * Clear all tools */ clear(): void { this.tools.clear(); this.categoryIndex.clear(); this.tagIndex.clear(); this.logger.info('Tool registry cleared'); this.emit('registry:cleared'); } } /** * Create a tool registry */ export function createToolRegistry(logger: ILogger): ToolRegistry { return new ToolRegistry(logger); } /** * Helper to create a tool definition */ export function defineTool<TInput = Record<string, unknown>, TOutput = unknown>( name: string, description: string, inputSchema: JSONSchema, handler: ToolHandler<TInput, TOutput>, options?: { category?: string; tags?: string[]; version?: string; deprecated?: boolean; cacheable?: boolean; cacheTTL?: number; timeout?: number; } ): MCPTool<TInput, TOutput> { return { name, description, inputSchema, handler, ...options, }; } |