Skip to content

Feature Request: Authorization Context Enrichment via Rego Policies #4112

Open

Description

@ghaskins

Summary

ToolHive's authorization middleware currently produces generic, coarse-grained
operation and resource identifiers for MCP requests. For example, a tools/call
to list_commits on the GitHub MCP server produces:

  • operation: mcp:tool:call
  • resource: mrn:mcp:github:tool:list_commits

These identifiers are sufficient for policies like "allow all tool calls on the
github server", but they carry no argument context. This makes it impossible to
write fine-grained policies such as "allow list_commits only on repos the user
owns" because the target repository is never surfaced to the PDP.

Proposed Solution

Add a context enrichment stage to the authorization pipeline. Before a
non-list MCP request is sent to the PDP, an optional user-defined Rego policy
runs and may transform the operation/resource identifiers based on full request
context (tool arguments, server ID, feature, etc.).

With enrichment enabled, the same list_commits call could produce:

  • operation: mcp:tool:github:commit:list
  • resource: mrn:toolhive:github:repo:owner/my-repo

The PDP can then enforce repository-scoped or verb-scoped policies that would
not be possible today.

Design

New pkg/authz/enrichment package

  • Enricher interface – Enrich(Input) (*Result, error). Implementations
    can use Rego, schema inference, or any other mechanism.
  • Pipeline – runs enrichers in order; returns the first non-nil result.
    Errors are logged and skipped so enrichment failures never block requests.
  • RegoEnricher – evaluates a Rego policy in package
    toolhive.authz.enrichment; the policy defines a rule named result that
    maps input fields to operation and resource strings.
  • Input / Result types – carry feature, operation, resource ID,
    arguments, and server ID into the policy; carry back enriched strings.

Config

Add an optional enrichment block to the existing authz YAML config:

version: "1.0"
type: httpv1

enrichment:
  rego_policy: "/path/to/enrichment.rego"
  # OR inline:
  # rego_policy_inline: |
  #   package toolhive.authz.enrichment
  #   default result := {}
  #   result := { "operation": ..., "resource": ... } if { ... }

pdp:
  http:
    url: "http://localhost:9000"
  claim_mapping: "standard"
  context:
    include_args: true

Configs without the enrichment section continue to work unchanged. Exactly
one of rego_policy (file path) or rego_policy_inline may be set.

Middleware integration

  • authz.Middleware gains an optional *enrichment.Pipeline parameter (nil
    disables enrichment; existing callers are unaffected in behavior).
  • For non-list operations the middleware runs applyEnrichment before calling
    the authorizer. The enrichment result is stored in the request context.
  • The HTTP PDP authorizer reads the enriched operation/resource from context and
    substitutes them into the PORC before dispatching to the PDP. The
    context.mcp fields retain the original unenriched values so the PDP has
    access to both.
  • List operations are not enriched (they use the existing filter-on-response
    path).

Example policies

Provide a full example enrichment policy for the GitHub MCP server
(examples/enrichment-github.rego) covering all 41 GitHub tools across six
resource scopes (repo, owner, org, search, user, global) with a consistent
noun:verb operation convention, plus an accompanying config example
(examples/authz-httpv1-enrichment-config.yaml).

Scope / Limitations

  • Phase 1: HTTP PDP authorizer (httpv1) only. Cedar (cedarv1) support is
    deferred to a follow-up.
  • Enrichment policies must use OPA v1 syntax (the if keyword is required
    before rule bodies).
  • Only non-list operations are enriched.

Acceptance Criteria

  • pkg/authz/enrichment package with Enricher, Pipeline, RegoEnricher,
    Input, Result, and Config types, with unit tests.
  • authz.Middleware accepts an optional *enrichment.Pipeline; nil is a
    no-op (no behaviour change for existing users).
  • HTTP PDP authorizer substitutes enriched operation/resource when an
    enrichment result is present in context.
  • enrichment block in authz YAML config (rego_policy / rego_policy_inline).
  • docs/authz.md updated with a "Context enrichment" section including
    the input/output contract, configuration reference, and examples.
  • Example Rego policy and config file added under examples/.
  • All existing authz tests continue to pass.

