{
  "openapi": "3.1.0",
  "info": {
    "title": "PostgreSQL Database API",
    "description": "REST API for querying PostgreSQL databases with table inspection and SELECT query capabilities",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://mcp.botguru.eu",
      "description": "Production server"
    }
  ],
  "paths": {
    "/tables": {
      "get": {
        "operationId": "listTables",
        "summary": "List all tables in a schema",
        "description": "Returns a list of all tables in the specified schema (defaults to 'public')",
        "parameters": [
          {
            "name": "schema",
            "in": "query",
            "description": "Database schema name",
            "required": false,
            "schema": {
              "type": "string",
              "default": "public"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "List of tables",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "schema": {
                      "type": "string"
                    },
                    "tables": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "table_name": {
                            "type": "string"
                          },
                          "table_type": {
                            "type": "string"
                          }
                        }
                      }
                    },
                    "count": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/tables/{tableName}": {
      "get": {
        "operationId": "inspectTable",
        "summary": "Get detailed table structure",
        "description": "Returns column definitions, primary keys, and foreign keys for the specified table",
        "parameters": [
          {
            "name": "tableName",
            "in": "path",
            "description": "Name of the table to inspect",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "schema",
            "in": "query",
            "description": "Database schema name",
            "required": false,
            "schema": {
              "type": "string",
              "default": "public"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Table structure details",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "schema": {
                      "type": "string"
                    },
                    "table": {
                      "type": "string"
                    },
                    "columns": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "column_name": {
                            "type": "string"
                          },
                          "data_type": {
                            "type": "string"
                          },
                          "character_maximum_length": {
                            "type": "integer",
                            "nullable": true
                          },
                          "is_nullable": {
                            "type": "string"
                          },
                          "column_default": {
                            "type": "string",
                            "nullable": true
                          }
                        }
                      }
                    },
                    "primary_keys": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "foreign_keys": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "column_name": {
                            "type": "string"
                          },
                          "foreign_table_name": {
                            "type": "string"
                          },
                          "foreign_column_name": {
                            "type": "string"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Table not found"
          }
        }
      }
    },
    "/search": {
      "get": {
        "operationId": "searchSchema",
        "summary": "Search for tables and columns",
        "description": "Find tables and columns whose names match a keyword within the selected schema.",
        "parameters": [
          {
            "name": "query",
            "in": "query",
            "description": "Search keyword used to match against table and column names",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "schema",
            "in": "query",
            "description": "Database schema name",
            "required": false,
            "schema": {
              "type": "string",
              "default": "public"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Search results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "schema": {
                      "type": "string"
                    },
                    "query": {
                      "type": "string"
                    },
                    "tables": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "table_name": {
                            "type": "string"
                          },
                          "table_type": {
                            "type": "string"
                          }
                        }
                      }
                    },
                    "columns": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "table_name": {
                            "type": "string"
                          },
                          "column_name": {
                            "type": "string"
                          },
                          "data_type": {
                            "type": "string"
                          }
                        }
                      }
                    },
                    "tableCount": {
                      "type": "integer"
                    },
                    "columnCount": {
                      "type": "integer"
                    },
                    "matches": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string"
                          },
                          "schema": {
                            "type": "string"
                          },
                          "table": {
                            "type": "string"
                          },
                          "table_type": {
                            "type": "string"
                          },
                          "column_matches": {
                            "type": "array",
                            "items": {
                              "type": "object",
                              "properties": {
                                "column_name": {
                                  "type": "string"
                                },
                                "data_type": {
                                  "type": "string"
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid request"
          }
        }
      }
    },
    "/query": {
      "post": {
        "operationId": "executeQuery",
        "summary": "Execute a SELECT query",
        "description": "Executes a SELECT SQL query and returns the results. Only SELECT queries are allowed for security.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["query"],
                "properties": {
                  "query": {
                    "type": "string",
                    "description": "SELECT SQL query to execute"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Query results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "rows": {
                      "type": "array",
                      "items": {
                        "type": "object"
                      }
                    },
                    "rowCount": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid query (not a SELECT statement)"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "QueryToken": {
        "type": "apiKey",
        "in": "query",
        "name": "token",
        "description": "API token passed as query parameter"
      }
    }
  },
  "security": [
    {
      "QueryToken": []
    }
  ]
}
