{
  "openapi": "3.1.0",
  "info": {
    "title": "BuildStability API",
    "description": "API for AI agents and developers to interact with BuildStability fitness business management platform. This specification exposes public and authenticated endpoints for client management, workout programming, and business analytics.",
    "version": "1.0.0",
    "contact": {
      "name": "BuildStability Support",
      "url": "https://buildstability.com/contact",
      "email": "support@buildstability.com"
    },
    "license": {
      "name": "Proprietary",
      "url": "https://buildstability.com/terms"
    }
  },
  "servers": [
    {
      "url": "https://buildstability.com/api",
      "description": "Production API"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/mcp": {
      "get": {
        "operationId": "getMcpServerInfo",
        "summary": "Get MCP Server Information",
        "description": "Returns metadata about the MCP server including available capabilities. No authentication required.",
        "tags": ["MCP"],
        "security": [],
        "responses": {
          "200": {
            "description": "MCP server information",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/McpServerInfo"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "mcpToolCall",
        "summary": "Execute MCP Tool Call",
        "description": "Execute a JSON-RPC 2.0 method call to interact with BuildStability. Requires authentication.",
        "tags": ["MCP"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/JsonRpcRequest"
              },
              "examples": {
                "checkChurnRisk": {
                  "summary": "Check clients at risk of churning",
                  "value": {
                    "jsonrpc": "2.0",
                    "id": 1,
                    "method": "buildstability/check_churn_risk",
                    "params": {
                      "threshold": 40
                    }
                  }
                },
                "listTools": {
                  "summary": "List available tools",
                  "value": {
                    "jsonrpc": "2.0",
                    "id": 1,
                    "method": "tools/list"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/JsonRpcResponse"
                }
              }
            }
          },
          "401": {
            "description": "Authentication required"
          }
        }
      }
    },
    "/mcp/tools": {
      "get": {
        "operationId": "listMcpTools",
        "summary": "List Available MCP Tools",
        "description": "Convenience endpoint to list available tools in a human-readable format.",
        "tags": ["MCP"],
        "security": [],
        "responses": {
          "200": {
            "description": "List of available tools",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ToolsList"
                }
              }
            }
          }
        }
      }
    },
    "/health": {
      "get": {
        "operationId": "healthCheck",
        "summary": "Health Check",
        "description": "Returns the health status of the API.",
        "tags": ["System"],
        "security": [],
        "responses": {
          "200": {
            "description": "API is healthy",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "example": "OK"
                    },
                    "timestamp": {
                      "type": "string",
                      "format": "date-time"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "Supabase JWT token obtained via authentication"
      }
    },
    "schemas": {
      "McpServerInfo": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "example": "buildstability-mcp"
          },
          "version": {
            "type": "string",
            "example": "1.0.0"
          },
          "protocolVersion": {
            "type": "string",
            "example": "2024-11-05"
          },
          "capabilities": {
            "type": "object",
            "properties": {
              "tools": {
                "type": "object"
              }
            }
          },
          "instructions": {
            "type": "string"
          }
        }
      },
      "JsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "id", "method"],
        "properties": {
          "jsonrpc": {
            "type": "string",
            "enum": ["2.0"]
          },
          "id": {
            "oneOf": [
              { "type": "string" },
              { "type": "integer" }
            ]
          },
          "method": {
            "type": "string",
            "description": "The MCP method to call",
            "enum": [
              "tools/list",
              "buildstability/get_client_summary",
              "buildstability/list_active_programs",
              "buildstability/check_churn_risk",
              "buildstability/get_business_stats"
            ]
          },
          "params": {
            "type": "object",
            "description": "Method-specific parameters"
          }
        }
      },
      "JsonRpcResponse": {
        "type": "object",
        "properties": {
          "jsonrpc": {
            "type": "string",
            "enum": ["2.0"]
          },
          "id": {
            "oneOf": [
              { "type": "string" },
              { "type": "integer" },
              { "type": "null" }
            ]
          },
          "result": {
            "description": "The result of the method call (present on success)"
          },
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "integer"
              },
              "message": {
                "type": "string"
              },
              "data": {}
            }
          }
        }
      },
      "ToolsList": {
        "type": "object",
        "properties": {
          "tools": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "description": {
                  "type": "string"
                },
                "parameters": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              }
            }
          },
          "usage": {
            "type": "object",
            "properties": {
              "endpoint": {
                "type": "string"
              },
              "authentication": {
                "type": "string"
              },
              "format": {
                "type": "string"
              },
              "example": {
                "type": "object"
              }
            }
          }
        }
      },
      "Client": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "first_name": {
            "type": "string"
          },
          "last_name": {
            "type": "string"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "status": {
            "type": "string",
            "enum": ["active", "archived", "lead"]
          }
        }
      },
      "EngagementScore": {
        "type": "object",
        "properties": {
          "score": {
            "type": "integer",
            "minimum": 0,
            "maximum": 100,
            "description": "Client engagement score (0-100). Below 40 indicates high churn risk."
          },
          "risk_level": {
            "type": "string",
            "enum": ["low", "moderate", "high"]
          },
          "last_workout": {
            "type": "string",
            "format": "date"
          },
          "workouts_this_week": {
            "type": "integer"
          }
        }
      },
      "ProgramTemplate": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "goal": {
            "type": "string"
          },
          "duration_weeks": {
            "type": "integer"
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "MCP",
      "description": "Model Context Protocol endpoints for AI agent integration"
    },
    {
      "name": "System",
      "description": "System health and status endpoints"
    }
  ]
}