SuperAPI AI Directory

opa-mcp-server

OPA and Rego policy toolkit wrapping the OPA CLI and Regal: format, lint, evaluate, test and benchmark policies, manage the OPA REST API and explain decisions.

Open website

About this tool

Purpose

opa-mcp-server is a Model Context Protocol (MCP) server that wraps Open Policy Agent (OPA) and Rego into a collection of tools that can be called by MCP clients. It is aimed at developers who need to write, review, debug, and deploy Rego policies, enabling AI clients (Claude Desktop, Claude Code, Cursor, VS Code, Windsurf, Zed, etc.) to operate OPA in a structured way without parsing free-form CLI output. The current version is v0.9.0, and the tool interfaces, error codes, and environment variables have followed SemVer since v0.1.0.

Main capabilities

The server provides tools organized by category:

  • Authoring and static analysis: generate, format, and refactor policies, run the real opa fmt, opa parse, opa check, opa inspect, opa capabilities, opa deps, and Regal checks (rego_lint), with output consistent with the command line.
  • Evaluation and testing: run queries against policies and input documents (rego_eval), support options such as --explain, --profile, and --coverage, and return execution traces, hot rules, and line-by-line coverage; run opa test and opa bench.
  • Decision explanation: rego_explain_decision presents the trace as readable text lines, helping answer "why was this denied".
  • Bundle operations: use opa build, opa sign, and opa build --verification-key to package, sign, and verify deployable bundles.
  • OPA server management: list, get, put, and delete policies on a running OPA server via the REST API, with bearer-token authentication available.
  • High-level helper tools: rego_generate_test_skeleton, rego_describe_policy, rego_suggest_fix, etc., combining low-level primitives into higher-level tasks.
  • conftest tools: run Rego policy tests against Kubernetes manifests, Terraform plans, Dockerfiles, Helm charts, and YAML/JSON/HCL/TOML/INI.
  • Built-in knowledge and resources: includes a catalog of OPA built-in functions, the official Rego style guide, and a curated pattern library covering scenarios such as RBAC, ABAC, Kubernetes admission, IaC gates, API authorization, and rate limiting. The server also provides three MCP prompts and three MCP resources.

Usage

The server runs locally over stdio; choose the installation path according to your client:

  • Claude Desktop: edit claude_desktop_config.json directly, or copy examples/claude-desktop.json; you can also download opa-mcp.mcpb from the latest release and double-click to install, or use a one-line Smithery command.
  • Claude Code (CLI): use claude mcp add to register it for the current project, which writes to .mcp.json in the project root; add --scope user to register globally. You can copy examples/CLAUDE.md to the repository root or .claude/CLAUDE.md for persistent context, and merge the hooks block from examples/claude-code-hook.json into .claude/settings.json to automatically run opa check after writing or editing .rego files.
  • Cursor: place examples/cursor.json in .cursor/mcp.json (project level) or ~/.cursor/mcp.json (user level).
  • VS Code (GitHub Copilot Chat): place examples/vscode.json in .vscode/mcp.json, or paste the servers block under mcp.servers in the user settings.json.
  • Other clients: refer to the configurations in examples/, or install manually and point the client at the opa-mcp binary. The Docker image is multi-architecture (linux/amd64, linux/arm64), with a pinned versionof opa and regal built in, runs as a non-root user, and requires no OPA or Regal installation on the host.

Configuration and error handling

Configuration is done through environment variables, all optional, with defaults suitable for local OPA (http://localhost:8181)。OPA_MCP_ALLOWED_PATHS The paths in must be absolute paths; *_BINARY values can be a command name on PATH or an absolute path, otherwise the server stops at startup. Each tool returns a JSON envelope with stable error codes, including INVALID_INPUT, INVALID_REGO, INVALID_BUNDLE, EVAL_ERROR, OPA_BINARY_NOT_FOUND, REGAL_NOT_FOUND, CONFTEST_NOT_FOUND, OPA_UNREACHABLE, OPA_AUTH_FAILED, POLICY_NOT_FOUND, DATA_NOT_FOUND, PATH_NOT_ALLOWED, PATH_NOT_FOUND, NO_TESTS_FOUND, COVERAGE_BELOW_THRESHOLD, OPA_VERSION_UNSUPPORTED, GITHUB_TOKEN_MISSING, GIST_CREATE_FAILED, OUTPUT_TOO_LARGE, SUBPROCESS_KILLED, OPA_URL_INVALID, TIMEOUT, CANCELLED, UNKNOWN_ERROR, etc. rego_eval batch runs may also return NOT_EVALUATED.

Use Cases

Suitable for writing, formatting, reviewing, testing, and debugging Rego in policy repositories; managing policies on local or production OPA servers; building and signing deployment bundles; using conftest to validate policies for Kubernetes, Terraform, Dockerfile, Helm, and other configurations; and enabling AI clients to answer typical tasks such as "why was this denied," "help me write a policy," and "push the policy to staging."

Limitations and Security

This server is designed to run locally over stdio for the user and should not be exposed to the network. File-based tools refuse to read paths outside OPA_MCP_ALLOWED_PATHS; if this variable is not set, PATH_NOT_ALLOWED is returned. Subprocesses run with shell: false, with hard timeouts, SIGTERM followed by escalation to SIGKILL, and byte limits on output streams. Subprocesses do not inherit the server environment; instead, via lib/child-env.ts an explicit allowlist is built, but that list includes HTTP_PROXY and its counterparts (proxy URLs may embed usernames and passwords), which can be excluded via OPA_MCP_BLOCK_ENV. stdout is the protocol channel; the server logs only through log files and does not write to stdout. No tool handler throws exceptions; all return structured error envelopes; if arguments do not satisfy the input schema, the MCP layer rejects them and returns a result with isError: true.

Versions and Dependencies

Starting with 0.7.0, Node.js 22 or later is required, the bundled OPA is 1.21, reads according to the YAML 1.2 schema, and bare yes, no, on, and off in data files are treated as strings; if you provide your own binary via OPA_BINARY or PATH, only the Node version requirement applies. Starting with 0.3.0, the bundled OPA is 1.19, Rego v0 policies are no longer parsed, and you can convert them with rego_migrate_v1 or pass v0Compatible: true. The npm package's bundled opa covers five platforms, MCPB does not include opa and regal, and the Docker image includes regal but not conftest. If any tool call returns OPA_BINARY_NOT_FOUND, set the absolute paths for OPA_BINARY and REGAL_BINARY in the client configuration. For more detailed upgrade instructions and troubleshooting, consult the repository documentation; information not provided in this introduction is not yet available.