Skip to content

Commit bd2765e

Browse files
committed
fix(mcp): prevent null parameters in MCP tool calls for OpenAI-compatible providers
Problem: MCP tool parameters with only a description (no explicit type) caused MiniMax and other OpenAI-compatible models to receive null values. Solution: - Extend sanitizeOpenAISchema() to @ai-sdk/openai-compatible providers - Improve type inference to add type: "string" when only description is present - Defensive null-stripping before forwarding to MCP server - Inherit MCP tool allow permissions in subagent sessions Fixes: MCP null parameter issue for MiniMax/OpenAI-compatible providers Refs: anomalyco#21080 (related), anomalyco#16491 (related)
1 parent 888dd26 commit bd2765e

6 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ UPCOMING_CHANGELOG.md
3232
logs/
3333
*.bun-build
3434
tsconfig.tsbuildinfo
35+
Dockerfile
36+
run.Dockerfile

‎packages/opencode/src/agent/subagent-permissions.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import type { Agent } from "./agent"
88
* 1. The parent session's deny rules and external_directory rules.
99
* Parent agent restrictions only govern that agent; the subagent's own
1010
* permissions determine its capabilities.
11-
* 2. Default `todowrite` and `task` denies if the subagent's own ruleset
11+
* 2. The parent session's allow rules for MCP tools — subagents need
12+
* explicit allow permissions to execute MCP tools (context7_resolve-library-id,
13+
* matrix_matrix_read, etc.). Without this, subagents can see MCP tools in
14+
* their tool list but get permission denied on execution. (#16491, #3808)
15+
* 3. Default `todowrite` and `task` denies if the subagent's own ruleset
1216
* doesn't already permit them.
1317
*/
1418
export function deriveSubagentSessionPermission(input: {
@@ -17,7 +21,13 @@ export function deriveSubagentSessionPermission(input: {
1721
}): PermissionV1.Ruleset {
1822
const canTask = input.subagent.permission.some((rule) => rule.permission === "task")
1923
const canTodo = input.subagent.permission.some((rule) => rule.permission === "todowrite")
24+
const parentSessionMcpAllows = input.parentSessionPermission.filter(
25+
(rule) =>
26+
rule.action === "allow" &&
27+
(rule.permission === "*" || rule.permission.includes("_")),
28+
)
2029
return [
30+
...parentSessionMcpAllows,
2131
...input.parentSessionPermission.filter(
2232
(rule) => rule.permission === "external_directory" || rule.action === "deny",
2333
),

‎packages/opencode/src/mcp/catalog.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: numbe
5151
description: mcpTool.description ?? "",
5252
inputSchema: jsonSchema(inputSchema),
5353
execute: async (args: unknown, options) => {
54+
const cleanArgs = Object.fromEntries(
55+
Object.entries((args || {}) as Record<string, unknown>).filter(
56+
([, v]) => v !== null && v !== undefined,
57+
),
58+
)
5459
const result = await client.callTool(
5560
{
5661
name: mcpTool.name,
57-
arguments: (args || {}) as Record<string, unknown>,
62+
arguments: cleanArgs,
5863
},
5964
CallToolResultSchema,
6065
{

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ const layer = Layer.effect(
382382

383383
if (!mcpClient) {
384384
if (status.status !== "connected" && status.status !== "disabled") {
385-
yield* Effect.logWarning("server unavailable", { key, type: mcp.type, status: status.status })
385+
yield* Effect.logWarning("server unavailable", { key, type: mcp.type, status: status.status, error: (status as any).error })
386386
}
387387
return { status } satisfies CreateResult
388388
}

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,9 @@ function sanitizeOpenAISchema(value: unknown): unknown {
13681368

13691369
if (typeof value.$ref === "string") result.$ref = value.$ref
13701370
if (typeof value.description === "string") result.description = value.description
1371+
if ("default" in value) result.default = value.default
1372+
if (Array.isArray(value.examples)) result.examples = value.examples
1373+
if (typeof value.title === "string") result.title = value.title
13711374
if ("const" in value) result.enum = [value.const]
13721375
else if (Array.isArray(value.enum)) result.enum = value.enum
13731376

@@ -1424,13 +1427,21 @@ function sanitizeOpenAISchema(value: unknown): unknown {
14241427
? ["object"]
14251428
: ["items", "prefixItems"].some((key) => key in value)
14261429
? ["array"]
1427-
: "enum" in result || "format" in value
1430+
: "enum" in result || "format" in value || "pattern" in value ||
1431+
"minLength" in value || "maxLength" in value ||
1432+
"contentMediaType" in value || "contentEncoding" in value
14281433
? ["string"]
14291434
: ["minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf"].some((key) => key in value)
14301435
? ["number"]
14311436
: []
14321437

1433-
if (inferredTypes.length === 0) return {}
1438+
if (inferredTypes.length === 0) {
1439+
if (typeof value.description === "string") {
1440+
result.type = "string"
1441+
return result
1442+
}
1443+
return {}
1444+
}
14341445

14351446
result.type = inferredTypes.length === 1 ? inferredTypes[0] : inferredTypes
14361447
if (inferredTypes.includes("object") && !("properties" in result)) result.properties = {}
@@ -1457,7 +1468,7 @@ export function schema(model: Provider.Model, schema: JSONSchema7): JSONSchema7
14571468
}
14581469
*/
14591470

1460-
if (model.api.npm === "@ai-sdk/openai" || model.api.npm === "@ai-sdk/azure") {
1471+
if (model.api.npm === "@ai-sdk/openai" || model.api.npm === "@ai-sdk/azure" || model.api.npm === "@ai-sdk/openai-compatible") {
14611472
schema = sanitizeOpenAISchema(schema) as JSONSchema7
14621473
// Codex also applies lossy compaction above 4 KB; defer that until OpenCode needs the same schema budget.
14631474
}

‎packages/opencode/test/provider/transform.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,10 +1396,12 @@ describe("ProviderTransform.schema - openai supported schema subset", () => {
13961396

13971397
expect(result).toEqual({
13981398
type: "object",
1399+
title: "Search",
13991400
properties: {
14001401
query: {
14011402
type: "string",
14021403
description: "Search query",
1404+
default: "https://example.com",
14031405
},
14041406
count: {
14051407
type: "integer",
@@ -1458,6 +1460,7 @@ describe("ProviderTransform.schema - openai supported schema subset", () => {
14581460
expect(result.properties.value).toEqual({
14591461
$ref: "#/$defs/Value",
14601462
description: "Referenced value",
1463+
examples: ["ignored"],
14611464
})
14621465
expect(result.$defs).toEqual({
14631466
Value: {

0 commit comments

Comments
 (0)