Skip to content

MCP Protocol and Policy Gates

Andrew Paolucci edited this page Dec 2, 2026 · 2 revisions

MCP Protocol & Policy Gates (guap-mcp)

guap-mcp (#![forbid(unsafe_code)]) implements a reusable Model Context Protocol (MCP) JSON-RPC 2.0 stdio runtime (protocolVersion: "2024-11-05") with strict safety policies, pure-Rust JSON Schema validation, and vendor-namespaced compensating rollback.


1. Supported JSON-RPC 2.0 Lifecycle

All tracing logs go strictly to stderr; stdout emits only newline-delimited JSON-RPC 2.0 frames (bounded at MAX_JSONRPC_LINE_BYTES = 16 MiB):

  • initialize — Returns protocolVersion: "2024-11-05", server capabilities ("tools": {}, "experimental": { "guapRollback": true }), and serverInfo.
  • ping — Empty {} heartbeat response.
  • notifications/initialized / notifications/cancelled — Handled silently without emitting a response frame (any request missing "id" is treated as a notification, provided "jsonrpc": "2.0" and "method" are valid).
  • tools/list — Emits JSON Schema definitions and MCP annotations (readOnlyHint, destructiveHint) for all registered TableProviders and ActionCommands.
  • tools/call — Executes tool dispatch under PolicyGate checks followed by strict validate_against_schema validation.
  • guap/rollback — Vendor-namespaced JSON-RPC 2.0 method for compensating rollback via a captured RollbackSnapshot (tools/list tool counts are unchanged).

2. Error Codes, Dispatch Precedence & PolicyGate Enforcement

Code Constant Cause
-32700 PARSE_ERROR Malformed JSON frame
-32600 INVALID_REQUEST Non-object frame, missing/non-"2.0" jsonrpc, missing method, or oversized line
-32601 METHOD_NOT_FOUND Unsupported JSON-RPC method, unknown tool name, or unknown rollback action
-32602 INVALID_PARAMS Missing required arguments, type/bounds/enum/additionalProperties schema violation, or non-restorable snapshot
-32003 POLICY_DENIED Mutating tool or guap/rollback invoked under PolicyMode::ReadOnlyOnly / forensic_lock, missing dry_run (DryRunOnly) / confirm_destructive (AllowMutating), or write path inside a protected_roots directory

Strict -32003 Before -32602 Precedence

Across both ActionCommand and TableProvider dispatch in McpServerRegistry::dispatch():

  1. Non-object/non-null arguments immediately fail with -32602 (INVALID_PARAMS).
  2. PolicyGate checks (ReadOnlyOnly, forensic_lock, validate_write_paths against protected_roots, and enforce_mutating_gate for dry_run / confirm_destructive) run before JSON Schema validation so blocked mutating calls or protected-root writes always fail closed with -32003 (POLICY_DENIED) even if arguments are incomplete or invalid.
  3. For ActionCommand, ensure_action_schema_properties() augments inputSchema with dry_run (always) and confirm_destructive (for mutating actions) prior to calling validate_against_schema, ensuring valid safety flags are never rejected by additionalProperties: false.

Read-Only Default (PolicyMode::ReadOnlyOnly)

By default, MCP servers construct their dispatchers in PolicyMode::ReadOnlyOnly. Read-only TableProvider endpoints carry readOnlyHint: true, destructiveHint: false, while ActionCommand endpoints carry readOnlyHint: false, destructiveHint: true. Calling any mutating endpoint without explicit policy opt-in (--dry-run-only or --allow-mutating) and confirmation fails closed with -32003 before touching OS, process, network, or filesystem state.


3. Pure-Rust JSON Schema Subset Validator (guap_mcp::schema)

validate_against_schema(schema: &Value, instance: &Value) -> Result<(), McpToolError> provides zero-dependency parameter validation with no external schema crates:

  • Supported Keywords:
    • type: single string ("string", "integer", "number", "boolean", "array", "object", "null") or union array (e.g. ["string", "array"] used by TableProvider sort, supporting both GuapQuery sort strings like "col:desc,-other" and [{ column, direction }] arrays; "integer" requires i64/u64, rejecting float literals).
    • properties, required, additionalProperties (bool or subschema object).
    • enum, minimum / maximum (exact i128 comparison when both bound and instance are integral), minLength / maxLength (Unicode scalar count via .chars().count()), and minItems / maxItems / items.
  • Null Handling: Explicit JSON null on optional (non-required) declared properties is tolerated as absent, whereas missing or explicit JSON null on required properties is strictly rejected with -32602 (INVALID_PARAMS).

4. Compensating Rollback (ActionCommand::rollback, guap/rollback, RollbackStore)

  • ActionCommand Hooks: Actions opt into automated restoration by overriding supports_rollback() -> bool (default false) and rollback(&self, snapshot: &RollbackSnapshot, dry_run: bool) -> Result<Value, McpToolError>.
  • RollbackSnapshot & RollbackStore (guap_mcp::rollback):
    • RollbackSnapshot captures snapshot_id, action, target, captured_at_utc, before_state, restore_hint, and restorable.
    • RollbackStore supports ephemeral RollbackStore::in_memory() and directory-backed RollbackStore::with_dir(dir) storage (<snapshot_id>.rollback.json), validating snapshot_id against path traversal, writing atomically via temp file + sync_all() + rename (0o600 mode on Unix), and enforcing 0700 Unix directory permissions on the store directory.
  • guap/rollback Dispatch (McpServerRegistry::dispatch_rollback):
    1. Rejects immediately with -32003 if forensic_lock is active or policy == PolicyMode::ReadOnlyOnly.
    2. Validates params.snapshot (RollbackSnapshot) and requires snapshot.restorable == true (else -32602).
    3. Enforces enforce_mutating_gate(&snapshot.action, params, dry_run) (-32003 if DryRunOnly without dry_run: true, or AllowMutating without dry_run: true or confirm_destructive: true).
    4. Collects snapshot.target and path fields inside snapshot.before_state (output, output_path, output_dir, export_path, custody_dir, dest, destination, target_path, out, out_dir, path, file_path, lnk_path) and checks PolicyGate::validate_write_paths() against protected_roots (-32003).
    5. Resolves snapshot.action (-32601 if unknown) and invokes action.rollback(&snapshot, dry_run).