Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions src/sequentialthinking/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, it, expect } from 'vitest';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { coercedBoolean } from '../lib.js';

describe('coercedBoolean - value coercion', () => {
it('accepts real booleans unchanged', () => {
expect(coercedBoolean.parse(true)).toBe(true);
expect(coercedBoolean.parse(false)).toBe(false);
});

it('coerces the strings "true"/"false" (case-insensitive)', () => {
expect(coercedBoolean.parse('true')).toBe(true);
expect(coercedBoolean.parse('false')).toBe(false);
expect(coercedBoolean.parse('TRUE')).toBe(true);
expect(coercedBoolean.parse('False')).toBe(false);
});

it('rejects other values instead of silently passing them through', () => {
// Regression guard: a naive coercion that returns the raw value would let
// the truthy string "false" through as `true`; these must all fail.
expect(coercedBoolean.safeParse('yes').success).toBe(false);
expect(coercedBoolean.safeParse('').success).toBe(false);
expect(coercedBoolean.safeParse(1).success).toBe(false);
expect(coercedBoolean.safeParse(null).success).toBe(false);
});
});

describe('coercedBoolean - JSON Schema generation (issue #4651)', () => {
async function toolInputSchema(inputSchema: Record<string, unknown>) {
const server = new McpServer({ name: 'test', version: '0.0.0' });
server.registerTool(
'probe',
{ inputSchema: inputSchema as never },
async () => ({ content: [] }),
);

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
const client = new Client({ name: 'test-client', version: '0.0.0' });
await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]);

const { tools } = await client.listTools();
await client.close();
return tools.find((t) => t.name === 'probe')!.inputSchema;
}

it('keeps a non-optional coercedBoolean field in `required`', async () => {
const schema = await toolInputSchema({ flag: coercedBoolean });
expect(schema.required).toContain('flag');
});

it('omits an .optional() coercedBoolean field from `required`', async () => {
const schema = await toolInputSchema({ flag: coercedBoolean.optional() });
expect(schema.required ?? []).not.toContain('flag');
});
});
12 changes: 1 addition & 11 deletions src/sequentialthinking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { SequentialThinkingServer } from './lib.js';

/** Safe boolean coercion that correctly handles string "false" */
const coercedBoolean = z.preprocess((val) => {
if (typeof val === "boolean") return val;
if (typeof val === "string") {
if (val.toLowerCase() === "true") return true;
if (val.toLowerCase() === "false") return false;
}
return val;
}, z.boolean());
import { SequentialThinkingServer, coercedBoolean } from './lib.js';

const server = new McpServer({
name: "sequential-thinking-server",
Expand Down
21 changes: 21 additions & 0 deletions src/sequentialthinking/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
import chalk from 'chalk';
import { z } from 'zod';

/**
* Boolean coercion that also accepts the strings "true"/"false" (case-insensitive),
* which some MCP clients send for boolean arguments.
*
* Implemented as a union rather than `z.preprocess(fn, z.boolean())`: `z.preprocess`
* widens the *input* type to `unknown`, so JSON Schema generation (`io: "input"`)
* treats the field as optional and drops it from `required` even when it is not
* `.optional()`. A union keeps a concrete input type, so required fields stay required.
*/
export const coercedBoolean = z.union([
z.boolean(),
z.string().transform((val, ctx) => {
const normalized = val.toLowerCase();
if (normalized === 'true') return true;
if (normalized === 'false') return false;
ctx.addIssue({ code: 'custom', message: `Expected "true" or "false", received "${val}"` });
return z.NEVER;
}),
]);

export interface ThoughtData {
thought: string;
Expand Down
3 changes: 2 additions & 1 deletion src/sequentialthinking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"dependencies": {
"@modelcontextprotocol/sdk": "^1.30.0",
"chalk": "^5.3.0",
"yargs": "^17.7.2"
"yargs": "^17.7.2",
"zod": "^4.0.0"
},
"devDependencies": {
"@types/node": "^22",
Expand Down