Skip to content

Commit 4553ee6

Browse files
cliffhallclaude
andcommitted
test(filesystem): characterize every tool in-process and gate coverage at 90 per file
Add a protocol-level vitest suite that drives the filesystem server through an SDK Client over an in-memory transport, pinning current SDK 1.x behavior: all 14 tools, annotations and schemas, structured content, path validation (traversal, symlinks, relative, ~, Unicode, Windows/UNC via a win32 path mock), Roots handling, and the stdio transport in-process. Known bugs are pinned with their issue numbers (#3602, #3204, #3199, #4512, #2034, #1970, #3527, #3402, #4841, #4206, #4207, #4195). To make that possible without changing behavior: - server.ts exports createServer(allowedDirectories); index.ts resolves argv and connects stdio inside main(), run only when the file is the entry point. - lib.ts no longer keeps the allow-list in a module global; validatePath takes it as a parameter, so each server instance validates against its own. vitest.config.ts enforces 90/90/90/90 per file; unreachable code carries a justified v8 ignore. Spawn-based tests are replaced by in-process ones plus one thin bin smoke. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 2e6224b commit 4553ee6

23 files changed

Lines changed: 3305 additions & 1746 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
Internal: the server is now built by a `createServer()` factory in `server.ts`, and `index.ts` starts it over stdio only when run as the bin, with each server instance holding its own allowed directories. No change to the tools, their results, or how the allowed directories are resolved from arguments and Roots.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// The one spawn-based test (#4854): it launches the built bin the way a user
2+
// does and checks that it boots, answers initialize with the package version
3+
// and serves a tool. Everything else runs in-process, so this only guards the
4+
// entry-point wiring that in-process tests cannot reach (the isEntryPoint
5+
// guard and process stdio). It needs `dist/`, which `npm run validate` builds
6+
// before testing; a bare `npm test` on a clean checkout skips it.
7+
8+
import { existsSync } from "node:fs";
9+
import { createRequire } from "node:module";
10+
import fs from "node:fs/promises";
11+
import path from "node:path";
12+
import { describe, expect, it } from "vitest";
13+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
14+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
15+
import { makeTempDir } from "./helpers.js";
16+
17+
const packageRoot = path.join(import.meta.dirname, "..");
18+
const distIndexPath = path.join(packageRoot, "dist", "index.js");
19+
const packageJson = createRequire(import.meta.url)("../package.json") as {
20+
version: string;
21+
};
22+
23+
describe("the built bin", () => {
24+
it.skipIf(!existsSync(distIndexPath))(
25+
"boots over stdio and serves a tool",
26+
async () => {
27+
const dir = await makeTempDir("mcp-fs-smoke-");
28+
const client = new Client({ name: "bin-smoke", version: "0.0.0" });
29+
try {
30+
await client.connect(
31+
new StdioClientTransport({
32+
command: process.execPath,
33+
args: [distIndexPath, dir],
34+
stderr: "pipe",
35+
}),
36+
);
37+
expect(client.getServerVersion()).toEqual({
38+
name: "secure-filesystem-server",
39+
version: packageJson.version,
40+
});
41+
const result = await client.callTool({
42+
name: "list_allowed_directories",
43+
arguments: {},
44+
});
45+
expect(result.structuredContent).toEqual({
46+
content: `Allowed directories:\n${dir}`,
47+
});
48+
} finally {
49+
await client.close();
50+
await fs.rm(dir, { recursive: true, force: true });
51+
}
52+
},
53+
);
54+
});

‎src/filesystem/__tests__/directory-tree.mcp-sdk.test.ts‎

Lines changed: 0 additions & 57 deletions
This file was deleted.

‎src/filesystem/__tests__/directory-tree.test.ts‎

Lines changed: 0 additions & 174 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Shared harness for the filesystem protocol tests (#4854). It links a real
2+
// SDK Client to a server built by createServer() over an in-memory transport,
3+
// so every assertion is on what crosses the wire: the tool list, call results
4+
// and errors. Tests touch only the public Client API and wire shapes, so the
5+
// SDK v2 codemod can update them with import and type changes alone.
6+
7+
import fs from "fs/promises";
8+
import os from "os";
9+
import path from "path";
10+
import { vi } from "vitest";
11+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
12+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
13+
import {
14+
ListRootsRequestSchema,
15+
type CallToolResult,
16+
type ClientCapabilities,
17+
type Root,
18+
} from "@modelcontextprotocol/sdk/types.js";
19+
import { createServer } from "../server.js";
20+
21+
export interface Connected {
22+
client: Client;
23+
close: () => Promise<void>;
24+
}
25+
26+
export interface ConnectOptions {
27+
/** Client capabilities to declare; `roots` makes the server ask for roots. */
28+
capabilities?: ClientCapabilities;
29+
/** Answer for `roots/list`; installed only when `capabilities.roots` is set. */
30+
listRoots?: () => Root[] | Promise<Root[]>;
31+
}
32+
33+
/** Build a server for `allowedDirectories` and connect a Client to it in-process. */
34+
export async function connect(
35+
allowedDirectories: string[],
36+
options: ConnectOptions = {},
37+
): Promise<Connected> {
38+
const server = createServer(allowedDirectories);
39+
const client = new Client(
40+
{ name: "filesystem-test-client", version: "0.0.0" },
41+
{ capabilities: options.capabilities ?? {} },
42+
);
43+
const { listRoots } = options;
44+
if (options.capabilities?.roots && listRoots) {
45+
client.setRequestHandler(ListRootsRequestSchema, async () => ({
46+
roots: await listRoots(),
47+
}));
48+
}
49+
const [clientTransport, serverTransport] =
50+
InMemoryTransport.createLinkedPair();
51+
await Promise.all([
52+
server.connect(serverTransport),
53+
client.connect(clientTransport),
54+
]);
55+
return {
56+
client,
57+
close: async () => {
58+
await client.close();
59+
await server.close();
60+
},
61+
};
62+
}
63+
64+
/** Call a tool and return its result, typed as a CallToolResult. */
65+
export async function call(
66+
client: Client,
67+
name: string,
68+
args: Record<string, unknown> = {},
69+
): Promise<CallToolResult> {
70+
return (await client.callTool({
71+
name,
72+
arguments: args,
73+
})) as CallToolResult;
74+
}
75+
76+
/** The text of a result's single text content block. */
77+
export function textOf(result: CallToolResult): string {
78+
const [block] = result.content;
79+
if (block?.type !== "text") {
80+
throw new Error(`expected a text block, got ${JSON.stringify(block)}`);
81+
}
82+
return block.text;
83+
}
84+
85+
/** A fresh realpath'd temporary directory (macOS /var is a symlink). */
86+
export async function makeTempDir(prefix: string): Promise<string> {
87+
return fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), prefix)));
88+
}
89+
90+
/** Silence console.error for the current test; returns the spy to assert on. */
91+
export function quietStderr() {
92+
return vi.spyOn(console, "error").mockImplementation(() => {});
93+
}
94+
95+
/** The allowed directories the server reports, one per line after the header. */
96+
export async function allowedDirectoriesOf(client: Client): Promise<string[]> {
97+
const text = textOf(await call(client, "list_allowed_directories"));
98+
return text.split("\n").slice(1).filter(Boolean);
99+
}

0 commit comments

Comments
 (0)