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.05 kB
"use strict"; /** * Research Topic Tool - Conducts quick, focused web research using Exa.ai's API * Ported from VS Code extension to MCP server */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ResearchTopicTool = void 0; const base_1 = require("./base"); const manager_1 = require("../config/manager"); const exa_js_1 = require("exa-js"); class ResearchTopicTool extends base_1.BaseMCPTool { name = 'researchTopic'; description = 'Conduct quick, focused web research on software development topics using Exa.ai\'s powerful research capabilities for current information and practical implementation guidance.'; inputSchema = { type: 'object', properties: { topic: { type: 'string', description: 'The research topic or problem you want to investigate. Be as detailed as possible about what you want to learn, any specific aspects to focus on, timeframes, geographical scope, or particular angles of interest.' } }, 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 quick research for topic: ${input.topic}`); // Conduct research const result = await this.conductQuickResearch(input.topic, config.research.exaKey); this.logOperation('Quick research completed successfully'); return this.createSuccessResponse(result.result); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); this.logOperation(`Research failed: ${errorMessage}`); return this.createErrorResponse(`Research failed: ${errorMessage}`); } } async conductQuickResearch(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 research task'); const task = await client.research.createTask({ instructions: topic, model: 'exa-research', // Quick 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 research with Exa.ai.'; throw new Error(`Exa.ai research failed: ${errorMessage}`); } } async pollTask(client, taskId) { let attempts = 0; const maxAttempts = 12; // 12 attempts * 10 seconds = 120 seconds timeout while (attempts < maxAttempts) { const task = await client.research.getTask(taskId); if (task.status === 'completed') { this.logOperation('Research task completed'); return task; } if (task.status === 'failed') { throw new Error('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 120 seconds throw new Error('Research task timed out after 120 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.ResearchTopicTool = ResearchTopicTool; //# sourceMappingURL=researchTopic.js.map