{
  "openapi": "3.1.0",
  "info": {
    "title": "Trinity agent API",
    "version": "1.0.0",
    "summary": "Get a token for a coding agent and call Trinity's MCP server.",
    "description": "Trinity's agent surface is one MCP server plus the endpoints that issue its token. An agent starts a device authorization, the person it works for approves it once in the browser, and the agent exchanges it for a bearer token. The token opens the MCP server at /api/v1/agent/mcp. Plain-text walkthrough: https://app.usetrinity.ai/agent-setup/other.txt",
    "contact": {
      "name": "Trinity",
      "email": "hi@usetrinity.ai",
      "url": "https://usetrinity.ai"
    }
  },
  "servers": [
    {
      "url": "https://app.usetrinity.ai"
    }
  ],
  "tags": [
    {
      "name": "Device authorization",
      "description": "Get a bearer token with one browser approval."
    },
    {
      "name": "MCP",
      "description": "The MCP server a token opens."
    }
  ],
  "paths": {
    "/api/v1/devices/authorize/start": {
      "post": {
        "operationId": "startDeviceAuthorization",
        "tags": [
          "Device authorization"
        ],
        "summary": "Start a device authorization",
        "description": "Opens a request the person approves at verificationUrl, where the page shows verificationCode. Keep deviceCode secret: it is what the exchange trades for a token. Limited to ten starts a minute per IP address.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/StartDeviceAuthorization"
              },
              "example": {
                "tool": "other",
                "name": "research-agent"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The request is open.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeviceAuthorization"
                },
                "example": {
                  "deviceCode": "EXAMPLEDEVICECODE23456723456723456723456723456723456",
                  "verificationUrl": "https://app.usetrinity.ai/?device_request=7c9e6679-7425-40de-944b-e07fc1f90ae7",
                  "verificationCode": "7C9E-6679",
                  "expiresInSeconds": 600,
                  "intervalSeconds": 2
                }
              }
            }
          },
          "400": {
            "description": "The body is not JSON, or name is empty.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "name is required"
                }
              }
            }
          },
          "422": {
            "description": "tool is not one Trinity knows, or name is longer than 255 characters.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "tool \"vim\" has no registered dialect"
                }
              }
            }
          },
          "429": {
            "description": "Too many starts from this IP address in the last minute, or too many open requests.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "too many device authorization requests"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/devices/authorize/exchange": {
      "post": {
        "operationId": "exchangeDeviceAuthorization",
        "tags": [
          "Device authorization"
        ],
        "summary": "Exchange an approved authorization for a token",
        "description": "Poll every intervalSeconds until the person approves. 202 means not yet. 200 answers the token once; the authorization is spent. Pairing the same machine again (same organization, person, tool and machineId) reuses its device and rotates the token.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ExchangeDeviceAuthorization"
              },
              "example": {
                "deviceCode": "EXAMPLEDEVICECODE23456723456723456723456723456723456",
                "hostname": "research-agent",
                "machineId": "3671e743ddc3bdbb88ac351167a4d030ab2790250aefbc70b6f542169fbdfc4a"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Approved. Store the token as a secret.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeviceToken"
                },
                "example": {
                  "token": "<secret>",
                  "deviceId": "3f2b8c1e-5d4a-4e6f-9a7b-1c2d3e4f5a6b",
                  "ingestUrl": "https://api.usetrinity.ai/api/v1/ingest/batches",
                  "mcpUrl": "https://api.usetrinity.ai/api/v1/agent/mcp"
                }
              }
            }
          },
          "202": {
            "description": "The person has not approved the request yet. Poll again after intervalSeconds.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pending"
                },
                "example": {
                  "status": "pending"
                }
              }
            }
          },
          "400": {
            "description": "deviceCode is malformed, or machineId is not 64 lowercase hex characters.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "malformed device code"
                }
              }
            }
          },
          "402": {
            "description": "The organization has no work access: its free period ended or its subscription lapsed. The error names the reason.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "free_access_expired"
                }
              }
            }
          },
          "409": {
            "description": "The organization is being deleted.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "organization_deleting"
                }
              }
            }
          },
          "404": {
            "description": "The authorization expired or was already spent. Start again. A request that expires after the person tried to approve it can mean the organization's free period ended or its subscription lapsed: approval is refused until an owner subscribes.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "unknown or expired device authorization"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/devices/authorize/pairing-code": {
      "post": {
        "operationId": "claimDeviceAuthorizationPairingCode",
        "tags": [
          "Device authorization"
        ],
        "summary": "Exchange an approved authorization for a pairing code",
        "description": "For an agent that hands the pairing to a Trinity capture plugin: instead of a token, an approved authorization answers a pairing code the plugin exchanges at /api/v1/devices/exchange. The authorization is spent.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ClaimPairingCode"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Approved.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PairingCode"
                },
                "example": {
                  "code": "EXAMPLECODE23",
                  "expiresInSeconds": 600
                }
              }
            }
          },
          "202": {
            "description": "The person has not approved the request yet. Poll again after intervalSeconds.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pending"
                },
                "example": {
                  "status": "pending"
                }
              }
            }
          },
          "400": {
            "description": "deviceCode is malformed.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "malformed device code"
                }
              }
            }
          },
          "402": {
            "description": "The organization has no work access: its free period ended or its subscription lapsed. The error names the reason.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "free_access_expired"
                }
              }
            }
          },
          "409": {
            "description": "The organization is being deleted.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "organization_deleting"
                }
              }
            }
          },
          "404": {
            "description": "The authorization expired or was already spent. A request that expires after the person tried to approve it can mean the organization's free period ended or its subscription lapsed: approval is refused until an owner subscribes.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "unknown or expired device authorization"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/devices/exchange": {
      "post": {
        "operationId": "exchangePairingCode",
        "tags": [
          "Device authorization"
        ],
        "summary": "Exchange a pairing code for a token",
        "description": "A pairing code comes from the Trinity dashboard's setup prompt or from claimDeviceAuthorizationPairingCode. It lasts ten minutes and works once.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ExchangePairingCode"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Paired. Store the token as a secret.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeviceToken"
                }
              }
            }
          },
          "400": {
            "description": "code is malformed, or machineId is not 64 lowercase hex characters.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "malformed pairing code"
                }
              }
            }
          },
          "402": {
            "description": "The organization has no work access: its free period ended or its subscription lapsed. The error names the reason.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "free_access_expired"
                }
              }
            }
          },
          "409": {
            "description": "The organization is being deleted.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "organization_deleting"
                }
              }
            }
          },
          "404": {
            "description": "The code is unknown, used or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "unknown, used or expired pairing code"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/agent/mcp": {
      "post": {
        "operationId": "callMcp",
        "tags": [
          "MCP"
        ],
        "summary": "MCP server (Streamable HTTP, stateless)",
        "description": "Send MCP JSON-RPC messages: initialize, tools/list, tools/call. Send Accept: application/json, text/event-stream. Each tool answers one text block holding JSON. Reads: get_task_context, get_task, find_tasks, get_milestone, list_milestones, read_source. Writes, made as the person who approved the token: start_task, report_progress, link_pull_request, propose_task, ask_blocker, finish_task. tools/list gives each tool's input schema.",
        "security": [
          {
            "deviceToken": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/JsonRpcRequest"
              },
              "example": {
                "jsonrpc": "2.0",
                "id": 1,
                "method": "tools/list",
                "params": {}
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The JSON-RPC answer, as JSON or as one server-sent event.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              },
              "text/event-stream": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "401": {
            "description": "No token, or a token Trinity does not know. Pair again.",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "403": {
            "description": "The device was revoked on the dashboard. Pair again.",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "deviceToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "The token an exchange answered. Send Authorization: Bearer <token>."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string",
            "description": "What went wrong, in one line."
          }
        }
      },
      "Pending": {
        "type": "object",
        "required": [
          "status"
        ],
        "properties": {
          "status": {
            "const": "pending"
          }
        }
      },
      "StartDeviceAuthorization": {
        "type": "object",
        "required": [
          "tool",
          "name"
        ],
        "properties": {
          "tool": {
            "type": "string",
            "enum": [
              "claude_code",
              "codex",
              "cursor",
              "gemini_cli",
              "copilot_cli",
              "other"
            ],
            "description": "The agent asking. Use other for any MCP client without a Trinity plugin."
          },
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 255,
            "description": "A short name the person sees on the approval screen and on the device."
          }
        }
      },
      "DeviceAuthorization": {
        "type": "object",
        "required": [
          "deviceCode",
          "verificationUrl",
          "verificationCode",
          "expiresInSeconds",
          "intervalSeconds"
        ],
        "properties": {
          "deviceCode": {
            "type": "string",
            "minLength": 52,
            "maxLength": 52,
            "description": "Secret. Never show it to the person or log it."
          },
          "verificationUrl": {
            "type": "string",
            "format": "uri",
            "description": "Where the person approves the request."
          },
          "verificationCode": {
            "type": "string",
            "pattern": "^[0-9A-F]{4}-[0-9A-F]{4}$",
            "description": "Shown on the approval screen so the person can match it."
          },
          "expiresInSeconds": {
            "type": "integer"
          },
          "intervalSeconds": {
            "type": "integer",
            "description": "How long to wait between exchange polls."
          }
        }
      },
      "MachineId": {
        "type": "string",
        "pattern": "^[0-9a-f]{64}$",
        "description": "A Trinity-specific digest, in lowercase hex, of a stable identifier of this machine or workspace. Never send the raw identifier and never hash a secret. Two agents must never send the same one: pairing a machine again takes over its device and token. The quickstart (https://app.usetrinity.ai/docs/) has a recipe."
      },
      "ExchangeDeviceAuthorization": {
        "type": "object",
        "required": [
          "deviceCode",
          "machineId"
        ],
        "properties": {
          "deviceCode": {
            "type": "string",
            "minLength": 52,
            "maxLength": 52
          },
          "hostname": {
            "type": "string",
            "description": "The device's name on the dashboard."
          },
          "machineId": {
            "$ref": "#/components/schemas/MachineId"
          }
        }
      },
      "ClaimPairingCode": {
        "type": "object",
        "required": [
          "deviceCode"
        ],
        "properties": {
          "deviceCode": {
            "type": "string",
            "minLength": 52,
            "maxLength": 52
          }
        }
      },
      "PairingCode": {
        "type": "object",
        "required": [
          "code",
          "expiresInSeconds"
        ],
        "properties": {
          "code": {
            "type": "string",
            "minLength": 13,
            "maxLength": 13
          },
          "expiresInSeconds": {
            "type": "integer"
          }
        }
      },
      "ExchangePairingCode": {
        "type": "object",
        "required": [
          "code",
          "machineId"
        ],
        "properties": {
          "code": {
            "type": "string",
            "minLength": 13,
            "maxLength": 13
          },
          "hostname": {
            "type": "string"
          },
          "machineId": {
            "$ref": "#/components/schemas/MachineId"
          }
        }
      },
      "DeviceToken": {
        "type": "object",
        "required": [
          "token",
          "deviceId",
          "ingestUrl",
          "mcpUrl"
        ],
        "properties": {
          "token": {
            "type": "string",
            "description": "Bearer token for the MCP server. Secret; it lasts until the device is revoked."
          },
          "deviceId": {
            "type": "string",
            "format": "uuid"
          },
          "ingestUrl": {
            "type": "string",
            "format": "uri",
            "description": "Where Trinity's capture plugins send session events."
          },
          "mcpUrl": {
            "type": "string",
            "format": "uri",
            "description": "The MCP server this token opens."
          }
        }
      },
      "JsonRpcRequest": {
        "type": "object",
        "required": [
          "jsonrpc",
          "method"
        ],
        "properties": {
          "jsonrpc": {
            "const": "2.0"
          },
          "id": {
            "type": [
              "integer",
              "string"
            ]
          },
          "method": {
            "type": "string",
            "examples": [
              "initialize",
              "tools/list",
              "tools/call"
            ]
          },
          "params": {
            "type": "object"
          }
        }
      }
    }
  }
}
