import { Tool } from '@modelcontextprotocol/sdk/types.js';
import { executeTargetedAnalysis as executeTargetedCode2Prompt } from '../utils/code2prompt-executor.js';
import { executeTargetedAnalysis as executeTargetedGemini } from '../utils/gemini-executor.js';
export const targetedAnalysisTool: Tool = {
name: 'gemini_targeted_analysis',
description: 'Perform targeted analysis on specific files or folders using Gemini CLI with focused codebase context via code2prompt',
inputSchema: {
type: 'object',
properties: {
workingDir: {
type: 'string',
description: 'The working directory path of the codebase to analyze',
},
targetPaths: {
type: 'array',
items: {
type: 'string',
},
description: 'Array of specific file or folder paths to analyze (relative to workingDir)',
minItems: 1,
},
analysisContext: {
type: 'string',
description: 'Optional context about what specific aspects to focus on in the analysis',
default: '',
},
},
required: ['workingDir', 'targetPaths'],
},
};
export async function handleTargetedAnalysis(args: any) {
try {
const { workingDir, targetPaths, analysisContext = '' } = args;
console.log(`Starting targeted analysis for: ${targetPaths.join(', ')} in ${workingDir}`);
// Validate inputs
if (!Array.isArray(targetPaths) || targetPaths.length === 0) {
throw new Error('targetPaths must be a non-empty array');
}
// Step 1: Execute code2prompt with targeted paths
console.log('Analyzing targeted files with code2prompt...');
const code2promptResult = await executeTargetedCode2Prompt(workingDir, targetPaths);
if (code2promptResult.error) {
throw new Error(`code2prompt execution failed: ${code2promptResult.error}`);
}
if (!code2promptResult.output) {
throw new Error('No targeted content extracted by code2prompt');
}
console.log(`Targeted analysis complete. Token count: ${code2promptResult.tokenCount || 'unknown'}`);
// Step 2: Send to Gemini CLI for targeted analysis
console.log('Sending to Gemini CLI for component analysis...');
const geminiResult = await executeTargetedGemini(
code2promptResult.output,
targetPaths,
workingDir
);
if (geminiResult.error) {
throw new Error(`Gemini CLI execution failed: ${geminiResult.error}`);
}
if (!geminiResult.response) {
throw new Error('No response received from Gemini CLI');
}
console.log('Targeted analysis completed successfully');
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
analysis: {
targetPaths,
tokenCount: code2promptResult.tokenCount,
reviewResponse: geminiResult.response,
workingDirectory: workingDir,
analysisContext,
timestamp: new Date().toISOString(),
}
}, null, 2)
}
]
};
} catch (error) {
console.error('Error in targeted analysis:', error);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: false,
error: error instanceof Error ? error.message : 'Unknown error occurred',
targetPaths: args.targetPaths,
timestamp: new Date().toISOString(),
}, null, 2)
}
],
isError: true
};
}
}