{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://awooop.platform/contracts/v1/mcp_gateway",
  "title": "MCP Gateway Contract",
  "description": "AwoooP MCP Gateway 合約 — 定義工具閘道的傳輸層、認證與速率控制",
  "type": "object",
  "required": ["gateway_id", "gateway_name", "transport"],
  "additionalProperties": false,
  "properties": {
    "gateway_id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9_-]{1,127}$",
      "description": "Gateway 識別符"
    },
    "gateway_name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "description": "人類可讀名稱"
    },
    "transport": {
      "type": "string",
      "enum": ["stdio", "http", "sse"],
      "description": "MCP 傳輸協議（ADR-113）"
    },
    "endpoint": {
      "type": "string",
      "format": "uri",
      "description": "Gateway 端點 URL（transport=http/sse 時必填）"
    },
    "auth_scheme": {
      "type": "string",
      "enum": ["none", "bearer", "hmac"],
      "default": "none",
      "description": "認證方式"
    },
    "hmac_secret_ref": {
      "type": "string",
      "description": "HMAC secret 的 K8s Secret key ref（auth_scheme=hmac 時使用）"
    },
    "tools_exposed": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["tool_name", "schema_sha256"],
        "additionalProperties": false,
        "properties": {
          "tool_name": {"type": "string"},
          "description": {"type": "string"},
          "schema_sha256": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$",
            "description": "工具 input schema 的 SHA-256（確保合約不可竄改）"
          },
          "is_destructive": {
            "type": "boolean",
            "default": false,
            "description": "工具是否具破壞性（影響 require_approval 判斷）"
          }
        }
      },
      "description": "此 gateway 暴露的工具清單"
    },
    "rate_limit_rpm": {
      "type": ["integer", "null"],
      "minimum": 1,
      "description": "每分鐘最大 tool call 次數；null = 無限制"
    },
    "timeout_seconds": {
      "type": "integer",
      "minimum": 1,
      "maximum": 300,
      "default": 30,
      "description": "單次 tool call 超時秒數"
    },
    "is_enabled": {
      "type": "boolean",
      "default": true,
      "description": "Gateway 是否啟用"
    }
  },
  "if": {
    "properties": {"transport": {"const": "http"}},
    "required": ["transport"]
  },
  "then": {
    "required": ["endpoint"]
  }
}
