Skip to main content
Glama
OrygnsCode

opa-mcp-server

Benchmark Rego query

rego_bench

Benchmark a Rego query against policy and input to obtain timing statistics, identifying slow rules.

Instructions

Benchmark a Rego query against a policy + input with opa bench. Returns statistical timing data: iterations, ns/op, and allocation counts. Use this to spot slow rules.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesRego query to benchmark.
pathsNoPolicy / data paths to load. Each must be in an allowed root.
inputNoInline input document.
inputPathNoPath to a JSON input file.
countNoNumber of benchmark iterations. Defaults to OPA's built-in default.

Implementation Reference

  • The main handler function `registerRegoBench` which registers the 'rego_bench' tool and contains the full execution logic: validates input (inline input vs inputPath), resolves paths, calls `opa.bench(...)`, checks subprocess exit code, parses JSON output, and returns results.
    export function registerRegoBench(server: McpServer, config: Config): void {
      const opa = new OpaCli(config);
    
      server.registerTool(
        'rego_bench',
        {
          title: 'Benchmark Rego query',
          description:
            'Benchmark a Rego query against a policy + input with `opa bench`. Returns statistical timing data: iterations, ns/op, and allocation counts. Use this to spot slow rules.',
          inputSchema: RegoBenchInput,
        },
        async ({ query, paths, input, inputPath, count }) => {
          return withToolEnvelope<RegoBenchOutput>(config, async () => {
            if (input !== undefined && inputPath) {
              return err(
                'INVALID_INPUT',
                'rego_bench accepts either `input` or `inputPath`, not both.',
              );
            }
            let resolvedPaths: string[] | undefined;
            if (paths?.length) {
              const validation = validatePaths(paths, config, { mustExist: true });
              if (!validation.ok) return validation.error;
              resolvedPaths = validation.resolved;
            }
            let resolvedInputPath: string | undefined;
            if (inputPath) {
              const validation = validatePaths([inputPath], config, { mustExist: true });
              if (!validation.ok) return validation.error;
              resolvedInputPath = validation.resolved[0];
            }
    
            const result = await opa.bench({
              query,
              paths: resolvedPaths,
              input,
              inputPath: resolvedInputPath,
              count,
            });
            const subprocessFailure = mapSubprocessFailure(result, 'opa');
            if (subprocessFailure) return subprocessFailure;
    
            if (result.exitCode !== 0) {
              return err('EVAL_ERROR', 'opa bench exited with an error.', {
                details: { stderr: result.stderr.trim() },
              });
            }
    
            const parsed = tryParseJson<RegoBenchOutput>(result.stdout);
            if (parsed === undefined) {
              return err('UNKNOWN_ERROR', 'opa bench produced no parseable JSON output.', {
                details: { stdout: result.stdout.trim() },
              });
            }
            return ok<RegoBenchOutput>(parsed);
          });
        },
      );
    }
  • Input schema `RegoBenchInput` defining the Zod schema for the tool's parameters: query (required string), paths (optional string array), input (optional unknown), inputPath (optional string), and count (optional positive integer).
    const RegoBenchInput = {
      query: z.string().min(1).describe('Rego query to benchmark.'),
      paths: z
        .array(z.string())
        .optional()
        .describe('Policy / data paths to load. Each must be in an allowed root.'),
      input: z.unknown().optional().describe('Inline input document.'),
      inputPath: z.string().optional().describe('Path to a JSON input file.'),
      count: z
        .number()
        .int()
        .positive()
        .optional()
        .describe("Number of benchmark iterations. Defaults to OPA's built-in default."),
    };
  • Output interface `RegoBenchOutput` defining the shape of the benchmark results: iterations (optional number), metrics (optional record), and raw (optional unknown).
    export interface RegoBenchOutput {
      iterations?: number;
      metrics?: Record<string, unknown>;
      raw?: unknown;
    }
  • Tool registration: `registerRegoBench` is imported from './bench.js' and called inside `registerEvaluationTools` to register the tool with the MCP server.
    import { registerRegoBench } from './bench.js';
    import { registerRegoCompileQuery } from './compile.js';
    import { registerRegoEval } from './eval.js';
    import { registerRegoTest } from './test.js';
    
    export function registerEvaluationTools(server: McpServer, config: Config): void {
      registerRegoEval(server, config); // registers rego_eval + 3 variants
      registerRegoTest(server, config);
      registerRegoBench(server, config);
  • Top-level registration: `registerEvaluationTools` (which includes rego_bench) is called from the main `registerTools` function.
    import { registerEvaluationTools } from './evaluation/index.js';
    import { registerHelperTools } from './helpers/index.js';
    import { registerServerManagementTools } from './server-management/index.js';
    
    export function registerTools(server: McpServer, config: Config): void {
      registerAuthoringTools(server, config);
      registerEvaluationTools(server, config);
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description must disclose behavior. It mentions the use of 'opa bench' and the returned metrics, but does not discuss resource usage, prerequisites (e.g., OPA server), or potential side effects. This is adequate but lacks depth.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is two sentences, front-loading the core function and ending with a practical usage tip. Every sentence earns its place with no fluff.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the absence of annotations and output schema, the description adequately covers what the tool does and what it returns. It assumes familiarity with OPA benchmark but is sufficient for a specialized tool. Some minor gaps (e.g., how to run) are acceptable.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100% with each parameter described. The description adds context that the tool benchmarks a query against policy and input, and that output includes timing data, but the schema already provides detailed parameter definitions. The added value is moderate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states it benchmarks a Rego query using 'opa bench' and returns statistical timing data (iterations, ns/op, allocation counts). It distinguishes itself from sibling tools like rego_eval and rego_check by focusing on performance measurement.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description explicitly says 'Use this to spot slow rules,' providing a clear use case. While it does not specify when not to use it or name alternatives, the guidance is sufficient for a specialized performance tool.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/OrygnsCode/opa-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server