Skip to main content
Glama

db_query

SQL query, run SQL, SELECT, INSERT, UPDATE, DELETE, execute SQL against the project database — the read/write-ROWS entry point. A documented Postgres-flavored syntax subset is translated; the default database keeps its own expression, typing, comparison, and ordering semantics. For example, division by zero returns NULL rather than throwing. The database is created on first use, but tables are not — new projects declare them in db/schema.ts and deploy raw source; db_migrate is for SQL-mode projects. Raw SQL reaches the project's raw SQL database only; on a project that keeps its tables in the managed database, rows are read with db_browse (plane: "managed"), schema with db_describe, and writes go through sw.db in functions.

Schema changes (CREATE / ALTER / DROP) belong in db/schema.ts plus deploy for managed projects, or db_migrate for SQL-mode projects, not here, and not inline in a deployed function's handler: runtime DDL runs on every request, isn't versioned, and hides new tables from the platform's per-user data protections (a table with an owner_id/user_id column is cross-user readable until it's scoped). db_query is for the rows.

Every successful query returns { data: [], count: N, changes: N, last_row_id: X }: data contains returned rows, count counts those rows, and changes counts affected rows. A write without RETURNING has empty data and a zero count even when it changes rows. last_row_id is a number, an exact decimal integer string when the ID exceeds JavaScript’s safe integer range, or null; other transport metadata may also be present.

Example:

{
  "project_id": "my-saas",
  "sql": "SELECT id, email FROM users WHERE created_at > ? ORDER BY created_at DESC LIMIT 20",
  "params": [1704067200]
}

The database has a 30-second hard query ceiling. timeout_ms bounds how long this call waits (default 30000, max 30000) and returns QUERY_TIMEOUT; it does not promise cancellation of work already accepted by the database.

Tool call template

Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.

{
  "id": 1,
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "arguments": {
      "project_id": "<project_id>",
      "sql": "<sql>"
    },
    "name": "db_query"
  }
}

Input Schema

NameRequiredDescriptionDefault
sqlYesSQL to execute. User input is represented by bound ? placeholders with params rather than string concatenation.
draftNoAlias for the preview target flag.
paramsNoBind parameters for ? placeholders. A native JSON array such as ["alice@example.com", 42] is preferred; a JSON-stringified array is also accepted. The field is optional when no placeholders exist.
targetNoDatabase target. Preview requires preview_session_id (or draft_id).
previewNoExplicit preview target flag. Cannot be combined with a production target.
draft_idNoAlias for preview_session_id.
preview_idNoOptional expected preview candidate release id.
productionNoExplicit production target flag. Cannot be combined with a preview target.
project_idYesProject ID (UUID), subdomain, or slug — resolved server-side, no UUID lookup needed. 'default' also works when the account has exactly one project; multi-project accounts must name one (the error lists them).
timeout_msNoStop waiting after N ms. Default 30000. Clamped to [1, 30000]. Returns QUERY_TIMEOUT; already-accepted database work may continue up to the hard ceiling.
preview_session_idNoIdentifier for an active preview session's isolated database.
candidate_release_idNoAlias for preview_id.

Output Schema

NameRequiredDescriptionDefault
okYes
dataYes

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

A4.4/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Goes well beyond the annotations (readOnlyHint=false, idempotentHint=false, destructiveHint=false) by disclosing the 30-second hard query ceiling, that timeout_ms does not cancel accepted work, that division by zero returns NULL, that databases are created on first use but tables are not, and the cross-user exposure risk of runtime DDL. The one gap is that a mutation-capable tool never states permission or auth requirements for writes. Nothing contradicts the annotations; the DDL prohibition is consistent with destructiveHint=false.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Front-loaded with the verb and the tool's role, then organized by routing, DDL hazards, return shape, and timeout — a sensible order for a 12-parameter tool. It is long and carries mild redundancy, restating the 30s ceiling and the db_migrate rule more than once, but nearly every sentence carries operational information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Complete for a high-complexity tool: routing rules, DDL exclusions, timeout behavior, and write semantics are all covered. Since an output schema exists, the explicit return-shape paragraph is surplus but harmless, and nothing an agent needs to call this correctly is missing.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100% with per-parameter descriptions, so the baseline is 3. The definition adds a worked example showing project_id/sql/params binding and restates the 30-second ceiling around timeout_ms, but the placeholder, params, and timeout semantics are already spelled out in the schema, so the added meaning is modest.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

States a specific verb and resource ("SQL query, run SQL, SELECT, INSERT, UPDATE, DELETE, execute SQL against the project database") and immediately scopes it as "the read/write-ROWS entry point," which separates it from schema-mutation siblings. It further names db_migrate, db_describe, and db_browse as the tools for the adjacent jobs, so an agent can pick correctly without opening another schema.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicit when-not guidance: CREATE/ALTER/DROP "belong in `db/schema.ts` plus deploy for managed projects, or db_migrate for SQL-mode projects, not here." It also defines the routing boundary by project type — raw SQL reaches the raw SQL database only, and managed-database rows are read with db_browse (plane: "managed"). Both the alternative and the selecting condition are named.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Try in Browser

Glama MCP Gateway

Add one secure layer between your agents and this server.

Resources