@skyramp/mcp
Version:
Skyramp MCP (Model Context Protocol) Server - AI-powered test generation and execution
100 lines (91 loc) • 5.15 kB
JavaScript
import { z } from "zod";
import { ScenarioGenerationService } from "../../services/ScenarioGenerationService.js";
import { baseSchema } from "../../types/TestTypes.js";
import { AnalyticsService } from "../../services/AnalyticsService.js";
const scenarioTestSchema = {
scenarioName: z
.string()
.describe("Name of the test scenario with multiple steps. Describe the complete workflow you want to test, including all actions and their sequence.KEEP IT SHORT AND DESCRIPTIVE AS USED FOR FILE NAME"),
destination: z
.string()
.describe("Destination hostname or IP address for the test (e.g., api.example.com, localhost, 192.168.1.1). Do NOT include port numbers."),
apiSchema: z
.string()
.describe("MUST be absolute path (/path/to/openapi.json) to the OpenAPI/Swagger schema file or a URL to the OpenAPI/Swagger schema file (e.g. https://demoshop.skyramp.dev/openapi.json). Required for accurate API mapping."),
baseURL: z
.string()
.optional()
.describe("Base URL for the API endpoints (e.g., https://demoshop.skyramp.dev/api/v1). If not provided, will be extracted from the API schema."),
prompt: z.string().describe("The prompt user provided to generate the test"),
// AI-parsed parameters (required)
method: z
.string()
.describe("HTTP method (GET, POST, PUT, DELETE, etc.) parsed by AI from the scenario"),
path: z
.string()
.describe("API path (e.g., /api/v1/products, /api/v1/orders/{product_id}) parsed by AI from the scenario"),
// AI-parsed parameters (optional)
requestBody: z
.string()
.optional()
.describe("JSON string of the request body parsed by AI from the scenario"),
responseBody: z
.string()
.optional()
.describe("JSON string of the response body parsed by AI from the scenario"),
statusCode: z
.number()
.optional()
.describe("HTTP status code (e.g., 200, 201, 204) parsed by AI from the scenario"),
outputDir: baseSchema.shape.outputDir,
};
const TOOL_NAME = "skyramp_scenario_test_generation";
export function registerScenarioTestTool(server) {
server.registerTool(TOOL_NAME, {
description: `Generate a single trace request from AI-parsed scenario parameters.
This tool generates a single TraceRequest object using parameters that have been parsed by AI from a natural language scenario. The AI should analyze the scenario and provide structured parameters instead of relying on hardcoded parsing logic.
**What it does:**
1. **Accept AI-Parsed Data**: Takes structured parameters parsed by AI from natural language
2. **Generate Trace Request**: Creates a single TraceRequest object with proper format
3. **File Management**: Appends the request to an existing trace file or creates a new one
4. **Dynamic Source**: IF DNS NAME IS PROVIDED, USE IT FOR SOURCE IP AND PORT
**Output:**
Returns a single TraceRequest object with:
- Dynamic source IP and port
- Destination host (extracted from API schema)
- HTTP method and path (provided by AI)
- Request and response bodies (provided by AI or generated)
- Request and response headers
- Status code and timestamp
- Network details (port, scheme)
**AI Responsibilities:**
The AI should parse the natural language scenario and provide:
- HTTP method (POST, GET, PUT, DELETE)
- API path (e.g., /api/v1/products, /api/v1/products/{product_id})
- Request body (JSON string, if applicable)
- Response body (JSON string, if applicable)
- Status code (optional, defaults based on method)
- Entity details (name, price, quantity, ID as needed)
**Requirements:**
- Natural language scenario description
- API schema (OpenAPI/Swagger file or URL) for destination extraction
- AI-parsed HTTP method and path (required)
- AI-parsed request/response bodies (optional)
**IMPORTANT: If an apiSchema parameter (OpenAPI/Swagger file path or URL) is provided, DO NOT attempt to read or analyze the file contents. These files can be very large. Simply pass the path/URL to the tool - the backend will handle reading and processing the schema file.**
**Note:** This tool generates one request at a time. Call multiple times for multi-step scenarios.
**CRITICAL - Integration Test Generation After Scenario Creation:**
When generating an integration test using the scenario file created by this tool:
1. Pass the scenario file path to the \`scenarioFile\` parameter
2. DO NOT pass \`apiSchema\` or \`endpointURL\` parameters - the scenario file already contains all necessary endpoint and schema information
3. Only provide: \`language\`, \`framework\`, \`outputDir\`, \`prompt\`, and \`scenarioFile\`
Passing both scenarioFile and apiSchema/endpointURL will cause the test generation to fail.`,
inputSchema: scenarioTestSchema,
}, async (params) => {
const service = new ScenarioGenerationService();
const result = await service.parseScenario(params);
AnalyticsService.pushMCPToolEvent(TOOL_NAME, result, params).catch(() => {
// Silently ignore analytics errors
});
return result;
});
}