mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 03:59:33 +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:
@@ -85,6 +85,55 @@ describe('MCP server', () => {
|
||||
expect(annotations('list_assets')).toMatchObject({ readOnlyHint: true, idempotentHint: true })
|
||||
})
|
||||
|
||||
it('advertises input schemas without nullable types', async () => {
|
||||
// Nullable types don't translate to every client's tool format (e.g. Gemini's)
|
||||
for (const tool of (await client.listTools()).tools) {
|
||||
const schema = JSON.stringify(tool.inputSchema)
|
||||
expect(schema, tool.name).not.toMatch(/"type":\[/)
|
||||
expect(schema, tool.name).not.toContain('"type":"null"')
|
||||
}
|
||||
})
|
||||
|
||||
it('treats null optional arguments as omitted, including nested ones', async () => {
|
||||
vi.mocked(runOperation).mockResolvedValue({
|
||||
status: 'pending',
|
||||
summary: 'Rendering is still in progress for "deck.pdf".',
|
||||
asset: { id: 'doc-1', name: 'deck.pdf', kind: 'document', mediaType: 'application/pdf' },
|
||||
details: [],
|
||||
images: [],
|
||||
taskId: 'task-1',
|
||||
retryAfterSeconds: 5,
|
||||
})
|
||||
|
||||
const result = await client.callTool({
|
||||
name: 'read_asset',
|
||||
arguments: {
|
||||
assetId: 'doc-1',
|
||||
annotationId: null,
|
||||
videoConfig: null,
|
||||
docConfig: { mode: 'pages', startPage: 1, endPage: null },
|
||||
inlineImages: null,
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.isError).toBeFalsy()
|
||||
expect(runOperation).toHaveBeenCalledWith(
|
||||
readAssetOp,
|
||||
{ userId: 'user1', via: 'mcp' },
|
||||
{ assetId: 'doc-1', docConfig: { mode: 'pages', startPage: 1 } },
|
||||
)
|
||||
})
|
||||
|
||||
it('still rejects null for required arguments', async () => {
|
||||
const result = await client.callTool({
|
||||
name: 'create_folder',
|
||||
arguments: { parent: null, name: 'Renders' },
|
||||
})
|
||||
|
||||
expect(result.isError).toBe(true)
|
||||
expect(runOperation).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not expose agent-only capabilities', async () => {
|
||||
const names = (await client.listTools()).tools.map((t) => t.name)
|
||||
for (const agentOnly of ['bash', 'read_skill', 'download_asset', 'mcp']) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Hono } from 'hono'
|
||||
import { createMcpHandler, McpServer, type CallToolResult } from '@modelcontextprotocol/server'
|
||||
import type { z } from 'zod'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { dropNullOptionals } from '@shumai/operations'
|
||||
import { getOperationsForSurface, runOperation, type Operation } from '@shumai/core/src/operations'
|
||||
import { mcpExtensions, type McpToolExtension } from './mcp-extensions'
|
||||
import { assertWritableInDemoMode, describeOperationError } from './operation-support'
|
||||
@@ -19,6 +21,21 @@ export const MCP_SERVER_INSTRUCTIONS = [
|
||||
'if it returns status "pending", call get_asset_render with its taskId.',
|
||||
].join(' ')
|
||||
|
||||
/**
|
||||
* The tool's input schema, with `null` for optional arguments treated as omitted. The SDK
|
||||
* validates arguments before calling the tool, so the nulls must be dropped here; the JSON
|
||||
* Schema shown to clients is unchanged (optional, not nullable, which more clients support).
|
||||
*/
|
||||
function toolInputSchema(schema: z.ZodObject): Pick<z.ZodObject, '~standard'> {
|
||||
const standard = schema['~standard']
|
||||
return {
|
||||
'~standard': {
|
||||
...standard,
|
||||
validate: (value) => standard.validate(dropNullOptionals(schema, value)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** Separate MCP-only arguments (declared by an extension) from the Operation input. */
|
||||
function splitArgs(
|
||||
extension: McpToolExtension | undefined,
|
||||
@@ -78,9 +95,11 @@ export function buildMcpServer(userId: string): McpServer {
|
||||
{
|
||||
title: definition.title,
|
||||
description: definition.description,
|
||||
inputSchema: extension?.extraInput
|
||||
? definition.input.extend(extension.extraInput.shape)
|
||||
: definition.input,
|
||||
inputSchema: toolInputSchema(
|
||||
extension?.extraInput
|
||||
? definition.input.extend(extension.extraInput.shape)
|
||||
: definition.input,
|
||||
),
|
||||
outputSchema: definition.output,
|
||||
annotations: {
|
||||
title: definition.title,
|
||||
|
||||
Reference in New Issue
Block a user