fix(mcp): make tool input schemas portable to single-type dialects

Nullable inputs were emitted as `type: ["number", "null"]` (or a `null`
anyOf branch), which clients that map tool schemas onto an OpenAPI-style
subset (e.g. Gemini function declarations) may reject.

- Make read_asset's startPage, endPage, and degree optional instead of
  nullable.
- Move null handling into a shared dropNullOptionals helper in
  @shumai/operations that also strips nulls inside nested config
  objects. The agent uses it, and the MCP server applies it before the
  SDK validates arguments, so models that send null keep working.
- Test that no Operation input schema (and no listed MCP tool) uses a
  nullable type.
This commit is contained in:
yiling
2026-10-26 17:24:34 +08:00
parent 4fc8476b0f
commit b7dccad6e5
11 changed files with 164 additions and 28 deletions
@@ -20,7 +20,6 @@ export const readerOptionSchemas = {
degree: z
.number()
.int()
.nullable()
.optional()
.describe(
'Turntable rotation degree (integer: 0, 15, 30, ..., 345 in 15° increments) to inspect a specific angle of a 3D model. Leave it out to view the 4-view composite overview.',
@@ -57,12 +56,10 @@ export const readerOptionSchemas = {
),
startPage: z
.number()
.nullable()
.optional()
.describe('Start page number (1-based, required when mode="pages"; omit for "text").'),
endPage: z
.number()
.nullable()
.optional()
.describe(
`End page number (1-based, max ${MAX_RENDER_ITEMS} pages per call, required when mode="pages"; omit for "text").`,
+1
View File
@@ -1,6 +1,7 @@
export * from './define'
export * from './schemas'
export * from './registry'
export { dropNullOptionals } from './null-optionals'
export { createFolder } from './asset/create-folder'
export { deleteAsset } from './asset/delete-asset'
export { listAssets } from './asset/list-assets'
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import { dropNullOptionals } from './null-optionals'
const schema = z.object({
id: z.string(),
note: z.string().optional(),
config: z
.object({
mode: z.enum(['a', 'b']),
start: z.number().optional(),
})
.optional(),
tags: z.array(z.string()).optional(),
})
describe('dropNullOptionals', () => {
it('drops nulls for optional fields, including inside nested objects', () => {
expect(
dropNullOptionals(schema, {
id: 'x',
note: null,
config: { mode: 'a', start: null },
tags: null,
}),
).toEqual({ id: 'x', config: { mode: 'a' } })
})
it('drops a null nested object when the object is optional', () => {
expect(dropNullOptionals(schema, { id: 'x', config: null })).toEqual({ id: 'x' })
})
it('keeps nulls for required fields so validation still fails', () => {
const cleaned = dropNullOptionals(schema, { id: null, config: { mode: null } })
expect(cleaned).toEqual({ id: null, config: { mode: null } })
expect(schema.safeParse(cleaned).success).toBe(false)
})
it('leaves unknown keys and non-object values alone', () => {
expect(dropNullOptionals(schema, { id: 'x', other: null })).toEqual({ id: 'x', other: null })
expect(dropNullOptionals(schema, null)).toBeNull()
expect(dropNullOptionals(schema, ['a'])).toEqual(['a'])
})
})
+29
View File
@@ -0,0 +1,29 @@
import { z } from 'zod'
/** Strip optional/default wrappers to reach an object schema, if the field is one. */
function objectSchemaOf(schema: z.ZodType): z.ZodObject | undefined {
let current: z.ZodType = schema
while (current instanceof z.ZodOptional || current instanceof z.ZodDefault) {
current = current.unwrap() as z.ZodType
}
return current instanceof z.ZodObject ? current : undefined
}
/**
* Models often send `null` for optional parameters they don't want to set, including inside
* nested config objects. Operation inputs are optional rather than nullable (nullable types
* don't translate to every model's tool format), so drop those nulls before validating:
* they are treated as omitted. Nulls for required fields are kept and still fail validation.
*/
export function dropNullOptionals(schema: z.ZodObject, value: unknown): unknown {
if (!value || typeof value !== 'object' || Array.isArray(value)) return value
const shape = schema.shape as Record<string, z.ZodType>
const result: Record<string, unknown> = {}
for (const [key, field] of Object.entries(value)) {
const fieldSchema = shape[key]
if (field === null && fieldSchema?.safeParse(undefined).success) continue
const nested = fieldSchema && objectSchemaOf(fieldSchema)
result[key] = nested ? dropNullOptionals(nested, field) : field
}
return result
}
+12
View File
@@ -70,6 +70,18 @@ describe('operation registry', () => {
},
)
it.each(operationDefinitions.map((d) => [d.name, d] as const))(
'%s input schema has no nullable types',
(_name, definition) => {
// Tool formats like Gemini's function declarations (an OpenAPI subset) can't express
// `type: [..., "null"]` or a `null` branch, so inputs use optional fields instead.
// Surfaces drop nulls that models send for optional fields (dropNullOptionals).
const schema = JSON.stringify(z.toJSONSchema(definition.input, { io: 'input' }))
expect(schema).not.toMatch(/"type":\[/)
expect(schema).not.toContain('"type":"null"')
},
)
it('keeps legacy CLI invocations mapped', () => {
const commands = operationDefinitions.map((d) => [
d.name,