mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 19:34:38 +00:00
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:
@@ -752,7 +752,7 @@ describe('read_asset agent tool', () => {
|
||||
assetId: 'doc-md',
|
||||
docConfig: { mode: 'text', startPage: 1, endPage: 2 },
|
||||
}),
|
||||
).rejects.toThrow('startPage and endPage must be null when docConfig mode is "text"')
|
||||
).rejects.toThrow('startPage and endPage must be left out when docConfig mode is "text"')
|
||||
})
|
||||
|
||||
it('reads raw text in mode "text"', async () => {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z } from 'zod'
|
||||
import type { TSchema } from 'typebox'
|
||||
import { Compile } from 'typebox/compile'
|
||||
import { type AgentTool, type AgentToolResult } from '@earendil-works/pi-agent-core'
|
||||
import { dropNullOptionals } from '@shumai/operations'
|
||||
import {
|
||||
getOperationsForSurface,
|
||||
runOperation,
|
||||
@@ -53,27 +54,13 @@ export function toToolParameters(op: Operation, extension?: AgentToolExtension):
|
||||
return schema as unknown as TSchema
|
||||
}
|
||||
|
||||
/**
|
||||
* Models sometimes send `null` for optional parameters they don't want to set. Drop those
|
||||
* so the input schema treats them as omitted; nulls for required fields still fail.
|
||||
*/
|
||||
export function dropNullOptionalFields(schema: z.ZodObject, params: unknown): unknown {
|
||||
if (!params || typeof params !== 'object' || Array.isArray(params)) return params
|
||||
const shape = schema.shape
|
||||
return Object.fromEntries(
|
||||
Object.entries(params).filter(
|
||||
([key, value]) => !(value === null && shape[key]?.safeParse(undefined).success),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/** Split agent-only parameters from the Operation input. */
|
||||
export function splitAgentParams(
|
||||
op: Operation,
|
||||
extension: AgentToolExtension | undefined,
|
||||
params: unknown,
|
||||
): { input: unknown; extra: Record<string, unknown> } {
|
||||
const cleaned = dropNullOptionalFields(toolInputSchema(op, extension), params)
|
||||
const cleaned = dropNullOptionals(toolInputSchema(op, extension), params)
|
||||
if (!extension?.extraParameters || !cleaned || typeof cleaned !== 'object') {
|
||||
return { input: cleaned, extra: {} }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user