Skip to content

Commit 2cde866

Browse files
Akshayclaude
authored andcommitted
fix(answer_commit): decode JSON-string args in MCP raw-argument readers (AltimateAI#1700)
Addresses review: finalAnswerText (adapter) and clarifyReading (server) read the raw MCP arguments, so JSON-string fields/rows fell back to the labelled FINAL ANSWER and JSON-string readings skipped the reading question. Export the parser's preprocess as normalizeAnswerArgs and use it in both places so they see the shape the tool validated. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01WyL6JbJvKGuJD8gT1BWVx3
1 parent a7d958e commit 2cde866

5 files changed

Lines changed: 48 additions & 7 deletions

File tree

‎packages/opencode/src/altimate/mcp-server/adapter.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Agent } from "@/agent/agent"
99
import { Tool } from "@/tool/tool"
1010
import { Truncate } from "@/tool/truncate"
1111
import { outputLimits } from "./output-settings"
12-
import { MAX_BASIS_CHARACTERS, renderAnswer } from "../tools/answer-commit"
12+
import { MAX_BASIS_CHARACTERS, normalizeAnswerArgs, renderAnswer } from "../tools/answer-commit"
1313
import { AnswerCommitConjunction } from "../tools/answer-commit-conjunction"
1414
import { MAX_SCANNED_CHARACTERS } from "../tools/answer-commit-checks"
1515
import * as Precedence from "../workspace/precedence"
@@ -230,7 +230,10 @@ type TurnMessage = Tool.Context["messages"][number]
230230
* explanation never becomes part of the answer text a client copies. The basis is kept,
231231
* above that line, as context for the user.
232232
*/
233-
export function finalAnswerText(args: Record<string, unknown>, committed: string, request?: unknown): string {
233+
export function finalAnswerText(raw: Record<string, unknown>, committed: string, request?: unknown): string {
234+
// altimate_change start — #1700: read the same decoded shape the tool validated (JSON-string fields/rows)
235+
const args = normalizeAnswerArgs(raw) as Record<string, unknown>
236+
// altimate_change end
234237
// Answer-only only for a complete commit: rows, or fields covering every item the request names.
235238
// Otherwise keep the committed text (basis on the line) so an item kept only in the basis is not lost.
236239
const fields = Array.isArray(args.fields) ? args.fields.length : 0

‎packages/opencode/src/altimate/mcp-server/server.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "@modelcontextprotocol/sdk/types.js"
1010
import * as Registry from "../native/connections/registry"
1111
import { createAdapter, descriptions, resultFailed } from "./adapter"
12+
import { normalizeAnswerArgs } from "../tools/answer-commit"
1213
import { catalogSummary } from "../tools/warehouse-discovery"
1314
import { readOnlyCaveat } from "../native/connections/read-only"
1415
import path from "node:path"
@@ -183,7 +184,10 @@ export async function createServer(options: ServeOptions = {}) {
183184
result: { status?: string },
184185
signal: AbortSignal,
185186
) => {
186-
const readings = Array.isArray(args?.readings) ? (args.readings as { reading?: unknown }[]) : []
187+
// altimate_change start — #1700: readings may arrive as a JSON string; read the decoded shape
188+
const decoded = normalizeAnswerArgs(args) as Record<string, unknown> | undefined
189+
const readings = Array.isArray(decoded?.readings) ? (decoded.readings as { reading?: unknown }[]) : []
190+
// altimate_change end
187191
const choices = readings.map((item) => item.reading).filter((item): item is string => typeof item === "string" && !!item)
188192
if (elicited || result.status === "error" || choices.length < 2 || !server.getClientCapabilities()?.elicitation) return undefined
189193
elicited = true

‎packages/opencode/src/altimate/tools/answer-commit.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,22 @@ function decodeJsonShaped(value: unknown): unknown {
198198
}
199199
// altimate_change end
200200

201-
const parser = z.preprocess((input) => {
201+
// altimate_change start — #1700: exported so raw-argument readers (MCP adapter/server) see the same decoded shape
202+
export function normalizeAnswerArgs(input: unknown): unknown {
202203
if (!input || typeof input !== "object" || Array.isArray(input)) return input
203204
const next: Record<string, unknown> = { ...input }
204-
// altimate_change start — #1700: decode JSON-encoded list/object inputs before validation
205205
for (const key of JSON_SHAPED_KEYS) {
206206
if (key in next) next[key] = decodeJsonShaped(next[key])
207207
}
208-
// altimate_change end
209208
if (!("answer_only" in next)) return next
210209
const value = typeof next.answer_only === "string" ? next.answer_only.toLowerCase() : next.answer_only
211210
next.answer_only =
212211
value === "true" || value === 1 ? true : value === "false" || value === 0 ? false : next.answer_only
213212
return next
214-
}, parameters)
213+
}
214+
215+
const parser = z.preprocess(normalizeAnswerArgs, parameters)
216+
// altimate_change end
215217

216218
function validationError(error: z.ZodError): string {
217219
const describe = (issues: z.core.$ZodIssue[], parent: PropertyKey[] = []): string[] =>

‎packages/opencode/test/altimate/mcp-server/final-answer.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,23 @@ describe("finalAnswerText", () => {
6969
test("falls back to the committed text when the args cannot be re-rendered", () => {
7070
expect(finalAnswerText({ basis: "" }, "committed")).toBe("FINAL ANSWER: committed")
7171
})
72+
73+
// #1700: some models send list inputs as JSON strings; the FINAL ANSWER line must match a real array.
74+
test("fields and asked sent as JSON strings give the same answer-only line", () => {
75+
const fields = [{ name: "orders", value: 42 }]
76+
const committed = `${basis}\n\n**Answer:** 42`
77+
const expected = finalAnswerText({ fields, asked: ["orders"], basis }, committed)
78+
expect(
79+
finalAnswerText({ fields: JSON.stringify(fields), asked: JSON.stringify(["orders"]), basis }, committed),
80+
).toBe(expected)
81+
expect(expected).toBe(`Basis: ${basis}\n\nFINAL ANSWER: 42`)
82+
})
83+
84+
test("rows sent as a JSON string give the same answer-only line", () => {
85+
const rows = { columns: ["city", "n"], values: [["Oslo", 3]] }
86+
expect(finalAnswerText({ rows: JSON.stringify(rows), asked: ["city", "n"], basis }, "unused")).toBe(
87+
finalAnswerText({ rows, asked: ["city", "n"], basis }, "unused"),
88+
)
89+
})
7290
})
7391
// altimate_change end

‎packages/opencode/test/altimate/mcp-server/server.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,20 @@ mcpTest("answer_commit elicits a reading and asks for a recompute when another i
855855
expect(asked).toBe(1)
856856
})
857857

858+
// #1700: readings sent as a JSON string still trigger the reading question.
859+
mcpTest("answer_commit elicits a reading when readings arrive as a JSON string", async () => {
860+
const { client, call } = await fixture(false, { elicitation: {} })
861+
let asked = 0
862+
client.setRequestHandler(ElicitRequestSchema, async () => {
863+
asked++
864+
return { action: "accept", content: { reading: "by id" } }
865+
})
866+
const result = await call("answer_commit", { ...twoReadings, readings: JSON.stringify(twoReadings.readings) })
867+
expect(result.isError).toBe(true)
868+
expect(text(result)).toContain("by id")
869+
expect(asked).toBe(1)
870+
})
871+
858872
mcpTest("answer_commit degrades silently when the client cancels or lacks elicitation", async () => {
859873
const cancelled = await fixture(false, { elicitation: {} })
860874
cancelled.client.setRequestHandler(ElicitRequestSchema, async () => ({ action: "cancel" }))

0 commit comments

Comments
 (0)