Skip to content

Commit 79839d4

Browse files
anandgupta42claude
andcommitted
fix: tolerate a non-string connection type in canonicalType; substitute config variables in MCP output limits
- `warehouse_list` discovery no longer throws on a connection whose `type` is not a string - `outputLimits` applies `ConfigVariable.substitute` like the agent loader Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e65cfa5 commit 79839d4

4 files changed

Lines changed: 41 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Flag, env } from "@opencode-ai/core/flag/flag"
66
import { FSUtil } from "@opencode-ai/core/fs-util"
77
import { Global } from "@/global"
88
import { ConfigPaths } from "@/config/paths"
9+
import { ConfigVariable } from "@/config/variable"
910
import { git } from "@/util/git"
1011
import { MAX_LINES, MAX_BYTES } from "@/tool/truncate-core"
1112

@@ -37,15 +38,20 @@ async function projectWorktree(directory: string): Promise<string> {
3738
export async function outputLimits() {
3839
const directory = process.cwd()
3940
const limits = { maxLines: MAX_LINES, maxBytes: MAX_BYTES }
40-
const apply = (text: string) => {
41-
const output = parse(text)?.tool_output
41+
// `{env:VAR}`, `${VAR}` and `{file:path}` resolve as in the agent loader; text that cannot be substituted is skipped.
42+
const apply = async (text: string, from: { path: string } | { source: string }) => {
43+
const expanded = await ConfigVariable.substitute(
44+
"path" in from ? { text, type: "path", path: from.path } : { text, type: "virtual", dir: directory, source: from.source },
45+
).catch(() => undefined)
46+
if (!expanded) return
47+
const output = parse(expanded)?.tool_output
4248
if (Number.isInteger(output?.max_lines) && output.max_lines > 0) limits.maxLines = output.max_lines
4349
if (Number.isInteger(output?.max_bytes) && output.max_bytes > 0) limits.maxBytes = output.max_bytes
4450
}
4551
// A missing or unreadable file contributes nothing; output limits never stop the server from starting.
4652
const load = async (file: string) => {
4753
const text = await readFile(file, "utf8").catch(() => undefined)
48-
if (text) apply(text)
54+
if (text) await apply(text, { path: file })
4955
}
5056
const worktree = await projectWorktree(directory)
5157
const discover = <A>(effect: Effect.Effect<A, unknown, FSUtil.Service>, fallback: A) =>
@@ -69,6 +75,6 @@ export async function outputLimits() {
6975
}
7076
}
7177
const content = env("OPENCODE_CONFIG_CONTENT")
72-
if (content) apply(content)
78+
if (content) await apply(content, { source: "OPENCODE_CONFIG_CONTENT" })
7379
return limits
7480
}

‎packages/opencode/src/altimate/native/connections/registry.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,9 @@ const DRIVER_MAP: Record<string, string> = {
401401
* different service with different credentials and endpoints, where Postgres
402402
* wire-compatibility is an implementation detail rather than an identity.
403403
*/
404-
export function canonicalType(type: string | undefined | null): string | null {
405-
if (!type) return null
404+
export function canonicalType(type: unknown): string | null {
405+
// A hand-edited connection may carry a non-string type; it maps to no driver rather than throwing.
406+
if (typeof type !== "string" || !type) return null
406407
const driverPath = DRIVER_MAP[type.toLowerCase()]
407408
if (!driverPath) return null
408409
return driverPath.slice(driverPath.lastIndexOf("/") + 1)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ test("ALTIMATE_CLI_DISABLE_PROJECT_CONFIG skips project files", async () => {
8181
expect(await outputLimits()).toEqual({ maxLines: MAX_LINES, maxBytes: MAX_BYTES })
8282
})
8383

84+
test("substitutes {env:VAR} as the agent loader does", async () => {
85+
process.env.MCP_OUTPUT_TEST_LINES = "11"
86+
try {
87+
await writeFile(path.join(repo, "opencode.json"), '{ "tool_output": { "max_lines": {env:MCP_OUTPUT_TEST_LINES} } }')
88+
expect(await outputLimits()).toEqual({ maxLines: 11, maxBytes: MAX_BYTES })
89+
} finally {
90+
delete process.env.MCP_OUTPUT_TEST_LINES
91+
}
92+
})
93+
8494
test("an unreadable config path does not stop startup", async () => {
8595
await mkdir(path.join(repo, "sub", "opencode.json"))
8696
await writeFile(path.join(repo, "opencode.jsonc"), output({ max_lines: 8 }))

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,25 @@ mcpTest("scratch table writes run without --allow-write; source writes stay refu
222222
expect((await call("sql_execute", { warehouse: "source", query: "INSERT INTO items VALUES (3, 'fig')" })).isError).toBe(true)
223223
})
224224

225-
mcpTest("a discovered connection without a string type does not stop startup", async () => {
226-
Registry.setConfigs({ broken: {} as never })
225+
mcpTest("a discovered connection without a string type stops neither startup nor warehouse_list", async () => {
226+
const dir = await mkdtemp(path.join(os.tmpdir(), "mcp-malformed-type-"))
227+
cleanups.push(() => rm(dir, { recursive: true, force: true }))
228+
const db = new Database(path.join(dir, "ok.sqlite"))
229+
db.run("CREATE TABLE items (id INTEGER)")
230+
db.close()
231+
Registry.setConfigs({ missing: {} as never, numeric: { type: 42 } as never, ok: { type: "sqlite", path: path.join(dir, "ok.sqlite") } })
227232
const server = await createServer({})
228-
await server.close()
233+
const client = new Client({ name: "test", version: "1" }, { capabilities: {} })
234+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair()
235+
await server.connect(serverTransport)
236+
await client.connect(clientTransport)
237+
cleanups.push(async () => {
238+
await client.close()
239+
await server.close()
240+
})
241+
const listed = await client.callTool({ name: "warehouse_list", arguments: {} })
242+
expect(listed.isError, text(listed)).toBe(false)
243+
expect(text(listed)).toContain("ok")
229244
})
230245

231246
mcpTest("MCP cancellation reaches the active tool abort signal", async () => {

0 commit comments

Comments
 (0)