Skip to main content
Glama

Compile (partially evaluate) a query on OPA

opa_compile_query
Read-onlyIdempotent

Compile Rego queries or translate OPA policy rules into residual queries and target data filters such as SQL WHERE clauses, UCAST trees, or Lucene queries.

Instructions

Send a query to the OPA server's /v1/compile endpoint for partial evaluation. Returns the residual query -- what remains after substituting in everything that's known. With target, it instead translates the rule at path into a data filter: a SQL WHERE clause (postgresql, mysql, sqlserver, sqlite), a UCAST tree (for Prisma, LINQ or your own code), or a Lucene query for Solr, through /v1/compile/{path}. The rule must be loaded on the server, and its unknowns, usually the rows searched, come from unknowns or a compile METADATA annotation. A SQL query of "", a UCAST query of {} or a Lucene query of : means every row matches; matchesNothing means none does, or no rule exists at path, which OPA answers the same way. A policy the target cannot express returns EVAL_ERROR with each expression at fault in details.errors; a target the server is too old for returns OPA_VERSION_UNSUPPORTED (Lucene needs OPA 1.23).

Tool call template

This tool declares no required parameters. Add optional parameters from the input schema as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {},
    "name": "opa_compile_query"
  }
}

Input Schema

NameRequiredDescriptionDefault
pathNoWith `target`: the filter rule under `data.`, e.g. "filters/include" or "filters.include", or its segments as an array.
inputNoThe known part of the input. OPA does not use a value at or under an unknown, so with the default `unknowns` of ["input"] an input is refused: name the unknown parts instead, e.g. ["input.region"].
queryNoRego query to compile, e.g. "data.rbac.allow == true". Required without `target`; not used with it.
targetNoTranslate the rule at `path` into a data filter of this kind. `multitarget` returns several at once, one per entry in `dialects`.
dialectsNoWith `target: "multitarget"`: the filters to return. Lucene is not one.
maskRuleNoWith `target`: the rule that produces column masks, e.g. "data.filters.masks". Overrides a `mask_rule` annotation.
unknownsNoRefs to treat as unknown. Default ["input"] for a query; with `target`, none beyond what the policy declares, e.g. ["input.fruits"] for the table searched.
tableMappingsNoWith a SQL target: table and column names to write instead of the ones in the policy, per dialect, e.g. {"postgresql": {"fruits": {"$self": "f", "colour": "col"}}}.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed9 schema fields changedv0.12.0
    • addedInput schema / properties / dialects
      Added value: +{
      +  "description": "With `target: \"multitarget\"`: the filters to return. Lucene is not one.",
      +  "items": {
      +    "enum": [
      +      "sql+postgresql",
      +      "sql+mysql",
      +      "sql+sqlserver",
      +      "sql+sqlite",
      +      "ucast+all",
      +      "ucast+minimal",
      +      "ucast+prisma",
      +      "ucast+linq"
      +    ],
      +    "type": "string"
      +  },
      +  "minItems": 1,
      +  "type": "array"
      +}
    • changedInput schema / properties / input / description
      Previous value: -"Optional partial input document."New value: +"The known part of the input. OPA does not use a value at or under an unknown, so with the default `unknowns` of [\"input\"] an input is refused: name the unknown parts instead, e.g. [\"input.region\"]."
    • addedInput schema / properties / maskRule
      Added value: +{
      +  "description": "With `target`: the rule that produces column masks, e.g. \"data.filters.masks\". Overrides a `mask_rule` annotation.",
      +  "minLength": 1,
      +  "type": "string"
      +}
    • addedInput schema / properties / path
      Added value: +{
      +  "anyOf": [
      +    {
      +      "minLength": 1,
      +      "type": "string"
      +    },
      +    {
      +      "items": {
      +        "minLength": 1,
      +        "type": "string"
      +      },
      +      "minItems": 1,
      +      "type": "array"
      +    }
      +  ],
      +  "description": "With `target`: the filter rule under `data.`, e.g. \"filters/include\" or \"filters.include\", or its segments as an array."
      +}
    • changedInput schema / properties / query / description
      Previous value: -"Rego query to compile, e.g. \"data.rbac.allow == true\"."New value: +"Rego query to compile, e.g. \"data.rbac.allow == true\". Required without `target`; not used with it."
    • addedInput schema / properties / tableMappings
      Added value: +{
      +  "additionalProperties": {},
      +  "description": "With a SQL target: table and column names to write instead of the ones in the policy, per dialect, e.g. {\"postgresql\": {\"fruits\": {\"$self\": \"f\", \"colour\": \"col\"}}}.",
      +  "type": "object"
      +}
    • addedInput schema / properties / target
      Added value: +{
      +  "description": "Translate the rule at `path` into a data filter of this kind. `multitarget` returns several at once, one per entry in `dialects`.",
      +  "enum": [
      +    "sql.postgresql",
      +    "sql.mysql",
      +    "sql.sqlserver",
      +    "sql.sqlite",
      +    "ucast.all",
      +    "ucast.minimal",
      +    "ucast.prisma",
      +    "ucast.linq",
      +    "lucene.solr",
      +    "multitarget"
      +  ],
      +  "type": "string"
      +}
    • changedInput schema / properties / unknowns / description
      Previous value: -"Refs to treat as unknown (default: [\"input\"])."New value: +"Refs to treat as unknown. Default [\"input\"] for a query; with `target`, none beyond what the policy declares, e.g. [\"input.fruits\"] for the table searched."
    • removedInput schema / required
      Removed value: -[
      -  "query"
      -]
  2. Addedv0.1.13
  3. Removedv0.1.5
  4. Addedv0.1.2
  5. Removedv0.1.1
  6. First observedv0.1.0

TDQS

A3.8/5.0
Behavior3/5

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

Annotations already declare readOnly/idempotent/non-destructive/openWorld, so safety is covered. The description still adds substantial behavioral context: empty-result sentinels (SQL "", UCAST {}, Lucene *:*), the `matchesNothing`/missing-rule equivalence, EVAL_ERROR with per-expression `details.errors`, and OPA_VERSION_UNSUPPORTED with the Lucene 1.23 floor. That is meaningful disclosure beyond structured data.

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

Conciseness4/5

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

Front-loaded with the core action in sentence one, then progressively narrows to target mode, sentinel values, and error cases. It is a dense single block rather than a structured list, but essentially every sentence carries operational information.

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

Completeness5/5

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

For an 8-parameter, no-output-schema tool, the description supplies the missing return-value semantics (residual query, empty-match sentinels, matchesNothing) and failure modes with error codes. An agent can reason about outcomes and errors without an output schema.

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 description coverage is 100% and the schema already documents `path`, `target`, `dialects`, `maskRule`, `unknowns`, `tableMappings` and the input-vs-unknowns interaction, so the schema does the heavy lifting. The description reinforces target types and the unknowns default but adds little parameter semantics not already present in the schema.

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

Purpose4/5

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

States a specific verb and resource ('Send a query to the OPA server's /v1/compile endpoint for partial evaluation') and cleanly splits the two modes: residual query vs. `target`-driven data filter. It never names a sibling (e.g. rego_compile_query or opa_query_decision), so the agent must infer that this is the server-side compile path rather than a local one.

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?

Explicit mode selection: without `target` you get the residual query, with `target` you get a translated data filter, and the required prerequisite ('the rule must be loaded on the server') is given. It stops short of saying when to prefer this over the local rego_compile_query sibling or opa_query_decision.

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