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 | 1x 1x 1x 1x 14x 14x 14x 13x 1x 14x 6x 3x 3x 5x 5x 5x 5x 5x 5x 11x 4x 4x 1x 4x 7x 7x 5x 3x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 4x 3x 14x 9x 9x 1x 8x 8x 8x 8x 6x 6x 2x 2x 2x 1x 1x 1x 1x 1x 7x 7x 14x 7x 7x 1x 6x 6x 4x 3x 3x 5x 5x 5x | import { Server } from "@modelcontextprotocol/sdk/server/index.js"; import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; import { ConnectionManager } from "./services/ConnectionManager"; import { logger } from "./logger"; interface JsonSchemaProperty { type?: string; $ref?: string; properties?: Record<string, JsonSchemaProperty>; items?: JsonSchemaProperty; enum?: unknown[]; oneOf?: JsonSchemaProperty[]; anyOf?: JsonSchemaProperty[]; [key: string]: unknown; } type JsonSchema = JsonSchemaProperty & { $schema?: string; properties?: Record<string, JsonSchemaProperty>; }; export async function setupHandlers(server: Server): Promise<void> { // Initialize connection and detect GitLab instance on startup const connectionManager = ConnectionManager.getInstance(); try { await connectionManager.initialize(); logger.info("Connection initialized during server setup"); } catch (error) { logger.warn( `Initial connection failed during setup, will retry on first tool call: ${error instanceof Error ? error.message : String(error)}` ); // Continue without initialization - tools will handle gracefully on first call } // List tools handler server.setRequestHandler(ListToolsRequestSchema, async () => { // Get tools from registry manager (already filtered) const { RegistryManager } = await import("./registry-manager"); const registryManager = RegistryManager.getInstance(); const tools = registryManager.getAllToolDefinitions(); // Helper function to resolve $ref references in JSON schema function resolveRefs( schema: JsonSchemaProperty | JsonSchemaProperty[], rootSchema?: JsonSchema ): JsonSchemaProperty | JsonSchemaProperty[] { Iif (!schema || typeof schema !== "object") return schema; // Set root schema for reference resolution rootSchema ??= schema as JsonSchema; // Handle arrays Iif (Array.isArray(schema)) { return schema.map(item => resolveRefs(item, rootSchema) as JsonSchemaProperty); } // Handle $ref resolution Iif (schema.$ref && typeof schema.$ref === "string") { const refPath = schema.$ref.replace("#/properties/", ""); const referencedProperty = rootSchema.properties?.[refPath]; if (referencedProperty) { // Resolve the referenced property recursively const resolvedRef = resolveRefs(referencedProperty, rootSchema) as JsonSchemaProperty; // Merge with current properties (excluding $ref) const schemaWithoutRef = { ...schema }; delete schemaWithoutRef.$ref; return { ...resolvedRef, ...schemaWithoutRef }; } // If reference can't be resolved, remove $ref and keep other properties const schemaWithoutRef = { ...schema }; delete schemaWithoutRef.$ref; return schemaWithoutRef; } // Recursively process all object properties const result: JsonSchemaProperty = {}; for (const [key, value] of Object.entries(schema)) { if (key === "properties" && typeof value === "object" && value !== null) { // Special handling for properties object const resolvedProperties: Record<string, JsonSchemaProperty> = {}; for (const [propKey, propValue] of Object.entries( value as Record<string, JsonSchemaProperty> )) { resolvedProperties[propKey] = resolveRefs(propValue, rootSchema) as JsonSchemaProperty; } result[key] = resolvedProperties; } else Iif (typeof value === "object" && value !== null && !Array.isArray(value)) { result[key] = resolveRefs(value as JsonSchemaProperty, rootSchema); } else { result[key] = value; } } return result; } // Remove $schema for Gemini compatibility and ensure proper JSON schema format const modifiedTools = tools.map(tool => { let inputSchema = tool.inputSchema; // Force all input schemas to be type: "object" for MCP compatibility Eif (inputSchema && typeof inputSchema === "object") { inputSchema = { ...inputSchema, type: "object" }; } // Resolve $ref references for MCP agent compatibility Eif (inputSchema && typeof inputSchema === "object") { const resolved = resolveRefs(inputSchema); // Only assign if resolved is an object (not array) Eif (resolved && typeof resolved === "object" && !Array.isArray(resolved)) { inputSchema = resolved; } } // Remove $schema for Gemini compatibility if (inputSchema && typeof inputSchema === "object" && "$schema" in inputSchema) { const cleanedSchema = { ...inputSchema } as Record<string, unknown>; delete cleanedSchema.$schema; inputSchema = cleanedSchema; } return { ...tool, inputSchema }; }); return { tools: modifiedTools, }; }); // Call tool handler server.setRequestHandler(CallToolRequestSchema, async request => { try { if (!request.params.arguments) { throw new Error("Arguments are required"); } logger.info(`Tool called: ${request.params.name}`); // Check if connection is initialized - try to initialize if needed const connectionManager = ConnectionManager.getInstance(); try { // Try to get client first connectionManager.getClient(); const instanceInfo = connectionManager.getInstanceInfo(); logger.info(`Connection verified: ${instanceInfo.version} ${instanceInfo.tier}`); } catch { logger.info("Connection not initialized, attempting to initialize..."); try { await connectionManager.initialize(); connectionManager.getClient(); const instanceInfo = connectionManager.getInstanceInfo(); logger.info(`Connection initialized: ${instanceInfo.version} ${instanceInfo.tier}`); } catch (initError) { logger.error( `Connection initialization failed: ${initError instanceof Error ? initError.message : String(initError)}` ); throw new Error("Bad Request: Server not initialized"); } } // Dynamic tool dispatch using the new registry manager const toolName = request.params.name; try { // Import the registry manager const { RegistryManager } = await import("./registry-manager"); const registryManager = RegistryManager.getInstance(); // Check if tool exists and passes all filtering (applied at registry level) if (!registryManager.hasToolHandler(toolName)) { throw new Error(`Tool '${toolName}' is not available or has been filtered out`); } logger.info(`Executing tool: ${toolName}`); // Execute the tool using the registry manager const result = await registryManager.executeTool(toolName, request.params.arguments); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to execute tool '${toolName}': ${errorMessage}`); } } catch (error) { logger.error( `Error in tool handler: ${error instanceof Error ? error.message : String(error)}` ); const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: JSON.stringify({ error: errorMessage }, null, 2), }, ], isError: true, }; } }); } |