Skip to content

Commit 86806cc

Browse files
cliffhallclaude
andcommitted
test(sequentialthinking): in-process protocol suite and per-file 90% coverage gate
Part of #4854. - index.ts: export createServer() and main(transport); attach stdio only when the module is the process entry point (realpath-compared, so the npx/.bin symlink still starts). Wire output is byte-identical. - New vitest suite driving the server through an SDK Client over InMemoryTransport: tools/list (annotations per #4721 and the 2781-char description per #799 pinned as they are today), input schema (ported from the stdio test, no longer skipped without dist/), history, totalThoughts adjustment, revisions, branches, the #4813 Object.prototype branch-id collision, and stderr thought logging. - Thin stdio smoke for the built bin (direct and via symlink). - vitest.config.ts: perFile 90/90/90/90 thresholds. - Remove lib.test.ts (every case now covered through the protocol). Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 9c0cf8f commit 86806cc

12 files changed

Lines changed: 1348 additions & 490 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-sequential-thinking": patch
3+
---
4+
5+
Build the server with an exported `createServer()` factory and attach stdio only when `index.js` runs as the binary, so the server can be tested in-process. No change to the tool, its schemas or its results.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// The in-process half of the entry point: `main()` connects a server to the
2+
// transport it is given, and `isEntryPoint()` decides whether index.ts was run
3+
// as the binary (and so attaches stdio) or merely imported, as these tests do.
4+
// The bootstrap that calls them only runs in a spawned process, which
5+
// stdio-smoke.test.ts covers.
6+
7+
import {
8+
describe,
9+
it,
10+
expect,
11+
beforeEach,
12+
afterEach,
13+
vi,
14+
type MockInstance,
15+
} from "vitest";
16+
import { mkdtempSync, rmSync, symlinkSync } from "node:fs";
17+
import { tmpdir } from "node:os";
18+
import path from "node:path";
19+
import { fileURLToPath, pathToFileURL } from "node:url";
20+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
21+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
22+
import { isEntryPoint, main } from "../index.js";
23+
24+
const indexUrl = new URL("../index.ts", import.meta.url).href;
25+
const indexPath = fileURLToPath(indexUrl);
26+
27+
describe("main", () => {
28+
let errorSpy: MockInstance<typeof console.error>;
29+
30+
beforeEach(() => {
31+
errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
32+
});
33+
34+
afterEach(() => {
35+
errorSpy.mockRestore();
36+
});
37+
38+
it("serves the tool on the given transport and announces itself on stderr", async () => {
39+
const [clientTransport, serverTransport] =
40+
InMemoryTransport.createLinkedPair();
41+
const client = new Client({ name: "main-test", version: "0.0.0" });
42+
try {
43+
await Promise.all([
44+
main(serverTransport),
45+
client.connect(clientTransport),
46+
]);
47+
expect(errorSpy).toHaveBeenCalledWith(
48+
"Sequential Thinking MCP Server running on stdio",
49+
);
50+
const { tools } = await client.listTools();
51+
expect(tools.map((t) => t.name)).toEqual(["sequentialthinking"]);
52+
} finally {
53+
await client.close();
54+
}
55+
});
56+
57+
it("rejects when the transport cannot start", async () => {
58+
const [, serverTransport] = InMemoryTransport.createLinkedPair();
59+
vi.spyOn(serverTransport, "start").mockRejectedValue(new Error("no pipe"));
60+
await expect(main(serverTransport)).rejects.toThrow("no pipe");
61+
expect(errorSpy).not.toHaveBeenCalled();
62+
});
63+
});
64+
65+
describe("isEntryPoint", () => {
66+
let dir: string;
67+
68+
beforeEach(() => {
69+
dir = mkdtempSync(path.join(tmpdir(), "seqthink-entry-"));
70+
});
71+
72+
afterEach(() => {
73+
rmSync(dir, { recursive: true, force: true });
74+
});
75+
76+
it("is false when the module is imported (argv[1] is the test runner)", () => {
77+
expect(isEntryPoint(indexUrl, process.argv[1])).toBe(false);
78+
});
79+
80+
it("is false with no argv[1]", () => {
81+
expect(isEntryPoint(indexUrl, undefined)).toBe(false);
82+
expect(isEntryPoint(indexUrl, "")).toBe(false);
83+
});
84+
85+
it("is true when argv[1] is the module itself", () => {
86+
expect(isEntryPoint(indexUrl, indexPath)).toBe(true);
87+
});
88+
89+
it("is true when argv[1] is a symlink to the module, as npx and .bin use", () => {
90+
const link = path.join(dir, "mcp-server-sequential-thinking");
91+
symlinkSync(indexPath, link);
92+
expect(isEntryPoint(indexUrl, link)).toBe(true);
93+
});
94+
95+
it("is false when argv[1] does not exist", () => {
96+
expect(isEntryPoint(indexUrl, path.join(dir, "missing.js"))).toBe(false);
97+
});
98+
99+
it("is false when the module URL does not resolve to a file", () => {
100+
expect(
101+
isEntryPoint(pathToFileURL(path.join(dir, "gone.js")).href, indexPath),
102+
).toBe(false);
103+
});
104+
});
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Shared harness for the in-process protocol tests (#4854). It links a real
2+
// SDK Client to a server built by `createServer()` over an InMemoryTransport,
3+
// so every test goes through the SDK's input validation, output-schema
4+
// validation and error mapping exactly as a client on the wire would, with no
5+
// process and no build. Tests use only the public Client API and the wire
6+
// shapes it returns, so the Part 3 SDK migration can update them with import
7+
// and type changes alone.
8+
9+
import { expect } from "vitest";
10+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
11+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
12+
import { createServer } from "../index.js";
13+
14+
export const TOOL_NAME = "sequentialthinking";
15+
16+
/** The JSON payload of a successful call, as both text and structuredContent. */
17+
export interface ThoughtResult {
18+
thoughtNumber: number;
19+
totalThoughts: number;
20+
nextThoughtNeeded: boolean;
21+
branches: string[];
22+
thoughtHistoryLength: number;
23+
}
24+
25+
/** A tool result as the client sees it on the wire. */
26+
export interface WireResult {
27+
content: Array<{ type: string; text?: string }>;
28+
structuredContent?: Record<string, unknown>;
29+
isError?: boolean;
30+
}
31+
32+
export interface Connected {
33+
client: Client;
34+
/** Call the tool with raw (unvalidated) arguments, as a client would. */
35+
think: (args: Record<string, unknown>) => Promise<WireResult>;
36+
close: () => Promise<void>;
37+
}
38+
39+
export interface ConnectOptions {
40+
/**
41+
* The DISABLE_THOUGHT_LOGGING value the server is constructed under
42+
* (`undefined` = unset). It is read once, when the server is built, so it is
43+
* set only around `createServer()` and restored at once. Defaults to "true",
44+
* which keeps the per-thought box off stderr in tests not about logging.
45+
*/
46+
disableThoughtLogging?: string | undefined;
47+
}
48+
49+
/** Build a fresh server and connect a client to it in-process. */
50+
export async function connect(
51+
options: ConnectOptions = { disableThoughtLogging: "true" },
52+
): Promise<Connected> {
53+
const previous = process.env.DISABLE_THOUGHT_LOGGING;
54+
setEnv(options.disableThoughtLogging);
55+
let server: ReturnType<typeof createServer>;
56+
try {
57+
server = createServer();
58+
} finally {
59+
setEnv(previous);
60+
}
61+
const [clientTransport, serverTransport] =
62+
InMemoryTransport.createLinkedPair();
63+
const client = new Client({ name: "test-client", version: "0.0.0" });
64+
await Promise.all([
65+
server.connect(serverTransport),
66+
client.connect(clientTransport),
67+
]);
68+
return {
69+
client,
70+
think: async (args) =>
71+
(await client.callTool({
72+
name: TOOL_NAME,
73+
arguments: args,
74+
})) as WireResult,
75+
close: async () => {
76+
await client.close();
77+
await server.close();
78+
},
79+
};
80+
}
81+
82+
/** The text of a result's single content block. */
83+
export function textOf(result: WireResult): string {
84+
expect(result.content).toHaveLength(1);
85+
expect(result.content[0].type).toBe("text");
86+
return result.content[0].text ?? "";
87+
}
88+
89+
/** Parse a successful result, checking text and structuredContent agree. */
90+
export function parseOk(result: WireResult): ThoughtResult {
91+
expect(result.isError).toBeFalsy();
92+
const parsed = JSON.parse(textOf(result)) as ThoughtResult;
93+
expect(result.structuredContent).toEqual(parsed);
94+
return parsed;
95+
}
96+
97+
/** Minimal valid arguments, overridable per test. */
98+
export function thought(
99+
overrides: Record<string, unknown> = {},
100+
): Record<string, unknown> {
101+
return {
102+
thought: "a thought",
103+
nextThoughtNeeded: true,
104+
thoughtNumber: 1,
105+
totalThoughts: 3,
106+
...overrides,
107+
};
108+
}
109+
110+
function setEnv(value: string | undefined): void {
111+
if (value === undefined) {
112+
delete process.env.DISABLE_THOUGHT_LOGGING;
113+
} else {
114+
process.env.DISABLE_THOUGHT_LOGGING = value;
115+
}
116+
}

0 commit comments

Comments
 (0)