⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
fix(api): strict MCP clients can call tools with nullable arguments (#811)
Pasqual Tronconee37ce5ac8d4c
diff --git a/apps/api/src/mcp/__tests__/integration/structured-results.test.ts b/apps/api/src/mcp/__tests__/integration/structured-results.test.tsindex c95af148..0e585807 100644--- a/apps/api/src/mcp/__tests__/integration/structured-results.test.ts+++ b/apps/api/src/mcp/__tests__/integration/structured-results.test.ts@@ -2,6 +2,8 @@ import { afterEach, beforeEach, describe, expect, it } from 'bun:test';import { Client } from '@modelcontextprotocol/sdk/client/index.js';import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';import { CallToolResultSchema, type CallToolRequest } from '@modelcontextprotocol/sdk/types.js';+import { AjvJsonSchemaValidator } from '@modelcontextprotocol/sdk/validation/ajv';+import type { JsonSchemaType } from '@modelcontextprotocol/sdk/validation/types.js';import { auth } from '@repo/auth';import { app, authedApi, ownWorkspaceId, createProject } from '#tests/helpers/app';import { signUpTestUser } from '#tests/helpers/auth';@@ -77,6 +79,23 @@ describe('MCP structured results through the SDK client', () => {expect(deleted.structuredContent).toEqual({ ok: true, status: 204, data: null });});+ // A client that checks arguments with Ajv refuses a tool whose schema it cannot compile,+ // even for a call with no arguments (#805).+ it('compiles every advertised input schema in the SDK validator', async () => {+ const user = await signUpTestUser();+ const client = await connect(user.userId);+ const validator = new AjvJsonSchemaValidator();+ const refused: string[] = [];+ for (const tool of (await client.listTools()).tools) {+ try {+ validator.getValidator(tool.inputSchema as JsonSchemaType);+ } catch {+ refused.push(tool.name);+ }+ }+ expect(refused).toEqual([]);+ });+it('creates a checklist batch through the SDK and returns a validated array result', async () => {const owner = await signUpTestUser();const api = authedApi(owner.cookie);diff --git a/apps/api/src/mcp/__tests__/unit/generate.test.ts b/apps/api/src/mcp/__tests__/unit/generate.test.tsindex bcc5759b..4f811b01 100644--- a/apps/api/src/mcp/__tests__/unit/generate.test.ts+++ b/apps/api/src/mcp/__tests__/unit/generate.test.ts@@ -1,4 +1,7 @@import { describe, it, expect } from 'bun:test';+import { t } from 'elysia';+import { AjvJsonSchemaValidator } from '@modelcontextprotocol/sdk/validation/ajv';+import type { JsonSchemaType } from '@modelcontextprotocol/sdk/validation/types.js';import { routeTools, mcpTool, type McpToolAnnotations } from '../../generate';import type { McpApp } from '../../types';@@ -141,6 +144,21 @@ describe('mcp tool input schema', () => {expect(tool.inputSchema.required).not.toContain('targetId');});+ it('keeps a nullable field accepting null without the keyword Ajv refuses', () => {+ const tool = toolWith({+ body: t.Object({+ typeId: t.Optional(t.Nullable(t.Integer())),+ tags: t.Optional(t.Array(t.Nullable(t.String()))),+ }),+ });++ expect(JSON.stringify(tool.inputSchema)).not.toContain('"nullable"');+ const check = new AjvJsonSchemaValidator().getValidator(tool.inputSchema as JsonSchemaType);+ expect(check({ thingId: 'a' }).valid).toBe(true);+ expect(check({ thingId: 'a', typeId: null, tags: [null, 'x'] }).valid).toBe(true);+ expect(check({ thingId: 'a', typeId: true }).valid).toBe(false);+ });+it('reports whether the route declares a body, whatever the method', () => {expect(toolWith({ body: { type: 'object', properties: {} } }).hasBody).toBe(true);expect(toolWith({}, 'GET').hasBody).toBe(false);diff --git a/apps/api/src/mcp/generate.ts b/apps/api/src/mcp/generate.tsindex fd1ac1dd..f4723d24 100644--- a/apps/api/src/mcp/generate.ts+++ b/apps/api/src/mcp/generate.ts@@ -9,7 +9,8 @@ import { outputSchema, type McpOutputSchema } from './result';// so validation, permissions, and the error model are reused, never duplicated.// The JSON Schema an MCP client receives for a tool's arguments. TypeBox emits-// JSON Schema, so the route schemas are merged into this shape as-is.+// JSON Schema, so the route schemas are merged into this shape as they are, except+// for the bare `nullable` (see withoutBareNullable).export interface McpInputSchema {type: 'object';properties: Record<string, unknown>;@@ -123,6 +124,30 @@ interface SchemaShape {oneOf?: SchemaShape[];}+// t.Nullable() adds OpenAPI's `nullable: true` beside an anyOf that already holds the+// `{ type: 'null' }` branch. Ajv, which MCP clients use to check arguments, refuses+// `nullable` without a `type` and so rejects the whole tool. Without a `type` the keyword+// changes nothing a validator accepts, so it is dropped there.+function withoutBareNullable(node: unknown): unknown {+ if (Array.isArray(node)) return node.map(withoutBareNullable);+ if (typeof node !== 'object' || node === null) return node;+ const schema = node as Record<string, unknown>;+ const out: Record<string, unknown> = {};+ for (const [key, value] of Object.entries(schema)) {+ if (key === 'nullable' && !('type' in schema)) continue;+ if (key === 'properties' && typeof value === 'object' && value !== null) {+ out[key] = Object.fromEntries(+ Object.entries(value).map(([name, field]) => [name, withoutBareNullable(field)]),+ );+ } else if (['items', 'additionalProperties', 'not', 'anyOf', 'oneOf', 'allOf'].includes(key)) {+ out[key] = withoutBareNullable(value);+ } else {+ out[key] = value;+ }+ }+ return out;+}+function mergeInputSchema(hooks: Record<string, unknown>, pathParams: string[]): McpInputSchema {const properties: Record<string, unknown> = {};const required: string[] = [];@@ -137,7 +162,9 @@ function mergeInputSchema(hooks: Record<string, unknown>, pathParams: string[]):let common: string[] | null = null;for (const part of parts) {if (!part.properties) continue;- Object.assign(properties, part.properties);+ for (const [name, field] of Object.entries(part.properties)) {+ properties[name] = withoutBareNullable(field);+ }const names = Array.isArray(part.required) ? (part.required as string[]) : [];common = common === null ? names : common.filter((n) => names.includes(n));}