Skip to content

Interceptor Framework

MCP Hangar implements the SEP-1763 interceptor framework with hook-based event delivery and priority-ordered mutator pipelines. See ADR-005 for design rationale.

Architecture

Tool invocation
    |
    v
DigestValidator            (ADR-004: schema integrity check)
    |  emits DigestMismatchEvent on mismatch
    v
MutatorPipeline            (ADR-005: sequential transformation)
    |  ResponseTruncator, future: PII redaction, schema enforcement
    v
EventBus.publish()
    |
    +---> flat subscribers     (backward-compatible)
    +---> hook subscribers     (phase-wrapped Hook objects)
    +---> wildcard filters     (EventPattern matching)

Components

Digest Pinning (ADR-004)

TypeLocationPurpose
ToolDigestdomain/value_objects/tool_digest.pySHA-256 fingerprint of a tool's canonical schema
DigestPolicydomain/value_objects/tool_digest.pyEnforcement level + unknown-tool handling + allowlist
DigestEnforcementdomain/value_objects/tool_digest.pyEnum: audit, warn, block
compute_tool_digest()domain/services/digest_computation.pyDeterministic SHA-256 over canonical JSON
DigestValidatordomain/services/digest_validator.pyValidates tools against policy, emits DigestMismatchEvent

Hook-Based Event Model (ADR-005)

TypeLocationPurpose
HookPhasedomain/value_objects/hook.pyEnum: BEFORE, AROUND, AFTER, ON_ERROR, OBSERVE
Hookdomain/value_objects/hook.pyWraps (event, phase, sequence_number)
IHookSubscriberdomain/contracts/hook_subscriber.pyProtocol for phase-aware event delivery
EventBusinfrastructure/event_bus.pyFan-out to both flat subscribers and hook subscribers

Mutator Pipeline (ADR-005)

TypeLocationPurpose
IMutatordomain/contracts/mutator.pyProtocol: priority_hint, applies_to, mutate()
MutationContextdomain/contracts/mutator.pyInput: method, direction, payload, correlation_id
MutationResultdomain/contracts/mutator.pyOutput: payload, changed flag, audit_only flag
MutatorPipelineapplication/services/mutator_pipeline.pySorts by (priority_hint, registration_index), executes sequentially
ResponseTruncatorapplication/mutators/response_truncator.pyTruncates oversized tools/call responses, emits ResponseTruncated

Wildcard Subscriptions (ADR-005)

TypeLocationPurpose
EventPatterndomain/value_objects/event_pattern.pySegment-wise wildcard matching (*, tools/*, */response)
compile_event_patterns()server/api/ws/filters.pyCompiles raw strings into EventPattern objects
matches_filters()server/api/ws/filters.pyTests events against wildcard-aware subscription filters

Interceptor Discoverability

GET /interceptors/list returns mcp-hangar's capabilities as a SEP-1763 interceptor:

json
{
  "interceptors": [
    {
      "name": "mcp-hangar",
      "version": "<package version>",
      "types": ["validator", "mutator", "observer"],
      "capabilities": {
        "failOpen": true,
        "auditMode": true,
        "trustBoundaryAware": true
      }
    }
  ]
}

Mutator Ordering

Mutators execute in ascending priority_hint order. Ties are broken by registration order (stable sort).

Mutatorpriority_hintRationale
(future: PII redactor)100Runs early to redact before other transforms
(future: schema enforcer)500Validates structure after redaction
ResponseTruncator1000Runs last to truncate after all other transforms

Event Flow

  1. DigestValidator.validate_tool() produces DigestValidationResult with optional DigestMismatchEvent.
  2. Caller publishes events via EventBus.publish().
  3. EventBus delivers to flat subscribers (type-matched), hook subscribers (phase-wrapped), and wildcard-filtered WebSocket streams.
  4. MutatorPipeline.execute() runs registered mutators sequentially.
  5. Mutators collect domain events (e.g., ResponseTruncated) via event_collector list pattern.
  6. Caller publishes mutator events to EventBus for audit trail.

P2 Items (Not Yet Implemented)

  • interceptor/invoke JSON-RPC method (explicit invocation mode)
  • Shadow mutations (audit mode on mutators)
  • Per-interceptor failOpen granularity
  • Extended lifecycle events (resources/*, prompts/*, sampling/*, elicitation/*, roots/*)

MCP Hangar · Released under MIT License.