Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mcps): call_tool 入口 args 改为可选,无参工具省略 args 不再被 -32602 拒收 (#6025)
Yunjin Zhang9f1ef577f150
diff --git a/packages/lizi-mcps/src/__tests__/json-object-arg.test.ts b/packages/lizi-mcps/src/__tests__/json-object-arg.test.tsindex 9aa1b616cf..4388ea0c45 100644--- a/packages/lizi-mcps/src/__tests__/json-object-arg.test.ts+++ b/packages/lizi-mcps/src/__tests__/json-object-arg.test.ts@@ -1,15 +1,18 @@/*** jsonObjectArg —— generic `call_tool({ name, args })` 入口 args schema 的回归测试。*- * 覆盖三件事:- * 1. 运行时兜底行为:object 透传 / JSON-string 还原成 object / 真烂字符串仍报错。+ * 覆盖四件事:+ * 1. 运行时兜底行为:object 透传 / JSON-string 还原成 object / 真烂字符串仍报错 /+ * null 归一成 {} / 整体省略(无参工具按内层空 schema 调用)时通过且 args 为 undefined。* 2. 端到端复现 GitHub issue #350:经【真实 MCP SDK 校验路径】传一段被 stringify 的* 超长 args,旧 `z.record` 会在 entry 报 -32602 invalid_type;新 schema 能还原成- * object 并落到 handler。+ * object 并落到 handler。同路径验证省略 args 不再被 -32602 挡回(2026-10-14 修复:+ * 无参工具如 memory_list 按内层空 schema 省略 args 曾直接被入口拒收)。* 3. 描述拼接与落点(CLAUDE.md 规则 10):jsonObjectArg 会在传入 desc 末尾追加固定的* OBJECT_HINT 反例后缀,本测试验证「拼接后的描述」仍逐字节落在【内层 record】上、* 与用相同拼接的 legacy `z.record(...).describe()` 一致,确保描述不因 preprocess 漂移,* 从而不改模型看到的工具定义 / 不破坏缓存前缀。+ * 4. 模型可见 JSON Schema:`args` 不在 required(可选),但 record 约束与描述不变。*/import { describe, it, expect } from 'vitest';import { z } from 'zod';@@ -46,6 +49,22 @@ describe('jsonObjectArg — runtime coercion', () => {it('rejects a JSON string that parses to a non-object (e.g. a number)', () => {expect(schema.safeParse({ args: '42' }).success).toBe(false);});++ it('treats a null args field as an empty object (host collapses {} into null)', () => {+ // 回归 2026-10-14:宿主把空对象 args 压成 null 送到入口,-32602 expected record。+ // null / 缺失 / {} 三种「无参数」形态必须统一落成 {}(消费方 args ?? {})。+ const r = schema.safeParse({ args: null });+ expect(r.success).toBe(true);+ if (r.success) expect(r.data.args).toEqual({});+ });++ it('accepts an omitted args field (no-parameter tools called without args)', () => {+ // 回归 2026-10-14:无参工具(自身 schema 是空对象, 如 memory_list)按内层 schema 省略+ // args 时, 入口曾直接 -32602。现在必须放过, args 为 undefined(消费方 registry 已 ?? {})。+ const r = schema.safeParse({});+ expect(r.success).toBe(true);+ if (r.success) expect(r.data.args).toBeUndefined();+ });});// ── 2. 端到端:真实 MCP SDK Client + InMemoryTransport(复现并验证 #350 修复) ──@@ -126,6 +145,37 @@ describe('jsonObjectArg — end-to-end through real MCP SDK validation (issue #3await h.cleanup();}});++ it('accepts a call that omits args entirely (no-parameter tool, e.g. memory_list)', async () => {+ // 回归 2026-10-14:模型遵守内层空 schema 不传 args, 入口曾 -32602 path ["args"]。+ const h = await makeEchoHarness();+ try {+ const result = (await h.client.callTool({+ name: 'call_tool',+ arguments: { name: 'memory_list' },+ })) as { content: Array<{ type: string; text?: string }>; isError?: boolean };+ expect(result.isError ?? false).toBe(false);+ const echo = readEcho(result);+ expect(echo.name).toBe('memory_list');+ expect(echo.argsType).toBe('undefined');+ } finally {+ await h.cleanup();+ }+ });++ it('accepts the null args the host produces from an empty-object args payload', async () => {+ const h = await makeEchoHarness();+ try {+ const result = (await h.client.callTool({+ name: 'call_tool',+ arguments: { name: 'memory_list', args: null },+ })) as { content: Array<{ type: string; text?: string }>; isError?: boolean };+ expect(result.isError ?? false).toBe(false);+ expect(readEcho(result).argsType).toBe('object');+ } finally {+ await h.cleanup();+ }+ });});// ── 3. schema 快照不变(规则 10:不改模型看到的工具定义 / 不破坏缓存前缀) ──────@@ -158,17 +208,23 @@ describe('jsonObjectArg — model-facing JSON Schema is byte-identical to legacyit(`stays identical for: ${desc.slice(0, 16)}…`, async () => {// jsonObjectArg 会在 desc 末尾追加固定的 OBJECT_HINT 反例后缀(见 json-object-arg.ts),// legacy 侧用相同拼接来验证描述仍逐字节落在【内层 record】上、不因 preprocess 漂移。+ // 两侧都带 .optional():args 自 2026-10-14 起可选,无参工具(自身 schema 为空对象)省略 args 不再被入口 -32602 拒收。const legacy = await inputSchemaOf({name: z.string().describe('t'),args: z.record(z.string(), z.unknown())- .describe(`${desc}(传 JSON 对象本身,不要序列化成字符串)`),+ .describe(`${desc}(传 JSON 对象本身,不要序列化成字符串)`)+ .optional(),});const next = await inputSchemaOf({name: z.string().describe('t'),args: jsonObjectArg(desc),});expect(JSON.stringify(next)).toBe(JSON.stringify(legacy));+ // 模型看到的契约:args 不在 required(可省略),但仍是 record 而非 any。+ const schema = next as { required?: string[]; properties: Record<string, { type?: string }> };+ expect(schema.required ?? []).not.toContain('args');+ expect(schema.properties.args.type).toBe('object');});}});diff --git a/packages/lizi-mcps/src/cindy_slackMcpServer.ts b/packages/lizi-mcps/src/cindy_slackMcpServer.tsindex 2f0a4c7308..3eb7c1041f 100644--- a/packages/lizi-mcps/src/cindy_slackMcpServer.ts+++ b/packages/lizi-mcps/src/cindy_slackMcpServer.ts@@ -198,7 +198,7 @@ export function createSlackMcpGatewayServer(deps: SlackHookMcpDeps): McpServer {descCallTool.trim(),{name: z.string().min(1).describe('工具名(slack_list_tools 可查)'),- arguments: jsonObjectArg('工具参数(按该工具的 inputSchema 传 JSON 对象; 无参可省略)').optional(),+ arguments: jsonObjectArg('工具参数(按该工具的 inputSchema 传 JSON 对象; 无参可省略)'),team_id: z.string().optional().describe(TEAM_ID_DESC),out_file: z.string()diff --git a/packages/lizi-mcps/src/json-object-arg.ts b/packages/lizi-mcps/src/json-object-arg.tsindex abe896499c..fb040a18d9 100644--- a/packages/lizi-mcps/src/json-object-arg.ts+++ b/packages/lizi-mcps/src/json-object-arg.ts@@ -3,8 +3,14 @@ import { z } from 'zod';/*** string → JSON.parse 出的 object;非 string、或 parse 失败时原样返回,* 交给下游 record 校验报原本的错(不吞坏数据)。+ *+ * null → `{}`:宿主侧实测(2026-10-14),模型明明传了空对象 `args: {}`,到达 MCP+ * 入口时却成了 null(同一构建里省略 args 能过、非空 args 能过,只有空对象形态报+ * `-32602 expected record`),而 `z.record().optional()` 只容忍缺失、不容忍 null。+ * null 与缺失对消费方同义(都是 `registry.call(name, args ?? {})`),统一归一成 `{}`。*/function coerceStringifiedJson(value: unknown): unknown {+ if (value === null) return {};if (typeof value !== 'string') return value;try {return JSON.parse(value);@@ -38,8 +44,15 @@ function coerceStringifiedJson(value: unknown): unknown {const OBJECT_HINT = '(传 JSON 对象本身,不要序列化成字符串)';export function jsonObjectArg(description: string) {+ // .optional() 落在【pipe 外层】,不动内层 record 的描述落点。+ // 为什么必须可选:入口 schema 曾把 args 列为必填,而「自身入参 schema 为空对象」的无参+ // 工具(如 cindy_memory 的 memory_list / memory_review)会被模型按内层 schema 合理地+ // 省略 args——入口 zod 校验直接 -32602 invalid_type,报错指向内部信封字段,模型无法+ // 自纠(2026-10-14 实测:call_tool({name:'memory_list'}) 被拒,带参工具全部正常)。+ // 省略后 args 为 undefined,所有消费方都是 registry.call(name, args ?? {}) /+ // handleCallTool(name, args ?? {}),已容忍;stringify 兜底与坏串报错语义不变。return z.preprocess(coerceStringifiedJson,z.record(z.string(), z.unknown()).describe(`${description}${OBJECT_HINT}`),- );+ ).optional();}diff --git a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.tsindex f17fa1924b..8d433b1655 100644--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts@@ -2448,3 +2448,50 @@ it('applies native Fast only to the exact declared connection and fails closed o} finally { vi.useRealTimers(); }expect(original).not.toHaveProperty('service_tier');});++// PR #6025 回归: cindy_mcp_call_tool 的 args 必须对模型可选 (无参工具按其内层+// 空 schema 省略 args 时, 入口层不能再以 required 拦住)。同时锁死转发值: 省略+// args 时发给 MCP server 的 arguments 必须是 {} (recordInput 归一), 而不是+// undefined / 丢字段 — 否则 call_tool({name, args}) 入口 schema 又会 -32602。+it('lets cindy_mcp_call_tool omit args and forwards an empty object for no-parameter tools', async () => {+ const source = CINDY_BRIDGE_EXTENSION_SOURCE;+ const compiled = ts.transpileModule(+ source.slice(source.indexOf('const CINDY_MCP_LIST_TOOLS'), source.indexOf('async function connectServer'))+ + '\n(globalThis as any).Gateway = CindyMcpGateway;',+ { compilerOptions: { module: ts.ModuleKind.None, target: ts.ScriptTarget.ES2022 } },+ ).outputText;+ const context: Record<string, any> = {+ recordInput: (value: unknown) => value && typeof value === 'object' ? value : {},+ mcpContentToPi: (content: unknown) => content,+ };+ runInNewContext(compiled, context);+ const requests: Array<{ method: string; params: any }> = [];+ const client = { request: async (method: string, params: unknown) => {+ requests.push({ method, params });+ return { content: [{ type: 'text', text: 'ok' }] };+ } };+ const gateway = new context.Gateway();+ gateway.add('cindy_memory', client, [+ { name: 'list_tools', inputSchema: { type: 'object' } },+ { name: 'call_tool', inputSchema: { type: 'object' } },+ ]);+ const ordinary: any[] = [];+ gateway.register({ registerTool: (tool: unknown) => ordinary.push(tool) });+ const callTool = ordinary.find((tool) => tool.name === 'cindy_mcp_call_tool');+ expect(callTool).toBeDefined();+ // 模型看到的契约: server/tool 必填, args 可选 (无参工具可省略)。+ expect(callTool.parameters.required).toEqual(['server', 'tool']);+ expect(callTool.parameters.required).not.toContain('args');+ // 披露门: 先按 schema 检视一次再调用 (与生产路径同序)。+ await gateway.listResult({ server: 'cindy_memory', tool: 'call_tool' });+ await gateway.executeCall({ server: 'cindy_memory', tool: 'call_tool' });+ expect(requests).toHaveLength(1);+ expect(requests[0].method).toBe('tools/call');+ expect(requests[0].params).toEqual({ name: 'call_tool', arguments: {} });+ // 对照: 显式传 args 时仍原样转发 (既有行为不变)。+ await gateway.executeCall({ server: 'cindy_memory', tool: 'call_tool', args: { name: 'memory_list', args: {} } });+ expect(requests[1].params).toEqual({+ name: 'call_tool',+ arguments: { name: 'memory_list', args: {} },+ });+});diff --git a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts b/packages/maker-core/src/agents/pi/cindy-bridge-source.tsindex feee1a1a12..5e3e92159c 100644--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts@@ -3512,7 +3512,8 @@ class CindyMcpGateway {label: 'Call a Cindy MCP tool',description:'Invoke a tool after inspecting its exact input schema with cindy_mcp_list_tools. Pass the exact server and ' +- 'tool names plus args as an object. If the server rejects the arguments, the error repeats that schema.',+ 'tool names plus args as an object; omit args for tools that take no parameters. ' ++ 'If the server rejects the arguments, the error repeats that schema.',parameters: {type: 'object',properties: {@@ -3520,11 +3521,11 @@ class CindyMcpGateway {tool: { type: 'string', description: 'Exact tool name from cindy_mcp_list_tools.' },args: {type: 'object',- description: 'Arguments for the selected tool as a JSON object.',+ description: 'Arguments for the selected tool as a JSON object; omit when the selected tool takes none.',additionalProperties: true,},},- required: ['server', 'tool', 'args'],+ required: ['server', 'tool'],additionalProperties: false,},execute: async (_toolCallId: string, params: unknown, signal?: AbortSignal) => this.executeCall(params, signal),