{"openapi": "3.1.0", "info": {"title": "Legal-Anything API — Deterministic Commercial Contract Trap Detection & Counter-Redline Engine", "version": "1.0.0", "summary": "Deterministic clause pre-review across 48 commercial trap rules, risk scoring (0-100), copy-paste counter-redline generation, and 3-tier negotiation fallback engine across SaaS MSA, NDA, GDPR DPA, SOW/Supplier, and Channel/Lease agreements.", "description": "Legal-Anything is a deterministic commercial contract clause pre-review software tool. It is not a law firm and does not provide legal advice or legal representation. Zero client data resale commitment: submitted contract text is never sold or used to train third-party models. Fixed non-contingent prepaid credit packs via Creem.io ($19 / $79 / $199) and zero charge ($0) on empty or invalid HTTP 422 inputs.", "contact": {"name": "Legal-Anything Operations", "email": "legal-ops@fastmesh.dev", "url": "https://legal.fastmesh.dev"}}, "servers": [{"url": "https://legal.fastmesh.dev", "description": "Production HTTPS Gateway"}, {"url": "http://127.0.0.1:9002", "description": "Local Upstream Engine"}], "tags": [{"name": "Health", "description": "Service liveness and loaded rule telemetry"}, {"name": "Analysis", "description": "Deterministic contract trap detection, batch scanning, and counter-redline generation"}, {"name": "Billing", "description": "Creem.io prepaid credit pack catalog, tenant balances, and HMAC-SHA256 webhook settlement"}, {"name": "MCP", "description": "Remote JSON-RPC 2.0 Model Context Protocol endpoint"}], "paths": {"/health": {"get": {"tags": ["Health"], "operationId": "getHealth", "summary": "Service health check and rule inventory summary", "responses": {"200": {"description": "Engine healthy and ready", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/HealthResponse"}}}}}}}, "/v1/analyze": {"post": {"tags": ["Analysis"], "operationId": "analyzeContract", "summary": "Analyze a commercial contract for trap clauses and generate counter-redlines", "description": "Parses plain text, Markdown, or dirty OCR contract text across SaaS MSA, NDA, GDPR DPA, SOW/Supplier, and Channel/Lease genres using 48 deterministic rules. Returns a 0-100 risk score, trap findings, copy-paste counter-redline clauses, 3-tier negotiation fallbacks, cross-tenant ArchiveCache metadata, and SHA-256 receipt proof. Rejects empty/gibberish inputs with HTTP 422 and billed_micros=0. Also aliased at POST /api/analyze.", "parameters": [{"name": "X-Tenant-Id", "in": "header", "required": false, "description": "Optional tenant identifier fallback if omitted from the JSON request body.", "schema": {"type": "string"}}], "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AnalyzeRequest"}}}}, "responses": {"200": {"description": "Deterministic ContractTrapReport", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ContractTrapReport"}}}}, "422": {"description": "Unbilled validation rejection (empty, placeholder, too short, or gibberish input; billed_micros=0)", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/UnbilledErrorResponse"}}}}}}}, "/v1/batch-analyze": {"post": {"tags": ["Analysis"], "operationId": "batchAnalyzeContracts", "summary": "Batch analyze multiple commercial contracts in a single request", "description": "Evaluates up to 100 contracts deterministically, isolating any invalid entries as unbilled 422 items without failing valid contracts in the batch.", "parameters": [{"name": "X-Tenant-Id", "in": "header", "required": false, "description": "Optional tenant identifier fallback for batch items.", "schema": {"type": "string"}}], "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/BatchAnalyzeRequest"}}}}, "responses": {"200": {"description": "Batch analysis summary and per-contract reports", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/BatchAnalyzeResponse"}}}}, "422": {"description": "Empty or malformed batch payload (billed_micros=0)", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/UnbilledErrorResponse"}}}}}}}, "/v1/redline": {"post": {"tags": ["Analysis"], "operationId": "generateRedlines", "summary": "Generate copy-paste counter-redline clauses and 3-tier negotiation fallbacks", "description": "Accepts either raw clause text (`clause_text`) or explicit trap rule IDs (`rule_ids`) and returns copy-paste replacement wording, negotiation email snippets, and 3-tier fallback tables.", "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/RedlineRequest"}}}}, "responses": {"200": {"description": "Generated counter-redline clauses and fallback tiers", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/RedlineResponse"}}}}, "422": {"description": "Unbilled invalid input (billed_micros=0)", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/UnbilledErrorResponse"}}}}}}}, "/v1/rules": {"get": {"tags": ["Analysis"], "operationId": "listRules", "summary": "List loaded deterministic trap detection rules and supported contract genres", "parameters": [{"name": "genre", "in": "query", "required": false, "description": "Optional contract genre filter (saas_msa, nda, dpa, sow_supplier, channel_lease, general)", "schema": {"type": "string", "enum": ["saas_msa", "nda", "dpa", "sow_supplier", "channel_lease", "general"]}}], "responses": {"200": {"description": "Deterministic rule catalog", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/RulesListResponse"}}}}}}}, "/v1/billing/balance": {"get": {"tags": ["Billing"], "operationId": "getBillingBalance", "summary": "Check prepaid Creem credit balance for a tenant", "description": "Returns remaining prepaid contract pre-review credits, lifetime counters, and available fixed-fee non-contingent Creem packs. Any email identifier is hashed to a 12-char SHA-256 prefix; zero plaintext PII is persisted.", "parameters": [{"name": "tenant_id", "in": "query", "required": false, "description": "Tenant identifier or SHA-256 12-char payer hash (may also be supplied via X-Tenant-Id header).", "schema": {"type": "string"}}, {"name": "X-Tenant-Id", "in": "header", "required": false, "description": "Optional tenant identifier header fallback when tenant_id query parameter is omitted.", "schema": {"type": "string"}}], "responses": {"200": {"description": "Tenant credit balance, lifetime counters, and available prepaid SKUs", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/BillingBalanceResponse"}}}}}}}, "/v1/billing/skus": {"get": {"tags": ["Billing"], "operationId": "listBillingSkus", "summary": "List Creem.io fixed-fee non-contingent prepaid credit pack SKUs", "responses": {"200": {"description": "Prepaid credit pack catalog ($19 Starter 5-Pack / $79 Pro 30-Pack / $199 Agency 100-Pack)", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/BillingSkusResponse"}}}}}}}, "/webhooks/creem": {"post": {"tags": ["Billing"], "operationId": "handleCreemWebhook", "summary": "Receive and verify Creem.io payment webhooks", "description": "Validates the `creem-signature` HMAC-SHA256 header in constant time, rejects invalid signatures with HTTP 401, deduplicates replays by `event_id`, and idempotently credits prepaid packs in SQLite WAL mode with PII-redacted receipts.", "parameters": [{"name": "creem-signature", "in": "header", "required": true, "description": "HMAC-SHA256 hex digest computed with CREEM_WEBHOOK_SECRET", "schema": {"type": "string"}}], "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "additionalProperties": true}}}}, "responses": {"200": {"description": "Webhook verified and idempotently recorded", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/CreemWebhookReceipt"}}}}, "401": {"description": "Missing or invalid creem-signature HMAC-SHA256 digest", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/UnbilledErrorResponse"}}}}}}}, "/mcp": {"get": {"tags": ["MCP"], "operationId": "getMcpManifest", "summary": "Inspect remote MCP server descriptor and tool inventory", "responses": {"200": {"description": "Remote MCP manifest JSON", "content": {"application/json": {"schema": {"type": "object", "additionalProperties": true}}}}}}, "post": {"tags": ["MCP"], "operationId": "invokeMcpJsonRpc", "summary": "JSON-RPC 2.0 Model Context Protocol endpoint", "description": "Supports MCP methods `initialize`, `tools/list`, and `tools/call` for `legal_analyze_contract`, `legal_generate_redlines`, `legal_list_rules`, and `legal_batch_analyze`.", "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/McpJsonRpcRequest"}}}}, "responses": {"200": {"description": "JSON-RPC 2.0 MCP response", "content": {"application/json": {"schema": {"type": "object", "properties": {"jsonrpc": {"type": "string", "const": "2.0"}, "id": {"type": ["string", "integer", "null"]}, "result": {"type": "object", "additionalProperties": true}, "error": {"type": "object", "additionalProperties": true}}}}}}}}}}, "components": {"schemas": {"ContractGenre": {"type": "string", "enum": ["auto", "saas_msa", "nda", "dpa", "sow_supplier", "channel_lease"], "default": "auto"}, "HealthResponse": {"type": "object", "required": ["ok", "status", "service", "version", "port", "rules_loaded", "genres", "tool_disclaimer"], "properties": {"ok": {"type": "boolean", "example": true}, "status": {"type": "string", "example": "ok"}, "service": {"type": "string", "example": "legal-anything"}, "version": {"type": "string", "example": "1.0.0"}, "port": {"type": "integer", "example": 9002}, "rules_loaded": {"type": "integer", "minimum": 48, "example": 48}, "genres": {"type": "array", "items": {"type": "string"}}, "tool_disclaimer": {"type": "string"}}}, "AnalyzeRequest": {"type": "object", "required": ["contract_text"], "properties": {"contract_text": {"type": "string", "minLength": 40, "description": "Raw commercial contract text (plain text, Markdown, or OCR output). Aliases `text`, `contract`, `document`, and `content` are also accepted."}, "contract_type": {"$ref": "#/components/schemas/ContractGenre"}, "contract_title": {"type": "string", "description": "Optional contract title or document label."}, "tenant_id": {"type": "string", "description": "Optional tenant identifier for cross-tenant 7d-30d verified-hit archive cache metering."}, "no_cache": {"type": "boolean", "default": false, "description": "Bypass ArchiveCache lookup and compute fresh deterministic output."}, "outcome_price_micros": {"type": "integer", "minimum": 0, "description": "Optional per-call outcome price override in micro-USD (default 190000)."}}}, "TrapFinding": {"type": "object", "required": ["finding_id", "rule_id", "genre", "title", "severity", "risk_weight", "clause_topic", "clause_index", "clause_heading", "matched_excerpt", "rationale"], "properties": {"finding_id": {"type": "string"}, "rule_id": {"type": "string"}, "genre": {"type": "string"}, "title": {"type": "string"}, "severity": {"type": "string", "enum": ["critical", "high", "medium", "low"]}, "risk_weight": {"type": "integer"}, "clause_topic": {"type": "string"}, "clause_index": {"type": "integer"}, "clause_heading": {"type": "string"}, "matched_excerpt": {"type": "string"}, "matched_pattern_label": {"type": "string"}, "rationale": {"type": "string"}, "char_start": {"type": "integer"}, "char_end": {"type": "integer"}}}, "CounterRedlineClause": {"type": "object", "required": ["finding_id", "rule_id", "target_clause_heading", "redline_instruction", "copy_paste_redline", "email_negotiation_snippet"], "properties": {"finding_id": {"type": "string"}, "rule_id": {"type": "string"}, "target_clause_heading": {"type": "string"}, "redline_instruction": {"type": "string"}, "copy_paste_redline": {"type": "string"}, "email_negotiation_snippet": {"type": "string"}}}, "NegotiationFallbackTier": {"type": "object", "required": ["finding_id", "rule_id", "tier_1_ideal", "tier_2_balanced", "tier_3_walkaway", "leverage_points"], "properties": {"finding_id": {"type": "string"}, "rule_id": {"type": "string"}, "tier_1_ideal": {"type": "string"}, "tier_2_balanced": {"type": "string"}, "tier_3_walkaway": {"type": "string"}, "leverage_points": {"type": "array", "items": {"type": "string"}}}}, "CacheMeta": {"type": "object", "properties": {"cache_hit": {"type": "boolean"}, "pricing_percent": {"type": "integer"}, "reason": {"type": "string"}, "is_repeat_tenant": {"type": "boolean"}, "tenant_id": {"type": "string"}, "key_hash": {"type": "string"}, "ttl_s": {"type": "integer"}, "age_s": {"type": "number"}, "outcome_price_micros": {"type": "integer"}, "charged_micros": {"type": "integer"}, "upstream_cost_micros": {"type": "integer"}, "billing_receipt_sha256": {"type": "string"}}, "additionalProperties": true}, "ContractTrapReport": {"type": "object", "required": ["ok", "status_code", "tool_disclaimer", "contract_type", "detected_contract_type", "clause_count", "risk_score_0_100", "risk_grade", "summary", "trap_findings", "counter_redline_clauses", "negotiation_fallback_tiers", "clean_clauses_verified", "cache_meta", "sha256_proof", "billed_micros", "generated_at_utc"], "properties": {"ok": {"type": "boolean"}, "status_code": {"type": "integer", "example": 200}, "tool_disclaimer": {"type": "string"}, "contract_type": {"type": "string"}, "requested_contract_type": {"type": "string"}, "detected_contract_type": {"type": "string"}, "contract_title": {"type": "string"}, "clause_count": {"type": "integer"}, "risk_score_0_100": {"type": "integer", "minimum": 0, "maximum": 100}, "risk_grade": {"type": "string", "enum": ["LOW", "MODERATE", "HIGH", "CRITICAL"]}, "summary": {"type": "string"}, "trap_findings": {"type": "array", "items": {"$ref": "#/components/schemas/TrapFinding"}}, "counter_redline_clauses": {"type": "array", "items": {"$ref": "#/components/schemas/CounterRedlineClause"}}, "negotiation_fallback_tiers": {"type": "array", "items": {"$ref": "#/components/schemas/NegotiationFallbackTier"}}, "clean_clauses_verified": {"type": "integer"}, "clean_clause_headings": {"type": "array", "items": {"type": "string"}}, "cache_meta": {"$ref": "#/components/schemas/CacheMeta"}, "sha256_proof": {"type": "string"}, "billed_micros": {"type": "integer"}, "charged_micros": {"type": "integer"}, "generated_at_utc": {"type": "string", "format": "date-time"}}}, "BatchAnalyzeRequest": {"type": "object", "required": ["contracts"], "properties": {"contracts": {"type": "array", "minItems": 1, "maxItems": 100, "items": {"oneOf": [{"type": "string"}, {"$ref": "#/components/schemas/AnalyzeRequest"}]}}, "tenant_id": {"type": "string"}, "no_cache": {"type": "boolean", "default": false}, "outcome_price_micros": {"type": "integer", "minimum": 0}}}, "BatchAnalyzeResponse": {"type": "object", "properties": {"ok": {"type": "boolean"}, "status_code": {"type": "integer", "example": 200}, "tool_disclaimer": {"type": "string"}, "total_submitted": {"type": "integer"}, "analyzed_count": {"type": "integer"}, "rejected_count": {"type": "integer"}, "highest_risk_score": {"type": "integer"}, "total_billed_micros": {"type": "integer"}, "billed_micros": {"type": "integer"}, "results": {"type": "array", "items": {"type": "object", "additionalProperties": true}}, "generated_at_utc": {"type": "string", "format": "date-time"}}}, "RedlineRequest": {"type": "object", "properties": {"clause_text": {"type": "string", "description": "Single clause text to analyze and redline."}, "rule_ids": {"type": "array", "items": {"type": "string"}, "description": "Optional explicit list of rule IDs for which to render counter-redline wording."}, "contract_type": {"$ref": "#/components/schemas/ContractGenre"}, "target_clause_heading": {"type": "string", "default": "Target Clause"}}}, "RedlineResponse": {"type": "object", "properties": {"ok": {"type": "boolean"}, "status_code": {"type": "integer", "example": 200}, "tool_disclaimer": {"type": "string"}, "matched_rule_ids": {"type": "array", "items": {"type": "string"}}, "unknown_rule_ids": {"type": "array", "items": {"type": "string"}}, "counter_redline_clauses": {"type": "array", "items": {"$ref": "#/components/schemas/CounterRedlineClause"}}, "negotiation_fallback_tiers": {"type": "array", "items": {"$ref": "#/components/schemas/NegotiationFallbackTier"}}, "generated_at_utc": {"type": "string", "format": "date-time"}}}, "RulesListResponse": {"type": "object", "properties": {"ok": {"type": "boolean"}, "status_code": {"type": "integer", "example": 200}, "tool_disclaimer": {"type": "string"}, "engine_version": {"type": "string"}, "total_rules": {"type": "integer"}, "all_rules_count": {"type": "integer"}, "genres": {"type": "array", "items": {"type": "string"}}, "rules": {"type": "array", "items": {"type": "object", "properties": {"rule_id": {"type": "string"}, "genre": {"type": "string"}, "title": {"type": "string"}, "severity": {"type": "string"}, "risk_weight": {"type": "integer"}, "clause_topic": {"type": "string"}, "rationale": {"type": "string"}, "scope": {"type": "string"}}, "additionalProperties": true}}}}, "BillingBalanceResponse": {"type": "object", "properties": {"ok": {"type": "boolean"}, "tenant_id": {"type": "string"}, "payer_sha256_12": {"type": "string"}, "credits_balance": {"type": "integer"}, "total_credits_granted": {"type": "integer"}, "total_credits_consumed": {"type": "integer"}, "total_paid_cents": {"type": "integer"}, "updated_at_utc": {"type": ["string", "null"]}, "skus": {"type": "array", "items": {"type": "object", "additionalProperties": true}}, "tool_disclaimer": {"type": "string"}}}, "BillingSkusResponse": {"type": "object", "properties": {"ok": {"type": "boolean"}, "packs": {"type": "array", "items": {"type": "object", "additionalProperties": true}}, "tool_disclaimer": {"type": "string"}}}, "CreemWebhookReceipt": {"type": "object", "properties": {"ok": {"type": "boolean"}, "status_code": {"type": "integer"}, "event_id": {"type": "string"}, "event_type": {"type": "string"}, "idempotent_duplicate": {"type": "boolean"}, "tenant_id": {"type": "string"}, "payer_sha256_12": {"type": "string"}, "sku_id": {"type": "string"}, "amount_cents": {"type": "integer"}, "currency": {"type": "string"}, "credits_added": {"type": "integer"}, "credits_balance": {"type": "integer"}, "receipt_sha256": {"type": "string"}, "recorded_at_utc": {"type": "string", "format": "date-time"}, "tool_disclaimer": {"type": "string"}}}, "McpJsonRpcRequest": {"type": "object", "required": ["jsonrpc", "method"], "properties": {"jsonrpc": {"type": "string", "const": "2.0"}, "id": {"type": ["string", "integer", "null"]}, "method": {"type": "string", "enum": ["initialize", "tools/list", "tools/call"]}, "params": {"type": "object", "additionalProperties": true}}}, "UnbilledErrorResponse": {"type": "object", "required": ["ok", "status_code", "billed_micros"], "properties": {"ok": {"type": "boolean", "const": false}, "status_code": {"type": "integer", "example": 422}, "billed_micros": {"type": "integer", "const": 0}, "charged_micros": {"type": "integer", "const": 0}, "error": {"type": "string"}, "error_code": {"type": "string"}, "error_detail": {"type": "string"}, "tool_disclaimer": {"type": "string"}}}}}}