Commit 772f9d7
authored
feat: validate format param, surface in tool prose, add opt-in JSON unwrap (#58)
* feat: validate format parameter, surface in tool prose, add opt-in JSON unwrap
- Reject invalid format values with a clear error naming accepted values
(json, csv, markdown) instead of silently falling through to JSON default
- Add format and unwrap_json documentation to trino_query and trino_execute
tool description prose so MCP agents discover them without reading param schemas
- Add opt-in unwrap_json parameter that parses single-row, single-VARCHAR-column
JSON results into an unwrapped_result field, reducing double-decode overhead
for table functions like raw_query
- Extract shared formatOutput/validateFormat/tryUnwrapJSON helpers to format.go
Closes #57
* fix: address all review findings from PR #58
Bug fixes:
- unwrap_json now renders the QueryOutput (with unwrapped_result) into
the TextContent so the LLM actually sees clean JSON in its context,
not just in the typed structured output
- formatOutput uses explicit case "json" instead of default, with an
error-returning default for unsupported formats
Design fixes:
- tryUnwrapJSON rejects bare JSON scalars (strings, numbers, booleans,
null) — only objects and arrays are meaningful to unwrap
- Accept CHAR, CHAR(N), and JSON column types in addition to VARCHAR
for unwrap detection via new isStringColumnType helper
- Validate trino_explain type parameter with same pattern as format:
invalid values return a clear error instead of silently defaulting
Code quality:
- Extract renderTextContent helper to reduce cyclomatic complexity in
handleQuery (18→14) and handleExecute (17→13)
- Extract output format string constants to satisfy goconst linter
- Move nolint:lll to end of same line (conventional placement)
- Revert unnecessary struct field alignment in QueryOutput
- Simplify JSON comparison in tests (direct string comparison)
Test additions:
- unwrap_json with CSV format: structured output has unwrapped_result,
CSV text does not
- Scalar JSON values ("hello", 42, true, null) are not unwrapped
- Invalid explain type returns error
- Text content assertions for unwrap_json (verifies issue #1 fix)
* fix: unwrap JSON inline in rows instead of sidecar field (Option 2)
Redesign unwrap_json to mutate the QueryOutput in place rather than
adding a separate unwrapped_result field. When the flag is on and the
result matches (1 row, 1 string-type column, valid JSON object/array):
- columns[0].type is changed to "JSON" as an unambiguous signal
- rows[0][col] is replaced with the parsed object
- the envelope shape is identical to the non-unwrap case
This eliminates the double-payload problem (old design serialized both
the escaped string in rows AND the parsed object in unwrapped_result),
the schema divergence (old design rendered QueryOutput vs QueryResult
depending on the flag), and delivers the actual token-cost reduction
that motivated the feature.
Specific changes:
- Remove UnwrappedResult from QueryOutput and renderTextContent helper
- Add unwrapJSONColumn that mutates QueryOutput in place
- Move formatCSV/formatMarkdown to format.go, change to accept *QueryOutput
- Add stringifyValue helper so CSV/markdown render maps as compact JSON
- Add columnTypeJSON constant for the "JSON" type signal
- Update descriptions to say "single-string-column" (matches CHAR/JSON too)
- Rewrite all unwrap tests to assert column type and row value, not sidecar
* fix: description mismatch, shared slice mutation, code organization
- Fix ToolExecute description to match ToolQuery: "single-string-column"
with full column-type-changes-to-JSON language (was still saying
"single-VARCHAR-column" from the first commit)
- Shallow-copy the row map in unwrapJSONColumn before mutation to avoid
modifying the shared client.QueryResult.Rows slice
- Move escapeCSV from query.go to format.go alongside its only caller
- Remove dead explain_test.go cases for inputs now rejected by validation1 parent 18313e8 commit 772f9d7
9 files changed
Lines changed: 1070 additions & 163 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
12 | 17 | | |
13 | 18 | | |
14 | 19 | | |
15 | 20 | | |
16 | 21 | | |
17 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
18 | 28 | | |
19 | 29 | | |
20 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| |||
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
| |||
68 | 72 | | |
69 | 73 | | |
70 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
71 | 80 | | |
72 | 81 | | |
73 | 82 | | |
| |||
117 | 126 | | |
118 | 127 | | |
119 | 128 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
124 | 136 | | |
125 | 137 | | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
138 | 142 | | |
139 | 143 | | |
140 | 144 | | |
141 | 145 | | |
142 | 146 | | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
62 | 67 | | |
63 | 68 | | |
64 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
92 | 85 | | |
93 | 86 | | |
94 | 87 | | |
| |||
0 commit comments