Skip to content

Validate SQL output column contracts in MCP tools - #333

Open
seyoung4503 wants to merge 5 commits into
fix/sqlite-query-deadlinefrom
fix/sql-output-columns
Open

seyoung4503 wants to merge 5 commits into
fix/sqlite-query-deadlinefrom
fix/sql-output-columns

Conversation

@seyoung4503

@seyoung4503 seyoung4503 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

SQL execution success currently cannot distinguish a correct projection from missing or extra output columns. Previously observed failures include returning the right values with an extra identifier, or returning selected entities without a requested measure.

Add optional expected_columns to run_sql: compare exact ordered labels against actual result metadata and return an MCP error with missing/unexpected columns on mismatch. SQLAlchemy preserves labels even for zero rows using a list-compatible result. Empty results from legacy adapters without metadata explicitly fail verification; calls without a contract retain their behavior. Bound Oracle metric mode explicitly rejects this SQL-only option.

This is deterministic validation of a caller-supplied contract, not automatic natural-language interpretation, type/row-count/uniqueness validation, or proof of semantic accuracy. A wrong or omitted contract can still produce a wrong answer. No system prompt, grader, gold data, or benchmark thresholds changed. Based on #332; later prompt experiment branches are not included.

Validation: 6 new stdio MCP scenarios failed before implementation and pass afterward; root tests 284 passed / 1 skipped; offline Ruff and diff checks pass. Invalid contracts never execute SQL; legacy empty-result behavior is covered. Targeted ChatGPT-auth Codex evaluation completed: bird165 0/1, local220 0/1, both successfully executed but officially incorrect. The model used expected_columns, yet chose the wrong output contract: BIRD included an extra average column; Spider used a one-row/two-player layout. No accuracy improvement demonstrated.

The first attempt exposed a regression introduced here: a success-message prefix displaced the row-count header, causing execution-provenance rejection. Fixed the renderer without loosening the verifier, added real-MCP-to-verifier tests, and reran only those failures. Both initial failures and corrected runs are archived under bench/accuracy/mcp-review-results/output-columns/, including raw events, records, settings, scores, analysis and SHA256 index. Benchmark-generated provider-call/token zero placeholders are marked unknown in copied summaries; actual CLI usage remains in traces. Bound metric rejection and failed-contract audit are also tested.

Implementation verification — reused RunSQL, DefinitionCheckedSQL schema/delegation, SQLAlchemy worker, existing safety/audit/rendering and MCP test fixture. Net +117 implementation/test lines.

Principle Result Evidence
Reuse O Existing execution path, one result-metadata concept
Cause scope O Projection mismatch and absent empty-result labels only
Logging O Scoped invalid/failed/validated contract events
Error levels O Explicit MCP error and failed-contract audit event
Environment injection N/A No environment reads/settings added
Issue numbers in comments O None added
Verification O 284 passed, 1 skipped; Ruff/diff clean; no CODE MAP generator in repo
Approval boundary O PR only, no merge
Call path O MCP schema → receipt gate → RunSQL → adapter metadata → validation
Sync/async O Existing async-to-thread DB worker, one validation path, no new blocking I/O
Hardcoded operational settings O No new timeout/batch/env/deployment settings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant