Skip to main content
Glama
OrygnsCode

opa-mcp-server

Generate Rego test skeleton

rego_generate_test_skeleton

Generates a *_test.rego skeleton from a Rego policy, creating one stub test per rule for easier unit test writing.

Instructions

Generate a *_test.rego skeleton from a policy. Parses the AST, finds each rule, and emits one stub test per rule. The agent fills in realistic inputs and assertions.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sourceYesRego source to generate tests for.

Implementation Reference

  • Handler function: registers the tool with MCP server; parses Rego source via 'opa parse', extracts package name and rule names from AST, generates a test skeleton file with one stub test per rule.
    export function registerRegoGenerateTestSkeleton(server: McpServer, config: Config): void {
      const opa = new OpaCli(config);
    
      server.registerTool(
        'rego_generate_test_skeleton',
        {
          title: 'Generate Rego test skeleton',
          description:
            'Generate a `*_test.rego` skeleton from a policy. Parses the AST, finds each rule, and emits one stub test per rule. The agent fills in realistic inputs and assertions.',
          inputSchema: RegoGenerateTestSkeletonInput,
        },
        async ({ source }) => {
          return withToolEnvelope<RegoGenerateTestSkeletonOutput>(config, async () => {
            const result = await opa.parse({ source });
            const subprocessFailure = mapSubprocessFailure(result, 'opa');
            if (subprocessFailure) return subprocessFailure;
            if (result.exitCode !== 0) {
              return err('INVALID_REGO', 'opa parse rejected the source.', {
                details: { stderr: result.stderr.trim() },
              });
            }
    
            const ast = tryParseJson<ParsedAst>(result.stdout);
            if (ast === undefined) {
              return err('UNKNOWN_ERROR', 'opa parse produced no parseable JSON.');
            }
    
            const packageName = packageNameFromAst(ast);
            const ruleNames = Array.from(
              new Set(
                (ast.rules ?? [])
                  .map(ruleNameFromAst)
                  .filter((n): n is string => typeof n === 'string' && n.length > 0),
              ),
            );
    
            if (ruleNames.length === 0) {
              return err('INVALID_INPUT', 'No rules found in the source — nothing to test.');
            }
    
            const testFile = makeSkeleton(packageName, ruleNames);
            return ok<RegoGenerateTestSkeletonOutput>({ testFile, ruleNames });
          });
        },
      );
    }
  • Input schema: single 'source' string parameter (Zod schema) validated as non-empty string.
    const RegoGenerateTestSkeletonInput = {
      source: z.string().min(1).describe('Rego source to generate tests for.'),
    };
  • Output type: contains 'testFile' (the generated test Rego code) and 'ruleNames' (list of extracted rule names).
    export interface RegoGenerateTestSkeletonOutput {
      testFile: string;
      ruleNames: string[];
    }
  • Registration in helper tools category: calls registerRegoGenerateTestSkeleton as part of registerHelperTools.
    export function registerHelperTools(server: McpServer, config: Config): void {
      registerRegoExplainDecision(server, config);
      registerRegoGenerateTestSkeleton(server, config);
      registerRegoDescribePolicy(server, config);
      registerRegoSuggestFix(server, config);
    }
  • Top-level registration: registerHelperTools is called from the main registerTools entry point.
    export function registerTools(server: McpServer, config: Config): void {
      registerAuthoringTools(server, config);
      registerEvaluationTools(server, config);
      registerBundleTools(server, config);
      registerServerManagementTools(server, config);
      registerHelperTools(server, config);
    }
Behavior4/5

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

With no annotations, the description fully explains the tool's behavior: parsing AST, finding rules, emitting stubs. No mention of side effects like file overwriting or error handling, but the core behavior is transparent.

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?

Two sentences efficiently convey the tool's purpose and method. No filler, every word contributes. Front-loaded with the key action.

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?

For a tool with one parameter, no output schema, and no annotations, the description is suitably complete. It explains what the tool does and how it works (AST parsing), sufficient for an agent to use it correctly.

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 a single 'source' parameter description. The description adds minimal extra meaning beyond the schema, but the phrase 'Rego source to generate tests for' reinforces the purpose. Baseline score of 3 is appropriate.

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 the tool generates a test skeleton from Rego policy, specifying the output format (*_test.rego), the process (parses AST, finds rules, emits stubs), and the agent's role (fills in inputs/assertions). It distinguishes itself from sibling tools like rego_test, which runs tests.

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 implicitly tells when to use (when you need a test skeleton) but lacks explicit guidance on when not to use or alternatives. The context is clear enough for basic usage.

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