From 79a0301bb8005143d4209aa635fc2e01e3cf8fad Mon Sep 17 00:00:00 2001 From: Christoph Dyllick-Brenzinger Date: Wed, 4 Nov 2026 16:54:16 +0100 Subject: [PATCH] Upgrade to zod 4 and bound tool input arrays (v1.9.3) Zod reports one issue per failing array element, so an unbounded array in a tool call could exhaust the process memory. Zod 4 checks .max() before it parses elements, but only where a bound is declared, so the upgrade needs bounds on the remaining arrays: parameters (1000), key_columns (100) and options (1000). zod-to-json-schema does not read zod 4 schemas and is replaced by z.toJSONSchema with io: 'input', so fields with a default stay optional. Two silent changes of the migration are kept out and pinned by tests: TRUST_PROXY would have defaulted to the string 'true', and find_rows would have required where. Co-Authored-By: Claude Opus 5.5 --- package-lock.json | 13 +++-- package.json | 5 +- src/config/env.ts | 4 +- src/mcp/server.ts | 7 +-- src/mcp/tools/addRow.ts | 2 +- src/mcp/tools/addSelectOption.ts | 2 +- src/mcp/tools/executeSql.ts | 2 +- src/mcp/tools/findRows.ts | 2 +- src/mcp/tools/querySql.ts | 2 +- src/mcp/tools/searchRows.ts | 2 +- src/mcp/tools/updateRow.ts | 2 +- src/mcp/tools/upsertRows.ts | 2 +- src/schema/generic.ts | 2 +- src/schema/map.ts | 2 +- tests/inputBounds.spec.ts | 51 ++++++++++++++++++ tests/toolInputSchemas.spec.ts | 92 ++++++++++++++++++++++++++++++++ 16 files changed, 164 insertions(+), 28 deletions(-) create mode 100644 tests/inputBounds.spec.ts create mode 100644 tests/toolInputSchemas.spec.ts diff --git a/package-lock.json b/package-lock.json index f5d4600..272a080 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@seatable/mcp-seatable", - "version": "1.9.2", + "version": "1.9.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@seatable/mcp-seatable", - "version": "1.9.2", + "version": "1.9.3", "license": "MIT", "dependencies": { "@modelcontextprotocol/sdk": "^1.17.4", @@ -17,8 +17,7 @@ "pino": "^9.4.0", "prom-client": "^15.1.3", "unpdf": "^1.4.0", - "zod": "^3.23.8", - "zod-to-json-schema": "^3.24.6" + "zod": "^4.6.7" }, "bin": { "mcp-seatable": "bin/seatable-mcp.cjs", @@ -6415,9 +6414,9 @@ } }, "node_modules/zod": { - "version": "3.25.76", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz", - "integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==", + "version": "4.6.7", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.6.7.tgz", + "integrity": "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==", "license": "MIT", "funding": { "url": "https://github.com/sponsors/colinhacks" diff --git a/package.json b/package.json index fb3987f..bb77bdc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@seatable/mcp-seatable", - "version": "1.9.2", + "version": "1.9.3", "type": "module", "license": "MIT", "mcpName": "io.github.seatable/seatable", @@ -75,8 +75,7 @@ "pino": "^9.4.0", "prom-client": "^15.1.3", "unpdf": "^1.4.0", - "zod": "^3.23.8", - "zod-to-json-schema": "^3.24.6" + "zod": "^4.6.7" }, "devDependencies": { "@types/node": "^22.5.4", diff --git a/src/config/env.ts b/src/config/env.ts index a59829d..ae3f222 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -62,9 +62,7 @@ const EnvSchema = z TRUST_PROXY: z .string() .optional() - .transform((v) => v !== '0' && v !== 'false') - .optional() - .default('true'), + .transform((v) => v !== '0' && v !== 'false'), }) .superRefine((data, ctx) => { if (data.SEATABLE_MODE === 'managed' && !data.SEATABLE_TOKEN_SECRET) { diff --git a/src/mcp/server.ts b/src/mcp/server.ts index 15095ad..bd20d28 100644 --- a/src/mcp/server.ts +++ b/src/mcp/server.ts @@ -9,7 +9,6 @@ import { type ListToolsResult, } from '@modelcontextprotocol/sdk/types.js' import { z } from 'zod' -import { zodToJsonSchema } from 'zod-to-json-schema' import { getEnv, parseBases, VERSION } from '../config/env.js' import type { CodedError } from '../errors.js' @@ -48,10 +47,8 @@ import { registerUpsertRows } from './tools/upsertRows.js' // Helper function to convert Zod schemas to JSON Schema for MCP tools const getInputSchema = (schema: z.ZodType): ListToolsResult['tools'][0]['inputSchema'] => { - const jsonSchema = zodToJsonSchema(schema, { - target: 'jsonSchema7', - strictUnions: true - }) + // io: 'input' describes what callers send, so fields with a default stay optional. + const jsonSchema = z.toJSONSchema(schema, { target: 'draft-7', io: 'input' }) as Record if (!('type' in jsonSchema) || jsonSchema.type !== 'object') { throw new Error(`Invalid input schema: expected an object but got ${ diff --git a/src/mcp/tools/addRow.ts b/src/mcp/tools/addRow.ts index 038e4f5..792605f 100644 --- a/src/mcp/tools/addRow.ts +++ b/src/mcp/tools/addRow.ts @@ -6,7 +6,7 @@ import { ToolRegistrar } from './types.js' const InputSchema = z.object({ table: z.string().describe('Target table name'), - row: z.record(z.any()).describe('Row object (column -> value)'), + row: z.record(z.string(), z.any()).describe('Row object (column -> value)'), }) export const registerAddRow: ToolRegistrar = (server, { client, getInputSchema }) => { diff --git a/src/mcp/tools/addSelectOption.ts b/src/mcp/tools/addSelectOption.ts index 68d0a29..2176fb7 100644 --- a/src/mcp/tools/addSelectOption.ts +++ b/src/mcp/tools/addSelectOption.ts @@ -10,7 +10,7 @@ const InputSchema = z.object({ name: z.string().describe('Option label'), color: z.string().optional().describe('Background color (hex, e.g. "#FF8000")'), textColor: z.string().optional().describe('Text color (hex, e.g. "#FFFFFF")'), - })).min(1).describe('Array of options to add'), + })).min(1).max(1000).describe('Array of options to add'), }) /** Names of the options a select column already has. Empty if the column has none yet. */ diff --git a/src/mcp/tools/executeSql.ts b/src/mcp/tools/executeSql.ts index 9bccd1c..200ab90 100644 --- a/src/mcp/tools/executeSql.ts +++ b/src/mcp/tools/executeSql.ts @@ -6,7 +6,7 @@ import { cleanSqlMetadata } from './utils.js' const InputSchema = z.object({ sql: z.string().describe('UPDATE, DELETE or INSERT statement').refine(sql => sql.trim().length > 0, 'SQL statement cannot be empty'), - parameters: z.array(z.union([z.string(), z.number(), z.boolean(), z.null()])).optional().describe('Values for ? placeholders in the SQL statement'), + parameters: z.array(z.union([z.string(), z.number(), z.boolean(), z.null()])).max(1000).optional().describe('Values for ? placeholders in the SQL statement'), }) export const registerExecuteSql: ToolRegistrar = (server, { client, getInputSchema }) => { diff --git a/src/mcp/tools/findRows.ts b/src/mcp/tools/findRows.ts index 62e2469..1a9f4b7 100644 --- a/src/mcp/tools/findRows.ts +++ b/src/mcp/tools/findRows.ts @@ -41,7 +41,7 @@ export type Where = z.infer // Use a loose schema for boundary parsing const InputSchema = z.object({ table: z.string().describe('Target table name'), - where: z.unknown().describe('Filter predicate (e.g. {"eq":{"field":"Name","value":"foo"}} or shorthand {"Name":"foo"})'), + where: z.unknown().optional().describe('Filter predicate (e.g. {"eq":{"field":"Name","value":"foo"}} or shorthand {"Name":"foo"})'), page: z.number().int().min(1).optional().default(1).describe('Page number (1-based)'), page_size: z.number().int().min(1).max(1000).optional().default(100).describe('Rows per page (max 1000)'), order_by: z.string().optional().describe('Column name to sort by'), diff --git a/src/mcp/tools/querySql.ts b/src/mcp/tools/querySql.ts index 094f66d..725f4c2 100644 --- a/src/mcp/tools/querySql.ts +++ b/src/mcp/tools/querySql.ts @@ -6,7 +6,7 @@ import { cleanSqlMetadata } from './utils.js' const InputSchema = z.object({ sql: z.string().describe('SELECT statement').refine(sql => sql.trim().length > 0, 'SQL query cannot be empty'), - parameters: z.array(z.union([z.string(), z.number(), z.boolean(), z.null()])).optional().describe('Values for ? placeholders in the SQL query'), + parameters: z.array(z.union([z.string(), z.number(), z.boolean(), z.null()])).max(1000).optional().describe('Values for ? placeholders in the SQL query'), }) export const registerQuerySql: ToolRegistrar = (server, { client, getInputSchema }) => { diff --git a/src/mcp/tools/searchRows.ts b/src/mcp/tools/searchRows.ts index d2ba5d7..12cedfd 100644 --- a/src/mcp/tools/searchRows.ts +++ b/src/mcp/tools/searchRows.ts @@ -4,7 +4,7 @@ import { ToolRegistrar } from './types.js' const InputSchema = z.object({ table: z.string().describe('Target table name'), - query: z.record(z.any()).describe('Filter object with column name -> value pairs'), + query: z.record(z.string(), z.any()).describe('Filter object with column name -> value pairs'), }) export const registerSearchRows: ToolRegistrar = (server, { client, getInputSchema }) => { diff --git a/src/mcp/tools/updateRow.ts b/src/mcp/tools/updateRow.ts index 33c4d74..9e34bf2 100644 --- a/src/mcp/tools/updateRow.ts +++ b/src/mcp/tools/updateRow.ts @@ -6,7 +6,7 @@ import { ToolRegistrar } from './types.js' const UpdateItem = z.object({ row_id: z.string().describe('Row ID (_id field) to update'), - values: z.record(z.any()).describe('Column name -> new value pairs'), + values: z.record(z.string(), z.any()).describe('Column name -> new value pairs'), }) const InputSchema = z.object({ diff --git a/src/mcp/tools/upsertRows.ts b/src/mcp/tools/upsertRows.ts index ae17942..036d405 100644 --- a/src/mcp/tools/upsertRows.ts +++ b/src/mcp/tools/upsertRows.ts @@ -7,7 +7,7 @@ import { ToolRegistrar } from './types.js' const InputSchema = z.object({ table: z.string().describe('Target table name'), - key_columns: z.array(z.string()).min(1).describe('Columns to match on for finding existing rows'), + key_columns: z.array(z.string()).min(1).max(100).describe('Columns to match on for finding existing rows'), rows: z.array(z.record(z.string(), z.any())).min(1).max(100).describe('Array of row objects (column name -> value)'), }) diff --git a/src/schema/generic.ts b/src/schema/generic.ts index 2419b99..37a84d8 100644 --- a/src/schema/generic.ts +++ b/src/schema/generic.ts @@ -34,7 +34,7 @@ export const GenericColumnSchema = z.object({ id: z.string(), name: z.string(), type: GenericColumnType, - options: z.record(z.unknown()).optional(), + options: z.record(z.string(), z.unknown()).optional(), }) export type GenericColumn = z.infer diff --git a/src/schema/map.ts b/src/schema/map.ts index df53969..e7ba97a 100644 --- a/src/schema/map.ts +++ b/src/schema/map.ts @@ -7,7 +7,7 @@ const ColumnSchema = z.object({ key: z.string(), name: z.string(), type: z.string(), - data: z.record(z.unknown()).nullable().optional(), + data: z.record(z.string(), z.unknown()).nullable().optional(), description: z.string().nullable().optional(), }) const TableSchema = z diff --git a/tests/inputBounds.spec.ts b/tests/inputBounds.spec.ts new file mode 100644 index 0000000..41b3fc9 --- /dev/null +++ b/tests/inputBounds.spec.ts @@ -0,0 +1,51 @@ +import { beforeAll, describe, expect, it } from 'vitest' + +import { buildServer, SeaTableMCPServer } from '../src/mcp/server' + +/* + * Zod collects one issue per failing element, so an unbounded array turns a + * request body into many times its size in issue objects. A single 10 MB call + * killed the process this way. Zod 4 checks .max() before it parses elements, + * but only where a bound is declared, so every array a caller controls needs one. + */ + +const callTool = (server: SeaTableMCPServer, name: string, args: Record) => + (server as any).handleCallTool({ params: { name, arguments: args } }) + +const select = { table: 'Table1', column: 'Status' } + +const OVERSIZED: Array<[string, string, (n: number) => Record]> = [ + ['query_sql', 'parameters', (n) => ({ sql: 'SELECT 1', parameters: new Array(n).fill({}) })], + ['execute_sql', 'parameters', (n) => ({ sql: 'DELETE FROM `T`', parameters: new Array(n).fill({}) })], + ['upsert_rows', 'key_columns', (n) => ({ table: 'T', key_columns: new Array(n).fill(0), rows: [{ a: 1 }] })], + ['add_select_options', 'options', (n) => ({ ...select, options: new Array(n).fill(0) })], + ['append_rows', 'rows', (n) => ({ table: 'T', rows: new Array(n).fill(0) })], +] + +describe('tool input arrays are bounded', () => { + let server: SeaTableMCPServer + + beforeAll(() => { + process.env.SEATABLE_SERVER_URL = 'http://localhost' + process.env.SEATABLE_API_TOKEN = 'test-token' + process.env.SEATABLE_MOCK = 'true' + server = buildServer() + }) + + it.each(OVERSIZED)('%s rejects an oversized %s with one short error', async (tool, field, args) => { + const result = await callTool(server, tool, args(200_000)) + + expect(result.isError).toBe(true) + const text = result.content[0].text as string + expect(text).toContain(field) + // One issue about the size, not one per element. + expect(text.length).toBeLessThan(2_000) + }) + + it.each(OVERSIZED.slice(0, 2))('%s still accepts a normal number of %s', async (tool, _field, _args) => { + const sql = tool === 'query_sql' ? 'SELECT * FROM `Table1` WHERE `Name` = ?' : 'DELETE FROM `Table1` WHERE `Name` = ?' + const result = await callTool(server, tool, { sql, parameters: ['a'] }) + + expect(result.isError).toBeFalsy() + }) +}) diff --git a/tests/toolInputSchemas.spec.ts b/tests/toolInputSchemas.spec.ts new file mode 100644 index 0000000..ed3c4b3 --- /dev/null +++ b/tests/toolInputSchemas.spec.ts @@ -0,0 +1,92 @@ +import { beforeAll, describe, expect, it } from 'vitest' + +import { getEnv } from '../src/config/env' +import { getStaticToolDefinitions } from '../src/mcp/server' + +/* + * The JSON Schema is what clients see. A field with a default has to stay + * optional: converted from the parsed output instead of the input it becomes + * required, and models start failing on calls that used to work. + */ +const REQUIRED: Record = { + list_tables: [], + list_rows: ['table'], + get_row: ['table', 'row_id'], + add_row: ['table', 'row'], + append_rows: ['table', 'rows'], + update_rows: ['table', 'updates'], + delete_rows: ['table', 'row_ids'], + find_rows: ['table'], + search_rows: ['table', 'query'], + upsert_rows: ['table', 'key_columns', 'rows'], + link_rows: ['table', 'link_column', 'pairs'], + unlink_rows: ['table', 'link_column', 'pairs'], + get_schema: [], + query_sql: ['sql'], + execute_sql: ['sql'], + list_collaborators: [], + upload_file: ['table', 'column', 'row_id', 'file_name', 'file_data'], + download_file: ['table', 'column', 'row_id'], + create_snapshot: [], + get_row_activities: ['table', 'row_id'], + add_select_options: ['table', 'column', 'options'], + ping_seatable: [], +} + +describe('tool input schemas', () => { + let tools: Array<{ name: string; inputSchema: any }> + + beforeAll(() => { + process.env.SEATABLE_SERVER_URL = 'http://localhost' + process.env.SEATABLE_API_TOKEN = 'test-token' + process.env.SEATABLE_MOCK = 'true' + tools = getStaticToolDefinitions() + }) + + it('keep their required fields', () => { + const actual = Object.fromEntries(tools.map((t) => [t.name, t.inputSchema.required ?? []])) + expect(actual).toEqual(REQUIRED) + }) + + it('are objects that allow additional properties', () => { + // These two declare their schema by hand instead of converting one. + const handWritten = ['list_tables', 'list_collaborators'] + for (const { name, inputSchema } of tools) { + expect(inputSchema.type, name).toBe('object') + if (!handWritten.includes(name)) expect(inputSchema.additionalProperties, name).toBe(true) + } + }) + + it('keep descriptions, defaults and limits', () => { + const findRows = tools.find((t) => t.name === 'find_rows')!.inputSchema.properties + expect(findRows.table.description).toBe('Target table name') + expect(findRows.page.default).toBe(1) + expect(findRows.page_size.maximum).toBe(1000) + + const appendRows = tools.find((t) => t.name === 'append_rows')!.inputSchema.properties + expect(appendRows.rows.maxItems).toBe(100) + }) +}) + +describe('TRUST_PROXY', () => { + const withValue = (value: string | undefined) => { + const before = process.env.TRUST_PROXY + if (value === undefined) delete process.env.TRUST_PROXY + else process.env.TRUST_PROXY = value + try { + return getEnv().TRUST_PROXY + } finally { + if (before === undefined) delete process.env.TRUST_PROXY + else process.env.TRUST_PROXY = before + } + } + + it('is on unless switched off', () => { + process.env.SEATABLE_SERVER_URL = 'http://localhost' + process.env.SEATABLE_API_TOKEN = 'test-token' + expect(withValue(undefined)).toBe(true) + expect(withValue('true')).toBe(true) + expect(withValue('0')).toBe(false) + expect(withValue('false')).toBe(false) + }) +})