Activity

  1. added
    kubernetes
    on Mar 11, 2026
  2. added
    enhancement
    go
    api
    and removed
    kubernetes
    on Mar 11, 2026
  3. JAORMX commented on Mar 12, 2026

    @JAORMX
    Collaborator

    Hey @ghaskins, thanks for the detailed write-up! The idea of making authorization more context-aware is something we care about, so it's great to see someone thinking through this.

    Before we dig into the proposal itself, I wanted to flag something that I think changes the framing a bit. The issue mentions that tool arguments are never surfaced to the PDP, but we actually already have infrastructure for this today.

    The authz middleware already extracts MCP tool arguments from the request body (via pkg/mcp/parser.go), passes them through the authorizer interface, and the HTTP PDP authorizer can include them in the PORC context. There's an include_args config option in the context block:

    pdp:
      http:
        url: "http://localhost:9000"
      context:
        include_args: true

    When enabled, the PDP receives the full arguments under context.mcp.args. So a policy that says "allow list_commits only on repos the user owns" is possible today by inspecting context.mcp.args.repo (or whatever the argument name is) in your PDP policy. It's disabled by default to avoid sending potentially sensitive data to external PDPs, but it's a single config toggle. You can find more details in docs/authz.md around the context configuration section.

    With that in mind, I think the genuinely new part of your proposal is the operation/resource identifier enrichment. That is, transforming mcp:tool:call + mrn:mcp:github:tool:list_commits into something more semantic like mcp:tool:github:commit:list + mrn:toolhive:github:repo:owner/my-repo. That's an interesting idea and I can see how it would make policies more readable and natural to write.

    That said, I'd want us to think carefully about the trade-offs here:

    • Adding a Rego dependency (OPA) is a significant addition to the project. We'd need to weigh that against the benefit.
    • The enrichment pipeline adds a new abstraction layer in the hot path of every authorized request. We'd need to be thoughtful about performance and failure modes.
    • There's a question of whether this complexity is warranted given that include_args: true already gives PDPs access to the raw data they need to make fine-grained decisions.

    I'm not closing the door on this at all! But I think the right first step is to clarify what the actual gap is now that we know arguments can already be forwarded. Is the main motivation about making policies more ergonomic to write? Or is there a use case where raw arguments in context aren't sufficient?

    Would love to hear your thoughts on this. Thanks again for the thorough proposal!

  4. ghaskins commented on Mar 12, 2026

    @ghaskins
    ContributorAuthor

    @JAORMX That is an astute observation, and one that I should have called out explicitly in the proposal. Here's the problem that I currently face that is addressed by the proposal:

    The PDP is "programmed" by its inputs in various ways. The values in the PORC expression can be arbitrarily evaluated, lending the include_args feature to helping enrich the policy decision, as you pointed out. However, other fields like principal.mroles/mgroups/scopes, operation, and resource.id fields also instruct the PDP on which policies (and additional metadata) need to be loaded first before evaluation begins.

    It is this last point that is the problem.

    To put it another way, it is the Policy Enforcement Point's (PEP) job to formulate a meaningful PORC that not only has the attributes for evaluation, but the policy references needed for that evaluation. When the PEP is custom application code, it naturally has the context to do so. When its more of a generalized PEP, such as the ToolHive MCP proxy, it lacks some nuance that the proposal helps to restore.

    So, to put everything in context, if we take the resource identifier as an example: Before this proposal, we have identifiers such as mrn:mcp:github:tool:list_commits that now become mrn:toolhive:github:repo:owner/my-repo which means the resource resolver can look up repo specific policies using mrn:toolhive:github:repo:* or even looking up metadata for owner/my-repo specifically (e.g. ACLs from github, etc). This wouldn't be possible purely with context arguments because they would only be factored in after the policies/metadata are resolved.

    As to your performance/impact, I have a working prototype and I included a benchmark suite as part of the work. The benchmark suite indicates that the new REGO eval adds about 6 microseconds on average, 12 max, in the test harness. I should note that this small penalty is only paid when enrichment is configured, too. Users who leave the default unenriched configuration pay no latency penalty at all. 6-12us seems acceptable to me, though I am not sure that everyone would agree. The bigger concern was that OPA adds about 15MB to the image IIUC. I need to go back and quantify this more carefully.

    In the end, this doesn't need to be REGO based, though I think REGO solves the problem. I am open to other mechanisms/languages if something else is more palatable.

    Thank you for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiauthenticationauthorizationenhancementgo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions