@skyramp/mcp
Version:
Skyramp MCP (Model Context Protocol) Server - AI-powered test generation and execution
147 lines (133 loc) • 6.3 kB
JavaScript
import { z } from "zod";
import { getTestRecommendationPrompt } from "../../prompts/test-recommendation/test-recommendation-prompt.js";
import { StateManager, } from "../../utils/AnalysisStateManager.js";
import { logger } from "../../utils/logger.js";
import { AnalyticsService } from "../../services/AnalyticsService.js";
/**
* Recommend Tests Tool
* MCP tool for generating actionable test recommendations
*/
const recommendTestsSchema = z.object({
stateFile: z
.string()
.describe("Path to state file from skyramp_map_tests (contains both analysis and mapping results)"),
topN: z
.number()
.default(7)
.describe("Number of top test types to recommend (default: 5)"),
minScore: z
.number()
.default(60)
.describe("Minimum score threshold for recommendations (default: 60)"),
});
const TOOL_NAME = "skyramp_recommend_tests";
export function registerRecommendTestsTool(server) {
server.registerTool(TOOL_NAME, {
description: `Generate actionable test recommendations with ready-to-use generation prompts.
**PREREQUISITE**: Call skyramp_map_tests first to get the stateFile path.
This tool reads the saved test mapping results and generates:
- Ordered list of recommended test types (high/medium/low priority)
- Specific test scenarios based on actual repository endpoints and flows
- Artifact availability status and guidance for missing artifacts
- Step-by-step next actions
SAMPLE OUTPUT:
\`\`\`
Recommended Tests (Prioritized)
Priority: <PRIORITY HIGH, MEDIUM, OR LOW>
1. <TEST TYPE> Tests [**CRITICAL: NEVER MENTION THE SCORE HERE IN THE OUTPUT**]
Rationale:
Specific Tests to Create:
Test 1: <TEST NAME>
Description: <TEST DESCRIPTION>
Target Flow: <TARGET FLOW>
\`\`\`
For each recommended test type, you'll get:
- 2-3 specific tests you can create right now
- Required vs. available artifacts. THIS SHOULD NOT CHANGE PRIORITIZATION OF THE TESTS.
- Guidance for creating missing artifacts WITHOUT PROVIDING ANY CLI COMMANDS.
**CRITICAL RULES**:
- THE PRIORITY SHOULD ONLY BE DEFINED AS HIGH, MEDIUM, OR LOW NOTHING ELSE.
- DO NOT SHOW ANY PRIORITY BREAKDOWN IN THE OUTPUT.
- DON'T MARK ANY TEST BLOCKED EVEN IF REQUIRED ARTIFACTS ARE MISSING.
Output: TestRecommendation with prioritized, actionable test recommendations.
**CRITICAL:** At the end of the tool execution, MUST display the below message:
** This tool is currently in Early Preview stage. Please verify the results. **`,
inputSchema: recommendTestsSchema.shape,
}, async (params) => {
let errorResult;
try {
logger.info("Recommend tests tool invoked", {
stateFile: params.stateFile,
topN: params.topN,
minScore: params.minScore,
});
// Read state file using StateManager
if (!params.stateFile) {
throw new Error("stateFile is required");
}
const stateManager = StateManager.fromStatePath(params.stateFile);
if (!stateManager.exists()) {
throw new Error(`State file not found: ${params.stateFile}. Run skyramp_map_tests first.`);
}
const stateData = await stateManager.readData();
if (!stateData) {
throw new Error(`Failed to read state file: ${params.stateFile}. Run skyramp_map_tests first.`);
}
const { analysis, mapping, repositoryPath } = stateData;
if (!analysis || !mapping) {
throw new Error("State file is missing required data (analysis or mapping)");
}
// Generate the prompt for LLM to create dynamic recommendations
const prompt = getTestRecommendationPrompt(mapping, analysis, params.topN);
// Return prompt for LLM to execute
return {
content: [
{
type: "text",
text: `
**State File**: \`${params.stateFile}\`
**Repository**: \`${repositoryPath}\`
Please generate actionable test recommendations based on the priority scores and repository analysis.
${prompt}
**Important Guidelines:**
1. **NO SCORES IN OUTPUT**: The numeric scores are for internal ranking only. DO NOT include "score" field in JSON and DO NOT mention numeric scores in any text (rationale, description, etc.). Use ONLY priority levels: "high", "medium", "low".
2. Include guidance for missing artifacts with specific Skyramp tool commands
3. Prioritize quick wins (tests with all artifacts available)
4. Use actual endpoint paths and file paths from the repository analysis
5. CRITICAL: Return the recommendations in text format, not JSON.
PRE-REQUISITES:
STEP 1: Call skyramp_analyze_repository to get repository analysis
STEP 2: Call skyramp_map_tests to get test priority scores and repository analysis
**MANDATORY RULES**:
- THE PRIORITY SHOULD ONLY BE DEFINED AS HIGH, MEDIUM, OR LOW NOTHING ELSE.
- DO NOT SHOW ANY PRIORITY BREAKDOWN IN THE OUTPUT.
- DON'T MARK ANY TEST BLOCKED EVEN IF REQUIRED ARTIFACTS ARE MISSING.
- DO NOT SHOW RESULTS IN .MD OR .JSON OR ANY OTHER FILE FORMAT.
- DO NOT CREATE OR SAVE FILES - output everything in your response text.
After analyzing the data above, return the complete JSON response following the structure defined in the prompt.
**CRITICAL:** At the end of the tool execution, MUST display the below message:
** This tool is currently in Early Preview stage. Please verify the results. **`,
},
],
isError: false,
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.error("Recommend tests tool failed", { error: errorMessage });
errorResult = {
content: [
{
type: "text",
text: `Error generating recommendations: ${errorMessage}`,
},
],
isError: true,
};
return errorResult;
}
finally {
AnalyticsService.pushMCPToolEvent(TOOL_NAME, errorResult, {});
}
});
}