UNPKG

il2cpp-dump-analyzer-mcp

Version:

Agentic RAG system for analyzing IL2CPP dump.cs files from Unity games

167 lines 6.64 kB
"use strict"; /** * Search Code Tool Implementation * Demonstrates the new base handler pattern for eliminating code duplication */ Object.defineProperty(exports, "__esModule", { value: true }); exports.searchCodeSchema = exports.SearchCodeToolHandler = void 0; exports.createSearchCodeTool = createSearchCodeTool; const zod_1 = require("zod"); const base_tool_handler_1 = require("../base-tool-handler"); const parameter_validator_1 = require("../../utils/parameter-validator"); const mcp_response_formatter_1 = require("../../utils/mcp-response-formatter"); /** * Search Code Tool Handler * Provides semantic search through IL2CPP code with filtering capabilities */ class SearchCodeToolHandler extends base_tool_handler_1.BaseSearchToolHandler { constructor(context) { super({ name: 'search_code', description: 'Search through IL2CPP code with semantic search and filtering', enableParameterValidation: true, enableResponseFormatting: true }, context); } /** * Validate search code specific parameters */ async validateParameters(params) { const errors = []; const warnings = []; const adjustedValues = {}; // Validate common parameters first const commonValidation = parameter_validator_1.ParameterValidator.validateCommonParams(params); errors.push(...commonValidation.errors); warnings.push(...commonValidation.warnings); if (commonValidation.adjustedValues) { Object.assign(adjustedValues, commonValidation.adjustedValues); } // Validate query parameter if (!params.query || (typeof params.query === 'string' && params.query.trim().length === 0)) { errors.push('query parameter is required and cannot be empty'); } else { adjustedValues.query = parameter_validator_1.ParameterValidator.validateQuery(params.query); } // Validate filter parameters const filter = this.createSearchFilter(params); const filterValidation = parameter_validator_1.ParameterValidator.validateSearchFilter(filter); errors.push(...filterValidation.errors); warnings.push(...filterValidation.warnings); // Validate top_k parameter if (params.top_k !== undefined) { adjustedValues.top_k = parameter_validator_1.ParameterValidator.validateTopK(params.top_k); } return { isValid: errors.length === 0, errors, warnings, adjustedValues }; } /** * Execute the core search logic */ async executeCore(params) { const query = this.extractQuery(params); const filter = this.createSearchFilter(params); const limit = params.top_k || 5; this.context.logger.debug(`Search filter:`, filter, `top_k: ${limit}`); // Perform the vector search const results = await this.performVectorSearch(query, filter, limit); this.context.logger.debug(`search_code returned ${results.length} results`); return results; } /** * Format the search results with enhanced metadata */ formatResponse(results, warnings = []) { const query = this.extractQuery({}); const filter = this.createSearchFilter({}); // Format the results with enhanced metadata const formattedResults = results.map(doc => ({ content: doc.pageContent, name: doc.metadata.name, namespace: doc.metadata.namespace, fullName: doc.metadata.fullName, type: doc.metadata.type, isMonoBehaviour: doc.metadata.isMonoBehaviour || false, baseClass: doc.metadata.baseClass, interfaces: doc.metadata.interfaces || [] })); const responseData = { results: formattedResults, metadata: { query, appliedFilters: filter, resultCount: results.length, timestamp: new Date().toISOString() } }; let response = mcp_response_formatter_1.MCPResponseFormatter.formatTextResponse(responseData); if (warnings.length > 0) { response = mcp_response_formatter_1.MCPResponseFormatter.addWarnings(response, warnings); } return mcp_response_formatter_1.MCPResponseFormatter.addExecutionTiming(response, this.startTime, this.config.name); } /** * Create search filter from parameters */ createSearchFilter(params) { const filter = {}; if (params.filter_type) filter.type = params.filter_type; if (params.filter_namespace) filter.namespace = params.filter_namespace; if (params.filter_monobehaviour) filter.isMonoBehaviour = true; return filter; } /** * Extract query from parameters */ extractQuery(params) { return parameter_validator_1.ParameterValidator.validateQuery(params.query || ''); } } exports.SearchCodeToolHandler = SearchCodeToolHandler; /** * Zod schema for search code tool parameters */ exports.searchCodeSchema = zod_1.z.object({ query: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).describe("Search query for IL2CPP code"), filter_type: zod_1.z.string().optional().describe("Filter by entity type (class, method, enum, interface)"), filter_namespace: zod_1.z.string().optional().describe("Filter by namespace"), filter_monobehaviour: zod_1.z.boolean().optional().describe("Filter to only MonoBehaviour classes"), top_k: zod_1.z.number().optional().default(5).describe("Number of results to return") }); /** * Factory function to create and register the search code tool */ function createSearchCodeTool(server, context) { const handler = new SearchCodeToolHandler(context); server.tool("search_code", exports.searchCodeSchema, async (params) => { return await handler.execute(params); }); return handler; } /** * Example of how to use the new tool handler pattern: * * // In mcp-sdk-server.ts: * import { createSearchCodeTool } from './tools/search-code-tool.js'; * * // Create tool execution context * const context: ToolExecutionContext = { * vectorStore: vectorStore!, * logger: Logger, * isInitialized: () => isInitialized * }; * * // Register the tool * createSearchCodeTool(server, context); * * This eliminates ~80% of the duplicated code while maintaining all functionality! */ //# sourceMappingURL=search-code-tool.js.map