{
  "openapi": "3.1.0",
  "info": {
    "title": "JoAi API",
    "description": "JoAi developer API for agents, teams, contacts, items, memories, and MCP-connected automation. Prefer live discovery via MCP tools/list and this OpenAPI document. Product docs: https://docs.joai.ai/api",
    "version": "1.0.0",
    "contact": {
      "name": "JoAi",
      "url": "https://joai.ai",
      "email": "hello@joai.ai"
    },
    "license": {
      "name": "Proprietary"
    }
  },
  "servers": [
    {
      "url": "https://api.joai.ai/api/v1",
      "description": "JoAi HTTP API (URL versioning)"
    },
    {
      "url": "https://cortex.joai.ai",
      "description": "JoAi Cortex (MCP / A2A)"
    }
  ],
  "tags": [
    { "name": "Auth", "description": "OAuth and API token authentication — https://docs.joai.ai/integrations/api-tokens" },
    { "name": "Agents", "description": "Agent metadata and prompting — https://docs.joai.ai/agents" },
    { "name": "Contacts", "description": "CRM contacts — https://docs.joai.ai/apps/contacts" },
    { "name": "MCP", "description": "Model Context Protocol — https://docs.joai.ai/protocols/mcp" },
    { "name": "Jobs", "description": "Long-running async work" }
  ],
  "paths": {
    "/agents": {
      "get": {
        "tags": ["Agents"],
        "summary": "List agents for the authenticated team",
        "operationId": "listAgents",
        "security": [{ "bearerAuth": [] }, { "oauth2": ["mcp:read"] }],
        "parameters": [
          {
            "name": "X-Team-Slug",
            "in": "header",
            "required": true,
            "schema": { "type": "string" },
            "description": "Team slug to scope the request"
          },
          {
            "name": "cursor",
            "in": "query",
            "required": false,
            "schema": { "type": "string" },
            "description": "Opaque cursor from a previous page"
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 25 },
            "description": "Page size"
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated agent list",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AgentListResponse" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/contacts": {
      "get": {
        "tags": ["Contacts"],
        "summary": "List contacts",
        "operationId": "listContacts",
        "security": [{ "bearerAuth": [] }, { "oauth2": ["mcp:read"] }],
        "parameters": [
          {
            "name": "X-Team-Slug",
            "in": "header",
            "required": true,
            "schema": { "type": "string" }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": { "type": "string" }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 25 }
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated contacts",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ContactListResponse" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      },
      "post": {
        "tags": ["Contacts"],
        "summary": "Create a contact",
        "operationId": "createContact",
        "security": [{ "bearerAuth": [] }, { "oauth2": ["mcp:write"] }],
        "parameters": [
          {
            "name": "X-Team-Slug",
            "in": "header",
            "required": true,
            "schema": { "type": "string" }
          },
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string", "maxLength": 255 },
            "description": "Client-supplied key so retries do not create duplicate contacts"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": { "type": "string" },
                  "email": { "type": "string", "format": "email" },
                  "phone": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Contact created",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Contact" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/jobs": {
      "post": {
        "tags": ["Jobs"],
        "summary": "Start a long-running job",
        "operationId": "createJob",
        "security": [{ "bearerAuth": [] }, { "oauth2": ["mcp:write"] }],
        "parameters": [
          {
            "name": "X-Team-Slug",
            "in": "header",
            "required": true,
            "schema": { "type": "string" }
          },
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string", "maxLength": 255 }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["type"],
                "properties": {
                  "type": { "type": "string", "description": "Job type identifier" },
                  "input": { "type": "object", "additionalProperties": true }
                }
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Job accepted — poll statusUrl",
            "headers": {
              "Location": {
                "description": "URL to poll job status",
                "schema": { "type": "string", "format": "uri" }
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JobAccepted" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/jobs/{jobId}": {
      "get": {
        "tags": ["Jobs"],
        "summary": "Get async job status",
        "operationId": "getJob",
        "security": [{ "bearerAuth": [] }, { "oauth2": ["mcp:read"] }],
        "parameters": [
          {
            "name": "jobId",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          },
          {
            "name": "X-Team-Slug",
            "in": "header",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Job status",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/JobStatus" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/mcp": {
      "post": {
        "tags": ["MCP"],
        "summary": "JoAi unified MCP endpoint",
        "description": "JSON-RPC MCP surface. Discover tools with tools/list after OAuth. Optional ?agent={uuid} prefills the agent during login. Docs: https://docs.joai.ai/protocols/mcp",
        "operationId": "mcpUnified",
        "servers": [{ "url": "https://cortex.joai.ai" }],
        "security": [{ "oauth2": ["mcp:use"] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": true
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "MCP JSON-RPC response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": true
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "API token",
        "description": "JoAi API token. Create via Settings → API or https://docs.joai.ai/integrations/api-tokens. Scope with X-Team-Slug."
      },
      "oauth2": {
        "type": "oauth2",
        "description": "JoAi OAuth for MCP and API clients. Authorization server: https://api.joai.ai",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://api.joai.ai/oauth/authorize",
            "tokenUrl": "https://api.joai.ai/oauth/token",
            "refreshUrl": "https://api.joai.ai/oauth/token",
            "scopes": {
              "mcp:read": "Read MCP tools and resources",
              "mcp:write": "Write MCP tools that mutate state",
              "mcp:use": "Full MCP use (read + write) for agent clients"
            }
          },
          "clientCredentials": {
            "tokenUrl": "https://api.joai.ai/oauth/token",
            "scopes": {
              "mcp:read": "Read MCP tools and resources",
              "mcp:write": "Write MCP tools that mutate state",
              "mcp:use": "Full MCP use (read + write) for agent clients"
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "object",
            "required": ["code", "message"],
            "properties": {
              "code": {
                "type": "string",
                "description": "Machine-readable error code",
                "examples": ["unauthorized", "not_found", "conflict", "rate_limited", "validation_error"]
              },
              "message": {
                "type": "string",
                "description": "Human-readable error message"
              },
              "details": {
                "type": "object",
                "additionalProperties": true
              }
            }
          }
        }
      },
      "Pagination": {
        "type": "object",
        "properties": {
          "nextCursor": {
            "type": ["string", "null"],
            "description": "Pass as cursor on the next request; null when no further pages"
          },
          "limit": { "type": "integer" }
        }
      },
      "Agent": {
        "type": "object",
        "properties": {
          "uuid": { "type": "string" },
          "name": { "type": "string" },
          "slug": { "type": "string" }
        },
        "additionalProperties": true
      },
      "AgentListResponse": {
        "type": "object",
        "required": ["data", "pagination"],
        "properties": {
          "data": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Agent" }
          },
          "pagination": { "$ref": "#/components/schemas/Pagination" }
        }
      },
      "Contact": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" },
          "email": { "type": "string" },
          "phone": { "type": "string" }
        },
        "additionalProperties": true
      },
      "ContactListResponse": {
        "type": "object",
        "required": ["data", "pagination"],
        "properties": {
          "data": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Contact" }
          },
          "pagination": { "$ref": "#/components/schemas/Pagination" }
        }
      },
      "JobAccepted": {
        "type": "object",
        "required": ["jobId", "status", "statusUrl"],
        "properties": {
          "jobId": { "type": "string" },
          "status": { "type": "string", "enum": ["queued", "running"] },
          "statusUrl": { "type": "string", "format": "uri" }
        }
      },
      "JobStatus": {
        "type": "object",
        "required": ["jobId", "status"],
        "properties": {
          "jobId": { "type": "string" },
          "status": {
            "type": "string",
            "enum": ["queued", "running", "succeeded", "failed"]
          },
          "result": { "type": "object", "additionalProperties": true },
          "error": { "$ref": "#/components/schemas/Error" }
        }
      }
    },
    "responses": {
      "Error": {
        "description": "Error",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          },
          "application/problem+json": {
            "schema": {
              "type": "object",
              "properties": {
                "type": { "type": "string", "format": "uri" },
                "title": { "type": "string" },
                "status": { "type": "integer" },
                "detail": { "type": "string" },
                "instance": { "type": "string" }
              }
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Unauthorized",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "NotFound": {
        "description": "Not found",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "Conflict": {
        "description": "Conflict (e.g. duplicate Idempotency-Key replay with different body)",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "RateLimited": {
        "description": "Rate limited",
        "headers": {
          "Retry-After": {
            "schema": { "type": "integer" },
            "description": "Seconds to wait before retrying"
          },
          "RateLimit-Limit": {
            "schema": { "type": "string" }
          },
          "RateLimit-Remaining": {
            "schema": { "type": "string" }
          }
        },
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      }
    }
  },
  "security": [
    { "bearerAuth": [] },
    { "oauth2": ["mcp:use"] }
  ],
  "externalDocs": {
    "description": "JoAi API docs",
    "url": "https://docs.joai.ai/api"
  }
}
