Skip to main content
Glama

Query a session database with SQL

smart_compact_sql
Read-onlyIdempotent

Run read-only SQLite SELECT queries against a session’s stored messages to compute counts, aggregates, and joins that full-text search cannot express.

Instructions

Run one read-only SQLite SELECT (or WITH … SELECT) against a session's database, for counts, aggregates and joins that smart_compact_search cannot express. The database holds every message of the session, in or out of your context. Schema: messages(seq INTEGER PRIMARY KEY, fingerprint, thread, role 'user'|'assistant'|'tool', kind 'text'|'tool_use'|'tool_result'|'thinking', name (tool name or NULL), text, blob_id, in_context 0|1, trimmed_at_seq, trimmed_at, created_at); lines(id, message_seq, line_no (1-based), role, text) with one row per line of each message; messages_fts(text) and lines_fts(text): FTS5 indexes whose rowid is messages.seq / lines.id; vectors(message_seq, model, dim, blob); events(id, at, kind 'trim'|'fail_open'|'usage', version, location, error, tokens_before, tokens_after, dropped, model, thread_root, trimmed 0|1|2, input_tokens, cache_read_tokens, cache_creation_tokens, output_tokens, breakpoints, breakpoint_at), one row per trim, fail-open or model call; trim token figures estimated, usage counts provider-reported (NULL when not reported). Full-text: lines_fts MATCH 'error AND payments' (FTS5 syntax; join lines ON lines.id = lines_fts.rowid); exact substrings: text LIKE '%level=error%' (case-insensitive for ASCII). Examples: (1) lines naming a level and a service: SELECT count() AS n FROM lines WHERE text LIKE '%level=error%' AND text LIKE '%service=payments%'; (2) tool calls per tool: SELECT name, count() AS calls FROM messages WHERE kind = 'tool_use' GROUP BY name ORDER BY calls DESC; (3) error lines per tool result: SELECT m.seq, m.name, count(*) AS errors FROM lines_fts JOIN lines l ON l.id = lines_fts.rowid JOIN messages m ON m.seq = l.message_seq WHERE lines_fts MATCH 'error' AND m.kind = 'tool_result' GROUP BY m.seq ORDER BY errors DESC LIMIT 10. Writes, ATTACH and PRAGMA are rejected; the query stops after 2 s; at most 200 rows return (truncated: true when there were more; long text cells are cut at 2000 characters).

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": {
      "session_id": "<session_id>",
      "sql": "<sql>"
    },
    "name": "smart_compact_sql"
  }
}

Input Schema

NameRequiredDescriptionDefault
sqlYesOne SELECT or WITH statement, at most 10000 characters
session_idYesSession id from the [openmem: continuing session …] pointer

Schema Changelog

Changes observed during successful MCP inspections.

  1. Addedv0.8.0

TDQS

A4.8/5.0
Behavior5/5

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

Beyond annotations, the description discloses critical behavior: writes, ATTACH, and PRAGMA are rejected; queries stop after 2 seconds; at most 200 rows return with truncated=true; and long text cells are cut at 2000 characters. These are exactly the operational constraints an agent needs.

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?

The description is long, but the length is largely justified by the complexity of the database schema and the need for examples. It is front-loaded with purpose and constraints, though the dense schema listing and multiple examples could be organized more readably.

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?

Given the absence of an output schema, the description still explains return limits, truncation, and text-cell clipping. Combined with the detailed schema and examples, it gives an agent enough context to construct valid queries and understand the result-set constraints.

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

Parameters4/5

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

With 100% schema coverage, both parameters are already documented, but the description substantially enriches the sql parameter by providing the full database schema, FTS syntax, LIKE guidance, and three worked examples. The session_id pointer is also referenced, though mainly left to the schema.

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?

The description states a specific verb and resource: run one read-only SQLite SELECT/WITH query against a session database. It also explicitly distinguishes the tool from the sibling smart_compact_search by saying it handles counts, aggregates, and joins that smart_compact_search cannot express.

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?

It names the sibling alternative and the precise condition for choosing this tool: analytical SQL queries that smart_compact_search cannot express. The examples and rejection rules further clarify appropriate use.

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