query_database #

View this page as Markdown

On this page

Generated automatically from the published contract sources.

A script calls this action over HTTPS at POST https://tzcloud.dev.turnzero.ai/v1/query_database, with a bearer credential and the action's payload as the JSON body. It also accepts GET. An MCP connection lists this action as the tool tzcloud_query_database.

OAuth access tokens and minted tokens, including the turnzero command's computer token, are bearer credentials. The action determines which kinds, scopes and grants it accepts (Sign-in, sessions, and tokens).

Build metadata: Registered in this build. Registration describes the default dispatcher in this build. It does not guarantee that a caller has the required credential or grant, that a tool is listed for that connection, or that the required service is configured.

Contract description #

Run one read-only SQL statement against your application's database and read a bounded page of its rows. The statement starts with SELECT, WITH, VALUES, or TABLE. It runs in a read-only transaction that is always rolled back, with a 10-second timeout. Rows are PostgreSQL's text or null, bounded by limit and 64 KiB. truncated (rows or bytes) means more rows remained: narrow with WHERE, LIMIT or fewer columns, or, for rows, raise limit up to 1,000. A write is refused query_not_read_only. Any other failure of the statement or its session is refused query_failed. Without environment, the call reads production.

More about this action #

The statement runs as a cursor's query, in a new database session of its own. It runs under the environment's own database role, the role your application runs as. The platform reads the role's credential from its own custody and never returns it. After it opens, the session uses one connection under the environment role's limit for up to 15 seconds. This can leave one fewer connection for a deploy. The query can read any data that role can read, so confirm the application and environment before querying production.

Send one statement per call, with no bound parameters. A second statement after a semicolon, or a SELECT INTO, is refused query_failed with SQLSTATE 42601. EXPLAIN, SHOW, and COPY are not accepted; read a setting with current_setting('<name>'). A write anywhere beneath the statement, in a function it calls or through nextval(), is refused 400 query_not_read_only, SQLSTATE 25006 or 0A000 from the cursor, and changes nothing.

A statement that runs past the 10-second timeout is refused query_failed with SQLSTATE 57014. Where all of the role's connections are in use, the refusal has SQLSTATE 53300: call again once your application's pool frees one. Any other SQLSTATE the server returns comes back as sqlstate, with the server's message cut to 300 characters, at status 422. A failure with no SQLSTATE, such as a connect timeout, a TLS failure, or a network failure, is refused query_failed at status 503 with a fixed sentence.

Values come back as PostgreSQL writes them as text: numbers, dates, JSON, and bytea (as \x hex) keep every digit. A duplicate column name stays as it is. The result is cut at limit rows or at 64 KiB of row data, whichever comes first. A read that would pass 256 KiB on the network ends at once with the rows read so far.

The environment argument: The value development reads the development database. For an application with one environment, that is the database the local run uses. An environment with no database is refused not_found. If the manifest declares a database and the development database is missing, submit the manifest again to provision it.

If the platform cannot read the saved database credential, the call returns HTTP 502 with database_credential_unreadable. No session opens. Follow the detail for that environment.

Neither the statement nor any value it reads is written to the platform's log.

Where truncated is rows, you can raise limit, up to 1,000, or narrow the query. Production's database is made at the first promote on two environments, or the first production deploy on one. Before then, name development for the development database or a one-environment application's local-run database.

Access and action metadata #

{
  "name": "query_database",
  "resource": "environment",
  "tier": "observe",
  "surface": "tzcloud",
  "addresses": [
    "tzcloud"
  ],
  "product": "cloud",
  "summary": "Run one read-only SQL statement against an environment's database and read a bounded page of its rows. The statement's first word is SELECT, WITH, VALUES, or TABLE, and it runs as a cursor's query on a session of its own, under the environment's own database role, in a read-only transaction that is always rolled back, with a 10-second statement timeout. The answer carries each column's name, at most `limit` rows (100 where none is given, 1,000 at most) and 64 KiB of row data, each value in PostgreSQL's text form or null, `truncated` (`false`, `rows`, or `bytes`), and the time taken. A write is refused `query_not_read_only`; any other failure of the statement or its session is refused `query_failed`. Only the application's own account reaches the action. `environment` is production where none is given; `development` reads the development database, a one-environment application's local-run database included. Neither the statement nor any value it reads is written to the platform's log.",
  "annotations": {
    "readOnlyHint": true,
    "destructiveHint": false,
    "openWorldHint": false,
    "idempotentHint": true
  }
}

MCP catalog entry #

{
  "name": "query_database",
  "title": "Query app database",
  "tier": "observe",
  "scenario": "CHI-L0-09",
  "summary": "Run one read-only SQL statement against your application's database and read a bounded page of its rows. The statement starts with SELECT, WITH, VALUES, or TABLE. It runs in a read-only transaction that is always rolled back, with a 10-second timeout. Rows are PostgreSQL's text or null, bounded by `limit` and 64 KiB. `truncated` (`rows` or `bytes`) means more rows remained: narrow with WHERE, LIMIT or fewer columns, or, for `rows`, raise `limit` up to 1,000. A write is refused `query_not_read_only`. Any other failure of the statement or its session is refused `query_failed`. Without `environment`, the call reads production.",
  "owners": [
    "DBS-L0-11",
    "PLD-L0-40",
    "MAPI-04"
  ]
}

