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
87 lines (83 loc) • 3.85 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.AskFollowUpTool = void 0;
const base_1 = require("./base");
const manager_1 = require("../session/manager");
const factory_1 = require("../providers/factory");
const manager_2 = require("../config/manager");
class AskFollowUpTool extends base_1.BaseMCPTool {
name = 'askFollowUp';
description = 'Ask follow-up questions about the previous terminal command execution without re-running the command. Only available after using runAndExtract tool.';
inputSchema = {
type: 'object',
properties: {
question: {
type: 'string',
description: 'Follow-up question about the previous terminal command execution and its output'
}
},
required: ['question']
};
async execute(args) {
try {
this.logOperation('Follow-up question started', { question: args.question });
// Validate required fields
const fieldError = this.validateRequiredFields(args, ['question']);
if (fieldError) {
return this.createErrorResponse(fieldError);
}
// Load previous terminal session
const session = await manager_1.SessionManager.loadTerminalSession();
if (!session) {
return this.createErrorResponse('No recent terminal execution found. Please use the runAndExtract tool first to execute a command, then you can ask follow-up questions about its output.');
}
// Process follow-up question with LLM
const answer = await this.processFollowUpQuestion(session, args.question);
this.logOperation('Follow-up question completed successfully');
return this.createSuccessResponse(answer);
}
catch (error) {
this.logOperation('Follow-up question failed', { error });
return this.createErrorResponse(`Follow-up question failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
async processFollowUpQuestion(session, question) {
const config = manager_2.ConfigurationManager.getConfig();
const provider = factory_1.LLMProviderFactory.createProvider(config.llm.provider);
const apiKey = this.getApiKey(config.llm.provider, config.llm);
const prompt = this.createFollowUpPrompt(session, question);
const response = await provider.processRequest(prompt, config.llm.model, apiKey);
if (!response.success) {
throw new Error(`LLM processing failed: ${response.error}`);
}
return response.content;
}
createFollowUpPrompt(session, question) {
return `You are answering a follow-up question about a previous terminal command execution.
Previous command: ${session.command}
Working directory: ${session.workingDirectory}
Exit code: ${session.exitCode}
Original extraction request: ${session.extractionPrompt}
Previously extracted information: ${session.extractedInfo}
Follow-up question: ${question}
Context - Original command output:
${session.output}
Instructions:
- Answer the follow-up question based on the command output and context
- Reference the original output when needed
- Be specific and helpful
- If the question cannot be answered from the available information, say so clearly
- Use markdown formatting for better readability
- Be concise but thorough in your response`;
}
getApiKey(provider, llmConfig) {
const keyField = `${provider}Key`;
const key = llmConfig[keyField];
if (!key) {
throw new Error(`API key not configured for provider: ${provider}`);
}
return key;
}
}
exports.AskFollowUpTool = AskFollowUpTool;
//# sourceMappingURL=askFollowUp.js.map
;