UNPKG

deepsource-mcp-server

Version:
149 lines (148 loc) 4.44 kB
/** * @fileoverview Tool registry for managing MCP tool definitions and handlers * @packageDocumentation */ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; import { z } from 'zod'; import { HandlerFunction, BaseHandlerDeps } from '../handlers/base/handler.interface.js'; import type { DiscoveryConfig } from '../config/default.js'; /** * Tool metadata for categorization and filtering */ export interface ToolMetadata { /** Tool category for grouping */ category?: string; /** Tool version */ version?: string; /** Tool tags for filtering */ tags?: string[]; /** Whether the tool is enabled by default */ enabled?: boolean; } /** * Tool definition interface */ export interface ToolDefinition<TInput = unknown, TOutput = unknown> { /** Tool name */ name: string; /** Tool description */ description: string; /** Input schema using Zod - can be ZodType or ZodRawShape */ inputSchema?: z.ZodType<TInput> | z.ZodRawShape; /** Output schema using Zod - can be ZodType or ZodRawShape */ outputSchema?: z.ZodType<TOutput> | z.ZodRawShape; /** Handler function */ handler: HandlerFunction<TInput>; /** Optional metadata for enhanced features */ metadata?: ToolMetadata; } /** * Tool discovery options (compatible with DiscoveryConfig) */ export type ToolDiscoveryOptions = Partial<DiscoveryConfig>; /** * Registry for managing MCP tools */ export declare class ToolRegistry { private tools; private toolMetadata; private discoveredTools; private server; private defaultDeps; constructor(server: McpServer, defaultDeps?: BaseHandlerDeps); /** * Registers a tool with the MCP server * @param tool - The tool definition */ registerTool<TInput = unknown, TOutput = unknown>(tool: ToolDefinition<TInput, TOutput>): void; /** * Registers multiple tools at once * @param tools - Array of tool definitions */ registerTools(tools: ToolDefinition[]): void; /** * Gets a registered tool by name * @param name - Tool name * @returns The tool definition or undefined */ getTool(name: string): ToolDefinition | undefined; /** * Gets all registered tools * @returns Array of tool names */ getToolNames(): string[]; /** * Checks if a tool is registered * @param name - Tool name * @returns True if registered */ hasTool(name: string): boolean; /** * Updates the default dependencies * @param deps - New default dependencies */ updateDefaultDeps(deps: BaseHandlerDeps): void; /** * Discovers and loads tools from specified directories (requires FEATURE_TOOL_DISCOVERY) * @param options - Discovery options * @returns Array of discovered tool names */ discoverTools(options?: ToolDiscoveryOptions): Promise<string[]>; /** * Scans a directory for tool files (internal) */ private scanDirectory; /** * Checks if a filename matches any of the patterns */ private static matchesPattern; /** * Loads a tool from a file */ private loadToolFromFile; /** * Checks if a tool passes the configured filters */ private static passesFilters; /** * Gets tool metadata * @param name - Tool name * @returns Tool metadata or undefined */ getToolMetadata(name: string): ToolMetadata | undefined; /** * Gets tools by category (requires metadata) * @param category - Category to filter by * @returns Array of tool names in the category */ getToolsByCategory(category: string): string[]; /** * Gets tools by tag (requires metadata) * @param tag - Tag to filter by * @returns Array of tool names with the tag */ getToolsByTag(tag: string): string[]; /** * Gets all tool categories * @returns Array of unique categories */ getCategories(): string[]; /** * Gets all tool tags * @returns Array of unique tags */ getTags(): string[]; /** * Gets enhanced tool information including metadata * @returns Array of tool information objects */ getToolsInfo(): Array<{ name: string; description: string; category?: string; version?: string; tags?: string[]; enabled?: boolean; discovered?: boolean; }>; }