UNPKG

jezweb-mcp-core

Version:

Jezweb Model Context Protocol (MCP) Core - A universal server for providing AI tools and resources, designed for seamless integration with various AI models and clients. Features adaptable multi-provider support, comprehensive tool and resource management

157 lines 6.16 kB
/** * Base MCP Handler - Consolidated handler for all deployment targets * * This class provides a unified MCP protocol implementation that can be used * across different deployment targets (Cloudflare Workers, NPM package, local dev). * It eliminates code duplication while preserving deployment-specific optimizations. * * Key Features: * - Single source of truth for MCP protocol handling * - Transport-agnostic design (HTTP, Stdio, etc.) * - Shared tool registry with performance optimizations * - Consistent error handling across deployments * - Resource management support * - Deployment-specific adapter pattern */ import { MCPRequest, MCPResponse, MCPInitializeRequest, MCPInitializeResponse, MCPToolsListRequest, MCPToolsListResponse, MCPToolsCallRequest, MCPToolsCallResponse, MCPResourcesListRequest, MCPResourcesListResponse, MCPResourcesReadRequest, MCPResourcesReadResponse, MCPPromptsListRequest, MCPPromptsListResponse, MCPPromptsGetRequest, MCPPromptsGetResponse, MCPCompletionRequest, MCPCompletionResponse } from '../types/index.js'; import { ProviderRegistry } from '../types/generic-types.js'; import { ToolRegistry } from './tool-registry.js'; /** * Simple configuration interface for the base MCP handler */ export interface SimpleMCPHandlerConfig { /** OpenAI API key (for backward compatibility) */ apiKey?: string; /** Server name for identification */ serverName?: string; /** Server version */ serverVersion?: string; /** Enable debug logging */ debug?: boolean; /** Custom capabilities */ capabilities?: { tools?: { listChanged?: boolean; }; resources?: { subscribe?: boolean; listChanged?: boolean; }; prompts?: { listChanged?: boolean; }; completions?: {}; }; } import { TransportAdapter } from './transport-adapters.js'; /** * Base MCP Handler class that consolidates all deployment targets * * This class implements the core MCP protocol logic and can be extended * or adapted for different deployment environments through the adapter pattern. */ export declare class BaseMCPHandler { protected providerRegistry: ProviderRegistry; protected toolRegistry: ToolRegistry; protected promptHandlers: Record<string, any>; protected completionHandlers: Record<string, any>; protected config: SimpleMCPHandlerConfig & { serverName: string; serverVersion: string; debug: boolean; capabilities: NonNullable<SimpleMCPHandlerConfig['capabilities']>; }; protected transportAdapter?: TransportAdapter; protected isInitialized: boolean; constructor(config: SimpleMCPHandlerConfig, providerRegistryOrTransportAdapter?: ProviderRegistry | TransportAdapter, transportAdapter?: TransportAdapter); /** * Create a backward compatibility provider registry * This is used when the old constructor signature is used */ private createBackwardCompatibilityRegistry; /** * Initialize the handler system with performance optimizations */ private initializeHandlerSystem; /** * Initialize the prompt handlers system */ private initializePromptHandlers; /** * Initialize the completion handlers system */ private initializeCompletionHandlers; /** * Main request handler - entry point for all MCP requests */ handleRequest(request: MCPRequest): Promise<MCPResponse>; /** * Handle initialize requests */ protected handleInitialize(request: MCPInitializeRequest): Promise<MCPInitializeResponse>; /** * Handle tools list requests - returns all tools (no pagination needed) */ protected handleToolsList(request: MCPToolsListRequest): Promise<MCPToolsListResponse>; /** * Handle tools call requests with optimized registry usage */ protected handleToolsCall(request: MCPToolsCallRequest): Promise<MCPToolsCallResponse>; /** * Handle resources list requests with pagination support */ protected handleResourcesList(request: MCPResourcesListRequest): Promise<MCPResourcesListResponse>; /** * Handle resources read requests */ protected handleResourcesRead(request: MCPResourcesReadRequest): Promise<MCPResourcesReadResponse>; /** * Handle prompts list requests */ protected handlePromptsList(request: MCPPromptsListRequest): Promise<MCPPromptsListResponse>; /** * Handle prompts get requests */ protected handlePromptsGet(request: MCPPromptsGetRequest): Promise<MCPPromptsGetResponse>; /** * Handle completion requests */ protected handleCompletion(request: MCPCompletionRequest): Promise<MCPCompletionResponse>; /** * Update registry context without recreating the entire registry * This is a performance optimization to avoid the registry recreation issue */ private updateRegistryContext; /** * Centralized error handling with transport adapter support and JSON-RPC 2.0 compliance */ protected handleError(error: any, requestId: string | number | null): MCPResponse; /** * Update API key and reinitialize services * Note: This method now works with the provider registry */ updateApiKey(apiKey: string): void; /** * Helper method to extract OpenAIService from provider for backward compatibility * This is a bridge method to maintain compatibility with existing tool handlers */ private getOpenAIServiceFromProvider; /** * Get registry statistics for debugging */ getRegistryStats(): import("./tool-registry.js").ToolRegistryStats; /** * Check if handler is initialized */ getIsInitialized(): boolean; /** * Create a fallback provider wrapper for backward compatibility * This wraps an OpenAIService in a minimal LLMProvider interface */ private createFallbackProvider; /** * Debug logging with feature flag support */ protected log(message: string, ...args: any[]): void; } //# sourceMappingURL=base-mcp-handler.d.ts.map