Repository navigation
MCP Protocol and Policy Gates
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.
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— ReturnsprotocolVersion: "2024-11-05", server capabilities ("tools": {},"experimental": { "guapRollback": true }), andserverInfo. -
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 registeredTableProviders andActionCommands. -
tools/call— Executes tool dispatch underPolicyGatechecks followed by strictvalidate_against_schemavalidation. -
guap/rollback— Vendor-namespaced JSON-RPC 2.0 method for compensating rollback via a capturedRollbackSnapshot(tools/listtool counts are unchanged).
| 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 |
Across both ActionCommand and TableProvider dispatch in McpServerRegistry::dispatch():
- Non-object/non-null
argumentsimmediately fail with-32602(INVALID_PARAMS). -
PolicyGatechecks (ReadOnlyOnly,forensic_lock,validate_write_pathsagainstprotected_roots, andenforce_mutating_gatefordry_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. - For
ActionCommand,ensure_action_schema_properties()augmentsinputSchemawithdry_run(always) andconfirm_destructive(for mutating actions) prior to callingvalidate_against_schema, ensuring valid safety flags are never rejected byadditionalProperties: false.
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.
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 byTableProvidersort, supporting both GuapQuery sort strings like"col:desc,-other"and[{ column, direction }]arrays;"integer"requiresi64/u64, rejecting float literals). -
properties,required,additionalProperties(boolor subschema object). -
enum,minimum/maximum(exacti128comparison when both bound and instance are integral),minLength/maxLength(Unicode scalar count via.chars().count()), andminItems/maxItems/items.
-
-
Null Handling: Explicit JSON
nullon optional (non-required) declared properties is tolerated as absent, whereas missing or explicit JSONnullonrequiredproperties is strictly rejected with-32602(INVALID_PARAMS).
-
ActionCommandHooks: Actions opt into automated restoration by overridingsupports_rollback() -> bool(defaultfalse) androllback(&self, snapshot: &RollbackSnapshot, dry_run: bool) -> Result<Value, McpToolError>. -
RollbackSnapshot&RollbackStore(guap_mcp::rollback):-
RollbackSnapshotcapturessnapshot_id,action,target,captured_at_utc,before_state,restore_hint, andrestorable. -
RollbackStoresupports ephemeralRollbackStore::in_memory()and directory-backedRollbackStore::with_dir(dir)storage (<snapshot_id>.rollback.json), validatingsnapshot_idagainst path traversal, writing atomically via temp file +sync_all()+ rename (0o600mode on Unix), and enforcing0700Unix directory permissions on the store directory.
-
-
guap/rollbackDispatch (McpServerRegistry::dispatch_rollback):- Rejects immediately with
-32003ifforensic_lockis active orpolicy == PolicyMode::ReadOnlyOnly. - Validates
params.snapshot(RollbackSnapshot) and requiressnapshot.restorable == true(else-32602). - Enforces
enforce_mutating_gate(&snapshot.action, params, dry_run)(-32003ifDryRunOnlywithoutdry_run: true, orAllowMutatingwithoutdry_run: trueorconfirm_destructive: true). - Collects
snapshot.targetand path fields insidesnapshot.before_state(output,output_path,output_dir,export_path,custody_dir,dest,destination,target_path,out,out_dir,path,file_path,lnk_path) and checksPolicyGate::validate_write_paths()againstprotected_roots(-32003). - Resolves
snapshot.action(-32601if unknown) and invokesaction.rollback(&snapshot, dry_run).
- Rejects immediately with
guap-kit (v0.1.0) • GUAP Ecosystem (guap.ca) • 5 Crates • Rust 2024 Edition (1.89+) • Apache-2.0 License • Repository • Security Policy