{
  "openapi": "3.1.0",
  "info": {
    "title": "Diakrin agent connection API",
    "version": "0.3.0",
    "summary": "How an AI agent signs a person in to Diakrin and calls its MCP server.",
    "description": "Diakrin's actions are tools on a Model Context Protocol server (Streamable HTTP). This document covers connecting to it: discovery, OAuth 2.1 with PKCE for public clients, and the MCP endpoint. List the tools with the MCP `tools/list` method after signing in; what is listed depends on the person's organization and role. Setup guide: https://diakrin.com/docs/builders.md",
    "contact": {
      "email": "support@diakrin.com",
      "url": "https://diakrin.com"
    },
    "termsOfService": "https://diakrin.com/terms"
  },
  "externalDocs": {
    "description": "MCP tool reference",
    "url": "https://diakrin.com/docs/mcp-tools.md"
  },
  "servers": [
    {
      "url": "https://api.diakrin.com"
    }
  ],
  "tags": [
    {
      "name": "mcp",
      "description": "Model Context Protocol"
    },
    {
      "name": "oauth",
      "description": "Sign-in for agents"
    }
  ],
  "paths": {
    "/mcp": {
      "post": {
        "operationId": "callMcp",
        "tags": [
          "mcp"
        ],
        "summary": "Send a Model Context Protocol message",
        "security": [
          {
            "diakrinOAuth": [
              "builder"
            ]
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/JsonRpcMessage"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The JSON-RPC response, as JSON or as an event stream.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/JsonRpcMessage"
                }
              },
              "text/event-stream": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "401": {
            "description": "No valid access token. `WWW-Authenticate` names the protected resource metadata.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            },
            "headers": {
              "WWW-Authenticate": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "403": {
            "description": "The token or the browser origin may not do this.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiError"
                }
              }
            }
          }
        }
      }
    },
    "/.well-known/oauth-protected-resource/mcp": {
      "get": {
        "operationId": "getProtectedResourceMetadata",
        "tags": [
          "oauth"
        ],
        "summary": "Protected resource metadata for the MCP server (RFC 9728)",
        "security": [],
        "responses": {
          "200": {
            "description": "Metadata.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "resource": {
                      "type": "string",
                      "format": "uri"
                    },
                    "authorization_servers": {
                      "type": "array",
                      "items": {
                        "type": "string",
                        "format": "uri"
                      }
                    },
                    "scopes_supported": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "bearer_methods_supported": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  },
                  "required": [
                    "resource",
                    "authorization_servers",
                    "scopes_supported",
                    "bearer_methods_supported"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/.well-known/oauth-authorization-server": {
      "get": {
        "operationId": "getAuthorizationServerMetadata",
        "tags": [
          "oauth"
        ],
        "summary": "Authorization server metadata (RFC 8414)",
        "security": [],
        "responses": {
          "200": {
            "description": "Metadata.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "issuer": {
                      "type": "string",
                      "format": "uri"
                    },
                    "authorization_endpoint": {
                      "type": "string",
                      "format": "uri"
                    },
                    "token_endpoint": {
                      "type": "string",
                      "format": "uri"
                    },
                    "registration_endpoint": {
                      "type": "string",
                      "format": "uri"
                    },
                    "revocation_endpoint": {
                      "type": "string",
                      "format": "uri"
                    },
                    "code_challenge_methods_supported": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "scopes_supported": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  },
                  "required": [
                    "issuer",
                    "authorization_endpoint",
                    "token_endpoint",
                    "registration_endpoint",
                    "revocation_endpoint",
                    "code_challenge_methods_supported",
                    "scopes_supported"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/oauth/register": {
      "post": {
        "operationId": "registerClient",
        "tags": [
          "oauth"
        ],
        "summary": "Register a public client (RFC 7591)",
        "description": "Loopback redirect URIs (RFC 8252) are accepted; other HTTPS redirect URIs must be on Diakrin's allow list.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "redirect_uris": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "uri"
                    },
                    "minItems": 1,
                    "maxItems": 5
                  },
                  "client_name": {
                    "type": "string",
                    "maxLength": 200
                  },
                  "token_endpoint_auth_method": {
                    "const": "none"
                  },
                  "grant_types": {
                    "type": "array",
                    "items": {
                      "enum": [
                        "authorization_code",
                        "refresh_token"
                      ]
                    }
                  },
                  "response_types": {
                    "type": "array",
                    "items": {
                      "const": "code"
                    }
                  },
                  "scope": {
                    "const": "builder"
                  }
                },
                "required": [
                  "redirect_uris"
                ]
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "The registered client.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "client_id": {
                      "type": "string"
                    },
                    "client_id_issued_at": {
                      "type": "integer"
                    },
                    "client_name": {
                      "type": "string"
                    },
                    "redirect_uris": {
                      "type": "array",
                      "items": {
                        "type": "string",
                        "format": "uri"
                      }
                    },
                    "token_endpoint_auth_method": {
                      "type": "string"
                    },
                    "grant_types": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "response_types": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "scope": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "client_id",
                    "client_id_issued_at",
                    "client_name",
                    "redirect_uris",
                    "token_endpoint_auth_method",
                    "grant_types",
                    "response_types",
                    "scope"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Invalid client metadata or redirect URI.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OAuthError"
                }
              }
            }
          },
          "429": {
            "description": "Too many registrations.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OAuthError"
                }
              }
            }
          }
        }
      }
    },
    "/oauth/authorize": {
      "get": {
        "operationId": "authorize",
        "tags": [
          "oauth"
        ],
        "summary": "Start sign-in in the person's browser",
        "description": "A person approves the connection on the Diakrin console. Agents cannot approve it themselves.",
        "security": [],
        "parameters": [
          {
            "name": "response_type",
            "in": "query",
            "required": true,
            "schema": {
              "const": "code"
            },
            "description": "Authorization code flow."
          },
          {
            "name": "client_id",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "A registered client ID or a client ID metadata document URL."
          },
          {
            "name": "redirect_uri",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uri"
            },
            "description": "A registered redirect URI."
          },
          {
            "name": "code_challenge",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[A-Za-z0-9_-]{43}$"
            },
            "description": "PKCE challenge."
          },
          {
            "name": "code_challenge_method",
            "in": "query",
            "required": true,
            "schema": {
              "const": "S256"
            },
            "description": "PKCE method."
          },
          {
            "name": "state",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 512
            },
            "description": "Returned on the redirect."
          },
          {
            "name": "scope",
            "in": "query",
            "required": false,
            "schema": {
              "const": "builder"
            },
            "description": "Requested scope."
          },
          {
            "name": "resource",
            "in": "query",
            "required": false,
            "schema": {
              "const": "https://api.diakrin.com/mcp"
            },
            "description": "Resource indicator (RFC 8707)."
          }
        ],
        "responses": {
          "302": {
            "description": "To the consent page, or back to the redirect URI with `error`.",
            "headers": {
              "Location": {
                "schema": {
                  "type": "string",
                  "format": "uri"
                }
              }
            }
          },
          "400": {
            "description": "Unknown client or redirect URI; no redirect."
          }
        }
      }
    },
    "/oauth/token": {
      "post": {
        "operationId": "issueToken",
        "tags": [
          "oauth"
        ],
        "summary": "Exchange a code, or rotate a refresh token",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "oneOf": [
                  {
                    "type": "object",
                    "properties": {
                      "grant_type": {
                        "const": "authorization_code"
                      },
                      "client_id": {
                        "type": "string"
                      },
                      "code": {
                        "type": "string"
                      },
                      "redirect_uri": {
                        "type": "string",
                        "format": "uri"
                      },
                      "code_verifier": {
                        "type": "string",
                        "minLength": 43,
                        "maxLength": 128
                      },
                      "resource": {
                        "type": "string",
                        "format": "uri"
                      }
                    },
                    "required": [
                      "grant_type",
                      "client_id",
                      "code",
                      "redirect_uri",
                      "code_verifier"
                    ]
                  },
                  {
                    "type": "object",
                    "properties": {
                      "grant_type": {
                        "const": "refresh_token"
                      },
                      "client_id": {
                        "type": "string"
                      },
                      "refresh_token": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "grant_type",
                      "client_id",
                      "refresh_token"
                    ]
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Tokens. A refresh token works once.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "access_token": {
                      "type": "string"
                    },
                    "token_type": {
                      "const": "Bearer"
                    },
                    "expires_in": {
                      "type": "integer"
                    },
                    "refresh_token": {
                      "type": "string"
                    },
                    "scope": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "access_token",
                    "token_type",
                    "expires_in",
                    "refresh_token",
                    "scope"
                  ]
                }
              }
            }
          },
          "400": {
            "description": "The grant or request is not valid.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OAuthError"
                }
              }
            }
          },
          "401": {
            "description": "Unknown client.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OAuthError"
                }
              }
            }
          },
          "503": {
            "description": "Temporarily unavailable.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OAuthError"
                }
              }
            }
          }
        }
      }
    },
    "/oauth/revoke": {
      "post": {
        "operationId": "revokeToken",
        "tags": [
          "oauth"
        ],
        "summary": "Revoke a token (RFC 7009)",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "type": "object",
                "properties": {
                  "token": {
                    "type": "string"
                  },
                  "client_id": {
                    "type": "string"
                  }
                },
                "required": [
                  "token",
                  "client_id"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Revoked, or the token was not known."
          },
          "400": {
            "description": "The request is not valid.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OAuthError"
                }
              }
            }
          },
          "401": {
            "description": "Unknown client.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/OAuthError"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "diakrinOAuth": {
        "type": "oauth2",
        "description": "OAuth 2.1 authorization code with PKCE (S256), public clients only.",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://api.diakrin.com/oauth/authorize",
            "tokenUrl": "https://api.diakrin.com/oauth/token",
            "refreshUrl": "https://api.diakrin.com/oauth/token",
            "scopes": {
              "builder": "Create and manage apps as the signed-in person, within their permissions."
            }
          }
        }
      }
    },
    "schemas": {
      "JsonRpcMessage": {
        "type": "object",
        "properties": {
          "jsonrpc": {
            "const": "2.0"
          },
          "id": {
            "type": [
              "string",
              "integer"
            ]
          },
          "method": {
            "type": "string"
          },
          "params": {
            "type": "object"
          },
          "result": {
            "type": "object"
          },
          "error": {
            "type": "object"
          }
        },
        "required": [
          "jsonrpc"
        ]
      },
      "ApiError": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            },
            "required": [
              "code"
            ]
          }
        },
        "required": [
          "error"
        ]
      },
      "OAuthError": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        },
        "required": [
          "error"
        ]
      }
    }
  }
}