request #

JSON pointer Description and constraints
"" (root) Type: object
Required fields: ["application","sql"]
/properties/application Your account's application id from list_applications, not its name or label.

Type: string
/properties/sql One SQL statement starting with SELECT, WITH, VALUES, or TABLE, at most 10,000 characters, with no bound parameters. A write is refused query_not_read_only; a second statement or a SELECT INTO is refused query_failed.

Type: string
Minimum length: 1
Maximum length: 10000
/properties/environment Production where absent, after its first promote (two environments) or production deploy (one); development reads development or a one-environment local run's database; another value draws invalid_request, a database not yet made draws not_found.

Type: string
Pattern: ^(development|production)$
/properties/limit How many rows at most, 1 to 1000; 100 where none is given. The answer also stops at 64 KiB of row data, and truncated says which bound cut it.

Type: integer
Minimum: 1
Maximum: 1000

response #

JSON pointer Description and constraints
"" (root) One page of the statement's rows, read under the environment's own database role in a read-only transaction that was rolled back. Each value is PostgreSQL's text output, or null for SQL NULL. truncated is rows where more rows remained past limit. It is bytes where the 64 KiB of row data, or the read's 256 KiB on the wire, cut the page, and false where the page is whole.

Type: object
Required fields: ["contract_version","application","environment","columns","rows","row_count","truncated","elapsed_ms","summary"]
/properties/contract_version Required value: 1
/properties/reference The short reference the platform recorded your call under: ten lowercase hexadecimal characters, the value in the call’s record row. Quote it when you report the call.

Type: string
Pattern: ^[0-9a-f]{10}$
/properties/application Type: string
/properties/environment Type: string
Allowed values: ["development","production"]
/properties/columns Each column's name, in order, duplicates kept.

Type: array
/properties/columns/items Type: string
/properties/rows One array per row, its values in the order of columns.

Type: array
/properties/rows/items Type: array
/properties/rows/items/items Type: ["string","null"]
/properties/row_count Type: integer
Minimum: 0
/properties/truncated Allowed values: [false,"rows","bytes"]
/properties/elapsed_ms Type: integer
Minimum: 0
/properties/summary Type: string

Complete payload contract #

{
  "request": {
    "type": "object",
    "required": [
      "application",
      "sql"
    ],
    "properties": {
      "application": {
        "type": "string",
        "description": "Your account's application `id` from `list_applications`, not its name or label."
      },
      "sql": {
        "type": "string",
        "minLength": 1,
        "maxLength": 10000,
        "description": "One SQL statement starting with SELECT, WITH, VALUES, or TABLE, at most 10,000 characters, with no bound parameters. A write is refused `query_not_read_only`; a second statement or a SELECT INTO is refused `query_failed`."
      },
      "environment": {
        "type": "string",
        "pattern": "^(development|production)$",
        "description": "Production where absent, after its first promote (two environments) or production deploy (one); `development` reads development or a one-environment local run's database; another value draws `invalid_request`, a database not yet made draws `not_found`."
      },
      "limit": {
        "type": "integer",
        "minimum": 1,
        "maximum": 1000,
        "description": "How many rows at most, 1 to 1000; 100 where none is given. The answer also stops at 64 KiB of row data, and `truncated` says which bound cut it."
      }
    }
  },
  "response": {
    "type": "object",
    "description": "One page of the statement's rows, read under the environment's own database role in a read-only transaction that was rolled back. Each value is PostgreSQL's text output, or null for SQL NULL. `truncated` is `rows` where more rows remained past `limit`. It is `bytes` where the 64 KiB of row data, or the read's 256 KiB on the wire, cut the page, and false where the page is whole.",
    "required": [
      "contract_version",
      "application",
      "environment",
      "columns",
      "rows",
      "row_count",
      "truncated",
      "elapsed_ms",
      "summary"
    ],
    "properties": {
      "contract_version": {
        "const": 1
      },
      "reference": {
        "type": "string",
        "pattern": "^[0-9a-f]{10}$",
        "description": "The short reference the platform recorded your call under: ten lowercase hexadecimal characters, the value in the call’s record row. Quote it when you report the call."
      },
      "application": {
        "type": "string"
      },
      "environment": {
        "type": "string",
        "enum": [
          "development",
          "production"
        ]
      },
      "columns": {
        "type": "array",
        "items": {
          "type": "string"
        },
        "description": "Each column's name, in order, duplicates kept."
      },
      "rows": {
        "type": "array",
        "items": {
          "type": "array",
          "items": {
            "type": [
              "string",
              "null"
            ]
          }
        },
        "description": "One array per row, its values in the order of `columns`."
      },
      "row_count": {
        "type": "integer",
        "minimum": 0
      },
      "truncated": {
        "enum": [
          false,
          "rows",
          "bytes"
        ]
      },
      "elapsed_ms": {
        "type": "integer",
        "minimum": 0
      },
      "summary": {
        "type": "string"
      }
    }
  }
}

Shared contracts #