|
| 1 | +import { mkdir, writeFile } from "node:fs/promises"; |
| 2 | + |
| 3 | +import { Command, Option } from "commander"; |
| 4 | +import dotenv from "dotenv"; |
| 5 | + |
| 6 | +import { MCPClient } from "./mcp_client.js"; |
| 7 | +import { PROMPTS } from "./prompts.js"; |
| 8 | +import { renderReport, saveReport } from "./report.js"; |
| 9 | + |
| 10 | +dotenv.config(); |
| 11 | + |
| 12 | +const program = new Command(); |
| 13 | + |
| 14 | +program |
| 15 | + .name("mcp-eval") |
| 16 | + .description("mcp evaluation tool from opslevel") |
| 17 | + .version("0.0.1") |
| 18 | + .addOption( |
| 19 | + new Option("--debug", "Enable debug logging").env("DEBUG").default(false), |
| 20 | + ) |
| 21 | + .addOption( |
| 22 | + new Option("--anthropic-api-key <key>", "Anthropic API key") |
| 23 | + .env("ANTHROPIC_API_KEY") |
| 24 | + .makeOptionMandatory(true), |
| 25 | + ) |
| 26 | + .addOption( |
| 27 | + new Option("--anthropic-model <model>", "Anthropic model") |
| 28 | + .env("ANTHROPIC_MODEL") |
| 29 | + .default("claude-3-7-sonnet-20250219"), |
| 30 | + ) |
| 31 | + .addOption( |
| 32 | + new Option("--mcp-server-path <path>", "Path to MCP server binary") |
| 33 | + .env("MCP_SERVER_PATH") |
| 34 | + .makeOptionMandatory(true), |
| 35 | + ) |
| 36 | + .addOption( |
| 37 | + new Option("--opslevel-api-token <token>", "OpsLevel API token") |
| 38 | + .env("OPSLEVEL_API_TOKEN") |
| 39 | + .makeOptionMandatory(true), |
| 40 | + ) |
| 41 | + .addOption( |
| 42 | + new Option("--opslevel-app-url <url>", "OpsLevel app URL for local development or self-hosted") |
| 43 | + .env("OPSLEVEL_APP_URL") |
| 44 | + ) |
| 45 | + .addOption( |
| 46 | + new Option("--slugs <slugs...>", "Prompt Slugs to run, defaults to all"), |
| 47 | + ); |
| 48 | + |
| 49 | +const saveResult = async (folder, fileName, data) => { |
| 50 | + const filePath = `${folder}/${fileName}.json`; |
| 51 | + await writeFile(filePath, JSON.stringify(data, null, 2)); |
| 52 | + console.log("Result saved to:", filePath); |
| 53 | +}; |
| 54 | + |
| 55 | +const packageResult = (prompt, messages, version, model_config) => { |
| 56 | + // maybe we can extract tool calls and results |
| 57 | + return { |
| 58 | + prompt: prompt, |
| 59 | + response: messages[messages.length - 1]["content"][0]["text"], |
| 60 | + raw_messages: messages, |
| 61 | + run_at: new Date().toISOString(), |
| 62 | + ops_level_mcp_version: version, |
| 63 | + model_config: model_config, |
| 64 | + }; |
| 65 | +}; |
| 66 | + |
| 67 | +async function main() { |
| 68 | + console.log("Starting MCP eval client"); |
| 69 | + |
| 70 | + program.parse(process.argv); |
| 71 | + const { |
| 72 | + anthropicApiKey, |
| 73 | + anthropicModel, |
| 74 | + mcpServerPath, |
| 75 | + opslevelApiToken, |
| 76 | + opslevelAppUrl, |
| 77 | + debug, |
| 78 | + slugs, |
| 79 | + } = program.opts(); |
| 80 | + |
| 81 | + const mcpClient = new MCPClient(anthropicApiKey, anthropicModel, debug); |
| 82 | + const results = []; |
| 83 | + const now = new Date(); |
| 84 | + |
| 85 | + // easiest way I can think of to get yyyy-mm-dd-hh-mm |
| 86 | + const folder = `results/${now.toISOString().slice(0, 16)}` |
| 87 | + .replace(/:/g, "-") |
| 88 | + .replace("T", "_"); |
| 89 | + await mkdir(folder, { recursive: true }); |
| 90 | + console.log("Saving results to folder:", folder); |
| 91 | + |
| 92 | + try { |
| 93 | + await mcpClient.connectToServer(mcpServerPath, [], { |
| 94 | + OPSLEVEL_API_TOKEN: opslevelApiToken, |
| 95 | + OPSLEVEL_APP_URL: opslevelAppUrl, |
| 96 | + }); |
| 97 | + |
| 98 | + const version = await mcpClient.mcp.getServerVersion()["version"]; |
| 99 | + |
| 100 | + // Filter prompts based on slugs if provided |
| 101 | + const filteredPrompts = slugs |
| 102 | + ? PROMPTS.filter((prompt) => slugs.includes(prompt.slug)) |
| 103 | + : PROMPTS; |
| 104 | + |
| 105 | + for (const prompt of filteredPrompts) { |
| 106 | + console.log("Prompt: ", prompt.query); |
| 107 | + const messages = await mcpClient.processQuery(prompt.query); |
| 108 | + const result = packageResult( |
| 109 | + prompt, |
| 110 | + messages, |
| 111 | + version, |
| 112 | + mcpClient.modelConfig(), |
| 113 | + ); |
| 114 | + saveResult(folder, prompt.slug, result); |
| 115 | + results.push(result); |
| 116 | + } |
| 117 | + } finally { |
| 118 | + await mcpClient.cleanup(); |
| 119 | + } |
| 120 | + |
| 121 | + const report = renderReport(results); |
| 122 | + saveReport(folder, report); |
| 123 | +} |
| 124 | + |
| 125 | +main(); |
0 commit comments