{
  "openapi": "3.1.0",
  "info": {
    "title": "Librari agent API",
    "version": "1.0.0",
    "description": "The public agent surface of Librari: the MCP server and the OAuth 2.0 endpoints that authorize it. Tool-level detail lives in the MCP server card and AGENTS.md.",
    "contact": { "email": "support@thelibrari.com" }
  },
  "externalDocs": {
    "description": "Agent interface (AGENTS.md)",
    "url": "https://www.thelibrari.com/AGENTS.md"
  },
  "servers": [{ "url": "https://www.thelibrari.com" }],
  "paths": {
    "/api/mcp": {
      "post": {
        "summary": "MCP JSON-RPC endpoint (Streamable HTTP)",
        "description": "Send one MCP JSON-RPC message per request (initialize, tools/list, tools/call, ...). The server is stateless. Requests that accept text/event-stream receive the response as a single SSE event.",
        "security": [{ "bearer": [] }, { "oauth2": ["read", "write"] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/JsonRpcRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC response",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JsonRpcResponse" }
              },
              "text/event-stream": {
                "schema": { "type": "string" }
              }
            }
          },
          "204": { "description": "Notification accepted; no response body" },
          "400": { "description": "Malformed JSON-RPC message" },
          "401": {
            "description": "Missing or invalid credential, or no active paid subscription. The WWW-Authenticate header points to the protected-resource metadata."
          },
          "403": { "description": "Request Origin not allowed" }
        }
      }
    },
    "/oauth/authorize": {
      "get": {
        "summary": "OAuth 2.0 authorization endpoint",
        "description": "Browser-facing consent page. Authorization code flow with PKCE (S256 only). Scopes are not negotiated: every grant carries read and write. When the user approves or denies on the consent page, the form submission responds 303 to redirect_uri with code and state, or with error=access_denied.",
        "parameters": [
          { "name": "response_type", "in": "query", "required": true, "schema": { "type": "string", "enum": ["code"] } },
          { "name": "client_id", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "redirect_uri", "in": "query", "required": true, "schema": { "type": "string", "format": "uri" } },
          { "name": "code_challenge", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "code_challenge_method", "in": "query", "required": true, "schema": { "type": "string", "enum": ["S256"] } },
          { "name": "state", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Consent page (HTML)" },
          "307": { "description": "Redirect to sign-in when the user is signed out, or to redirect_uri with error=invalid_request (and state) when PKCE is missing or invalid" }
        }
      }
    },
    "/api/oauth/token": {
      "post": {
        "summary": "OAuth 2.0 token endpoint",
        "description": "Exchange an authorization code (with code_verifier) or a refresh token for an access token.",
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "required": ["grant_type"],
                "properties": {
                  "grant_type": { "type": "string", "enum": ["authorization_code", "refresh_token"] },
                  "code": { "type": "string" },
                  "code_verifier": { "type": "string" },
                  "redirect_uri": { "type": "string", "format": "uri" },
                  "refresh_token": { "type": "string" },
                  "client_id": { "type": "string" },
                  "client_secret": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Token response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "access_token": { "type": "string" },
                    "token_type": { "type": "string" },
                    "expires_in": { "type": "integer" },
                    "refresh_token": { "type": "string" },
                    "scope": { "type": "string" }
                  }
                }
              }
            }
          },
          "400": { "description": "OAuth error response" },
          "401": { "description": "Client authentication failed" },
          "403": { "description": "access_denied: the user has no active paid subscription (authorization_code and refresh_token grants)" }
        }
      }
    },
    "/api/oauth/register": {
      "post": {
        "summary": "OAuth 2.0 Dynamic Client Registration (RFC 7591)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["redirect_uris"],
                "properties": {
                  "redirect_uris": { "type": "array", "items": { "type": "string", "format": "uri" } },
                  "client_name": { "type": "string" },
                  "token_endpoint_auth_method": { "type": "string", "enum": ["none"], "description": "Registered clients are public; any other value is ignored." }
                }
              }
            }
          }
        },
        "responses": {
          "201": { "description": "Public client registered; returns client_id (no client_secret)" },
          "400": { "description": "Invalid client metadata" }
        }
      }
    },
    "/api/oauth/revoke": {
      "post": {
        "summary": "OAuth 2.0 Token Revocation (RFC 7009)",
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "required": ["token"],
                "properties": {
                  "token": { "type": "string" },
                  "token_type_hint": { "type": "string", "enum": ["access_token", "refresh_token"] }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Token revoked, or was already invalid" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "An OAuth access token or a personal Librari API key."
      },
      "oauth2": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://www.thelibrari.com/oauth/authorize",
            "tokenUrl": "https://www.thelibrari.com/api/oauth/token",
            "refreshUrl": "https://www.thelibrari.com/api/oauth/token",
            "scopes": {
              "read": "Search and read the user's contracts",
              "write": "Archive and unarchive the user's contracts. Always granted alongside read."
            }
          }
        }
      }
    },
    "schemas": {
      "JsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "method"],
        "properties": {
          "jsonrpc": { "const": "2.0" },
          "id": { "type": ["string", "integer"] },
          "method": { "type": "string" },
          "params": { "type": "object" }
        }
      },
      "JsonRpcResponse": {
        "type": "object",
        "required": ["jsonrpc"],
        "properties": {
          "jsonrpc": { "const": "2.0" },
          "id": { "type": ["string", "integer", "null"] },
          "result": { "type": "object" },
          "error": {
            "type": "object",
            "properties": {
              "code": { "type": "integer" },
              "message": { "type": "string" }
            }
          }
        }
      }
    }
  }
}
