|
| 1 | +// Read-only mode (#632, #1160): `--read-only` on the command line, or |
| 2 | +// `readOnly` in createServer's options, serves only the tools whose |
| 3 | +// annotations declare `readOnlyHint: true`. The write tools are not merely |
| 4 | +// refused at call time but absent, so a client never lists them. These tests |
| 5 | +// pin the exact read-only tool set on both protocol eras, prove a write tool |
| 6 | +// cannot be called and leaves the disk untouched, and drive the flag through |
| 7 | +// main() so the command-line wiring is covered too. |
| 8 | + |
| 9 | +import fs from "fs/promises"; |
| 10 | +import path from "path"; |
| 11 | +import { PassThrough } from "node:stream"; |
| 12 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 13 | +import { |
| 14 | + Client, |
| 15 | + InMemoryTransport, |
| 16 | + type CallToolResult, |
| 17 | +} from "@modelcontextprotocol/client"; |
| 18 | +import { serveStdio } from "@modelcontextprotocol/server/stdio"; |
| 19 | +import { main, parseArgs } from "../index.js"; |
| 20 | +import { createServer } from "../server.js"; |
| 21 | +import { makeTempDir, quietStderr } from "./helpers.js"; |
| 22 | + |
| 23 | +// Every tool that cannot change the filesystem. A tool added to the server |
| 24 | +// must be added here if it is read-only; a write tool is withheld without any |
| 25 | +// change, which is the point of keying the mode on the annotation. |
| 26 | +const READ_ONLY_TOOLS = [ |
| 27 | + "directory_tree", |
| 28 | + "get_file_info", |
| 29 | + "list_allowed_directories", |
| 30 | + "list_directory", |
| 31 | + "list_directory_with_sizes", |
| 32 | + "read_file", |
| 33 | + "read_media_file", |
| 34 | + "read_multiple_files", |
| 35 | + "read_text_file", |
| 36 | + "search_files", |
| 37 | +]; |
| 38 | + |
| 39 | +let dir: string; |
| 40 | +let stderr: ReturnType<typeof quietStderr>; |
| 41 | +let closers: Array<() => Promise<void>>; |
| 42 | + |
| 43 | +beforeEach(async () => { |
| 44 | + stderr = quietStderr(); |
| 45 | + dir = await makeTempDir("mcp-fs-read-only-"); |
| 46 | + await fs.writeFile(path.join(dir, "note.txt"), "original"); |
| 47 | + closers = []; |
| 48 | +}); |
| 49 | + |
| 50 | +afterEach(async () => { |
| 51 | + for (const close of closers.reverse()) await close(); |
| 52 | + vi.restoreAllMocks(); |
| 53 | + await fs.rm(dir, { recursive: true, force: true }); |
| 54 | +}); |
| 55 | + |
| 56 | +/** A client connected to a server for `dir`, negotiating per `mode`. */ |
| 57 | +async function connect( |
| 58 | + readOnly: boolean, |
| 59 | + mode: "auto" | "legacy", |
| 60 | +): Promise<Client> { |
| 61 | + const [clientTransport, serverTransport] = |
| 62 | + InMemoryTransport.createLinkedPair(); |
| 63 | + const handle = serveStdio( |
| 64 | + ({ era }) => createServer([dir], { era, readOnly }), |
| 65 | + { transport: serverTransport }, |
| 66 | + ); |
| 67 | + const client = new Client( |
| 68 | + { name: "fs-read-only-test", version: "0.0.0" }, |
| 69 | + { versionNegotiation: { mode } }, |
| 70 | + ); |
| 71 | + await client.connect(clientTransport); |
| 72 | + closers.push( |
| 73 | + () => handle.close(), |
| 74 | + () => client.close(), |
| 75 | + ); |
| 76 | + return client; |
| 77 | +} |
| 78 | + |
| 79 | +describe.each([ |
| 80 | + ["2026-07-28", "auto"], |
| 81 | + ["2025-11-25", "legacy"], |
| 82 | +] as const)("read-only mode on %s", (_label, mode) => { |
| 83 | + it("lists only the read-only tools, each annotated readOnlyHint: true", async () => { |
| 84 | + const client = await connect(true, mode); |
| 85 | + const { tools } = await client.listTools(); |
| 86 | + expect(tools.map((tool) => tool.name).sort()).toEqual(READ_ONLY_TOOLS); |
| 87 | + for (const tool of tools) { |
| 88 | + expect(tool.annotations?.readOnlyHint).toBe(true); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + it("withholds exactly the tools not annotated read-only", async () => { |
| 93 | + const full = await connect(false, mode); |
| 94 | + const { tools } = await full.listTools(); |
| 95 | + const writeTools = tools |
| 96 | + .filter((tool) => tool.annotations?.readOnlyHint !== true) |
| 97 | + .map((tool) => tool.name); |
| 98 | + expect(writeTools).toEqual( |
| 99 | + expect.arrayContaining([ |
| 100 | + "write_file", |
| 101 | + "edit_file", |
| 102 | + "create_directory", |
| 103 | + "move_file", |
| 104 | + ]), |
| 105 | + ); |
| 106 | + const readOnly = await connect(true, mode); |
| 107 | + const listed = (await readOnly.listTools()).tools.map((t) => t.name); |
| 108 | + for (const name of writeTools) expect(listed).not.toContain(name); |
| 109 | + }); |
| 110 | + |
| 111 | + it("refuses a call to a write tool and leaves the file untouched", async () => { |
| 112 | + const client = await connect(true, mode); |
| 113 | + const target = path.join(dir, "note.txt"); |
| 114 | + const result = (await client |
| 115 | + .callTool({ |
| 116 | + name: "write_file", |
| 117 | + arguments: { path: target, content: "changed" }, |
| 118 | + }) |
| 119 | + .catch((error: unknown) => ({ |
| 120 | + isError: true, |
| 121 | + content: [{ type: "text", text: String(error) }], |
| 122 | + }))) as CallToolResult; |
| 123 | + expect(result.isError).toBe(true); |
| 124 | + expect(JSON.stringify(result.content)).toMatch(/write_file/); |
| 125 | + expect(await fs.readFile(target, "utf8")).toBe("original"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("still serves the read tools", async () => { |
| 129 | + const client = await connect(true, mode); |
| 130 | + const result = (await client.callTool({ |
| 131 | + name: "read_text_file", |
| 132 | + arguments: { path: path.join(dir, "note.txt") }, |
| 133 | + })) as CallToolResult; |
| 134 | + expect(result.isError).toBeFalsy(); |
| 135 | + expect(result.content).toEqual([{ type: "text", text: "original" }]); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe("parseArgs", () => { |
| 140 | + it("takes every argument as a directory when no option is given", () => { |
| 141 | + expect(parseArgs(["/a", "/b"])).toEqual({ |
| 142 | + directories: ["/a", "/b"], |
| 143 | + readOnly: false, |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it("recognizes --read-only before, between or after the directories", () => { |
| 148 | + for (const args of [ |
| 149 | + ["--read-only", "/a", "/b"], |
| 150 | + ["/a", "--read-only", "/b"], |
| 151 | + ["/a", "/b", "--read-only"], |
| 152 | + ]) { |
| 153 | + expect(parseArgs(args)).toEqual({ |
| 154 | + directories: ["/a", "/b"], |
| 155 | + readOnly: true, |
| 156 | + }); |
| 157 | + } |
| 158 | + }); |
| 159 | + |
| 160 | + it("treats everything after -- as a directory", () => { |
| 161 | + expect(parseArgs(["--read-only", "--", "--read-only", "/a"])).toEqual({ |
| 162 | + directories: ["--read-only", "/a"], |
| 163 | + readOnly: true, |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
| 167 | + |
| 168 | +describe("main() with --read-only", () => { |
| 169 | + it("serves only the read-only tools over stdio and says so on stderr", async () => { |
| 170 | + const stdin = new PassThrough(); |
| 171 | + const stdout = new PassThrough(); |
| 172 | + stdout.setEncoding("utf8"); |
| 173 | + const handle = await main(["--read-only", dir], stdin, stdout); |
| 174 | + closers.push(() => handle.close()); |
| 175 | + const line = new Promise<string>((resolve) => { |
| 176 | + let buffered = ""; |
| 177 | + stdout.on("data", (chunk: string) => { |
| 178 | + buffered += chunk; |
| 179 | + const newline = buffered.indexOf("\n"); |
| 180 | + if (newline !== -1) resolve(buffered.slice(0, newline)); |
| 181 | + }); |
| 182 | + }); |
| 183 | + stdin.write( |
| 184 | + `${JSON.stringify({ |
| 185 | + jsonrpc: "2.0", |
| 186 | + id: 1, |
| 187 | + method: "tools/list", |
| 188 | + params: { |
| 189 | + _meta: { |
| 190 | + "io.modelcontextprotocol/protocolVersion": "2026-07-28", |
| 191 | + "io.modelcontextprotocol/clientCapabilities": {}, |
| 192 | + }, |
| 193 | + }, |
| 194 | + })}\n`, |
| 195 | + ); |
| 196 | + const { result } = JSON.parse(await line) as { |
| 197 | + result: { tools: Array<{ name: string }> }; |
| 198 | + }; |
| 199 | + expect(result.tools.map((tool) => tool.name).sort()).toEqual( |
| 200 | + READ_ONLY_TOOLS, |
| 201 | + ); |
| 202 | + expect(stderr).toHaveBeenCalledWith( |
| 203 | + "Read-only mode: only tools that cannot change the filesystem are served", |
| 204 | + ); |
| 205 | + }); |
| 206 | + |
| 207 | + it("prints usage when --read-only is the only argument", async () => { |
| 208 | + const handle = await main( |
| 209 | + ["--read-only"], |
| 210 | + new PassThrough(), |
| 211 | + new PassThrough(), |
| 212 | + ); |
| 213 | + closers.push(() => handle.close()); |
| 214 | + expect(stderr).toHaveBeenCalledWith( |
| 215 | + "Usage: mcp-server-filesystem [--read-only] [allowed-directory] [additional-directories...]", |
| 216 | + ); |
| 217 | + }); |
| 218 | +}); |
0 commit comments