UNPKG

context-optimizer-mcp-server

Version:

Context optimization tools MCP server for AI coding assistants - compatible with GitHub Copilot, Cursor AI, and other MCP-supporting assistants

115 lines 5.18 kB
"use strict"; /** * Deep Research Tool - Conducts comprehensive, in-depth research using Exa.ai's API * Ported from VS Code extension to MCP server */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DeepResearchTool = void 0; const base_1 = require("./base"); const manager_1 = require("../config/manager"); const exa_js_1 = require("exa-js"); class DeepResearchTool extends base_1.BaseMCPTool { name = 'deepResearch'; description = 'Conduct comprehensive, in-depth research using Exa.ai\'s exhaustive analysis capabilities for critical decision-making and complex architectural planning.'; inputSchema = { type: 'object', properties: { topic: { type: 'string', description: 'The research topic or problem you want to investigate comprehensively. Be as detailed as possible about what you want to learn, including technical requirements, architectural considerations, performance needs, security concerns, or strategic implications you want analyzed in depth.' } }, required: ['topic'] }; async execute(args) { try { // Validate input const validationError = this.validateRequiredFields(args, ['topic']); if (validationError) { return this.createErrorResponse(validationError); } const input = args; // Validate topic is not empty if (!input.topic.trim()) { return this.createErrorResponse('Topic cannot be empty'); } // Get configuration const config = manager_1.ConfigurationManager.getConfig(); if (!config.research.exaKey) { return this.createErrorResponse('Exa.ai API key is not configured. Please set the exaKey in your configuration or EXA_KEY environment variable.'); } this.logOperation(`Starting deep research for topic: ${input.topic}`); // Conduct research const result = await this.conductDeepResearch(input.topic, config.research.exaKey); this.logOperation('Deep research completed successfully'); return this.createSuccessResponse(result.result); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); this.logOperation(`Deep research failed: ${errorMessage}`); return this.createErrorResponse(`Research failed: ${errorMessage}`); } } async conductDeepResearch(topic, exaKey) { const client = new exa_js_1.Exa(exaKey); try { const schema = { type: 'object', properties: { result: { type: 'string' } }, required: ['result'], description: 'Schema with just the result in markdown.' }; this.logOperation('Creating Exa deep research task'); const task = await client.research.createTask({ instructions: topic, model: 'exa-research-pro', // Deep research model output: { schema }, }); this.logOperation(`Task created with ID: ${task.id}. Polling for results...`); const result = await this.pollTask(client, task.id); return this.formatResponse(result); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Failed to conduct deep research with Exa.ai.'; throw new Error(`Exa.ai deep research failed: ${errorMessage}`); } } async pollTask(client, taskId) { let attempts = 0; const maxAttempts = 18; // 18 attempts * 10 seconds = 180 seconds timeout (longer for deep research) while (attempts < maxAttempts) { const task = await client.research.getTask(taskId); if (task.status === 'completed') { this.logOperation('Deep research task completed'); return task; } if (task.status === 'failed') { throw new Error('Deep research task failed'); } if (task.status === 'running' || task.status === 'pending') { this.logOperation(`Task status: ${task.status}... (${attempts * 10}s elapsed)`); await new Promise((resolve) => setTimeout(resolve, 10000)); // Wait 10 seconds attempts++; } else { // Unexpected status throw new Error(`Unexpected task status: ${task.status}`); } } // Timeout after 180 seconds (longer for deep research) throw new Error('Deep research task timed out after 180 seconds'); } formatResponse(result) { if (!result?.data?.result) { throw new Error('Malformed response from Exa.ai - missing result data'); } return { result: result.data.result, raw: result, }; } } exports.DeepResearchTool = DeepResearchTool; //# sourceMappingURL=deepResearch.js.map