enforcement

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package enforcement provides the concrete Enforcement and Activator implementations that compose the safety subsystems (policy, rate limiting, dry-run, truncation, bootstrap) into the MCP server's pluggable interfaces.

This package sits between the protocol core (mcp) and the domain-specific safety packages, keeping both layers decoupled.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gate

type Gate struct {
	Policy    *policy.Policy
	Bootstrap *bootstrap.Config
}

Gate implements mcp.Activator using policy and bootstrap.

func (*Gate) Clone

func (g *Gate) Clone() *Gate

func (*Gate) IsGroupAllowed

func (g *Gate) IsGroupAllowed(group string) bool

IsGroupAllowed checks whether the policy permits activating a group.

func (*Gate) OnActivate

func (g *Gate) OnActivate(names []string)

OnActivate marks tools as visible in the bootstrap layer.

type Pipeline

type Pipeline struct {
	Policy     *policy.Policy
	Bootstrap  *bootstrap.Config
	RateLimit  *ratelimit.RateLimiter
	DryRun     dryrun.Config
	Truncation truncate.Config
}

Pipeline implements mcp.Enforcement by composing the safety subsystems.

func (*Pipeline) AfterCall

func (p *Pipeline) AfterCall(result any) (any, error)

AfterCall applies post-processing (truncation) to a successful result.

Tool handlers return typed structs (e.g. ResultEnvelope) which the truncate package's type switch can't walk. We JSON-roundtrip the result into a generic map[string]any / []any tree before calling Truncate so the walker sees the whole structure. The extra marshal cost is acceptable because the server marshals the result for wire transport moments later anyway.

On marshal/unmarshal failure we fail open and return the original result unchanged — dropping a tool response because truncation misbehaved would be worse than returning an over-budget payload.

func (*Pipeline) BeforeCall

func (p *Pipeline) BeforeCall(ctx context.Context, name string, args map[string]any, hints mcp.ToolHints, schema map[string]any, lookupHandler func(string) (mcp.ToolHandler, bool)) (any, func(), error)

BeforeCall runs the enforcement pipeline before a tool handler:

  1. Schema validation (W2-01)
  2. Policy gate
  3. Rate limit acquire
  4. Dry-run intercept

func (*Pipeline) Clone

func (p *Pipeline) Clone() *Pipeline

func (*Pipeline) FilterTool

func (p *Pipeline) FilterTool(name string, hints mcp.ToolHints) bool

FilterTool reports whether a tool should be listed in tools/list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL