|
| 1 | +// Access control on both protocol eras (#4852). main() serves createServer() |
| 2 | +// through the SDK's serveStdio entry, so the first message a client sends |
| 3 | +// picks the connection's era. On 2025-11-25 a client that supports Roots |
| 4 | +// replaces the command-line directories after initialize, as before. On |
| 5 | +// 2026-07-28 there is no initialize and no server-to-client roots/list, and |
| 6 | +// Roots is deprecated, so the command-line directories are the whole |
| 7 | +// allow-list: a client's roots are never asked for and never widen it, and a |
| 8 | +// server started with none says so on every call. |
| 9 | +// |
| 10 | +// The SDK-client tests serve createServer() exactly as main() does, over an |
| 11 | +// in-memory transport; the raw tests drive main() itself over in-memory |
| 12 | +// stdio, so its era wiring is covered too. |
| 13 | + |
| 14 | +import fs from "fs/promises"; |
| 15 | +import path from "path"; |
| 16 | +import { PassThrough } from "node:stream"; |
| 17 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 18 | +import { |
| 19 | + Client, |
| 20 | + InMemoryTransport, |
| 21 | + type CallToolResult, |
| 22 | +} from "@modelcontextprotocol/client"; |
| 23 | +import { serveStdio } from "@modelcontextprotocol/server/stdio"; |
| 24 | +import { main } from "../index.js"; |
| 25 | +import { |
| 26 | + createServer, |
| 27 | + NO_ALLOWED_DIRECTORIES_MODERN_ERROR, |
| 28 | +} from "../server.js"; |
| 29 | +import { makeTempDir, quietStderr } from "./helpers.js"; |
| 30 | + |
| 31 | +let allowed: string; |
| 32 | +let other: string; |
| 33 | +let closers: Array<() => Promise<void>>; |
| 34 | + |
| 35 | +beforeEach(async () => { |
| 36 | + quietStderr(); |
| 37 | + allowed = await fs.realpath(await makeTempDir("mcp-fs-era-allowed-")); |
| 38 | + other = await fs.realpath(await makeTempDir("mcp-fs-era-other-")); |
| 39 | + await fs.writeFile(path.join(other, "secret.txt"), "secret"); |
| 40 | + closers = []; |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(async () => { |
| 44 | + for (const close of closers.reverse()) await close(); |
| 45 | + vi.restoreAllMocks(); |
| 46 | + await fs.rm(allowed, { recursive: true, force: true }); |
| 47 | + await fs.rm(other, { recursive: true, force: true }); |
| 48 | +}); |
| 49 | + |
| 50 | +/** |
| 51 | + * A client that supports Roots and offers `other` as its only root, connected |
| 52 | + * to a server for `directories` and negotiating per `mode`. |
| 53 | + */ |
| 54 | +async function connect( |
| 55 | + directories: string[], |
| 56 | + mode: "auto" | "legacy", |
| 57 | +): Promise<{ client: Client; rootsAsked: () => number }> { |
| 58 | + const [clientTransport, serverTransport] = |
| 59 | + InMemoryTransport.createLinkedPair(); |
| 60 | + const handle = serveStdio(({ era }) => createServer(directories, { era }), { |
| 61 | + transport: serverTransport, |
| 62 | + }); |
| 63 | + const client = new Client( |
| 64 | + { name: "fs-era-test", version: "0.0.0" }, |
| 65 | + { capabilities: { roots: {} }, versionNegotiation: { mode } }, |
| 66 | + ); |
| 67 | + let asked = 0; |
| 68 | + client.setRequestHandler("roots/list", async () => { |
| 69 | + asked++; |
| 70 | + return { roots: [{ uri: `file://${other}`, name: "other" }] }; |
| 71 | + }); |
| 72 | + await client.connect(clientTransport); |
| 73 | + closers.push( |
| 74 | + () => handle.close(), |
| 75 | + () => client.close(), |
| 76 | + ); |
| 77 | + return { client, rootsAsked: () => asked }; |
| 78 | +} |
| 79 | + |
| 80 | +async function listAllowed(client: Client): Promise<string> { |
| 81 | + const result = (await client.callTool({ |
| 82 | + name: "list_allowed_directories", |
| 83 | + arguments: {}, |
| 84 | + })) as CallToolResult; |
| 85 | + return (result.content[0] as { text: string }).text; |
| 86 | +} |
| 87 | + |
| 88 | +describe("on 2026-07-28", () => { |
| 89 | + it("serves only the command-line directories, and never asks for roots", async () => { |
| 90 | + const { client, rootsAsked } = await connect([allowed], "auto"); |
| 91 | + expect(client.getProtocolEra()).toBe("modern"); |
| 92 | + expect(await listAllowed(client)).toBe(`Allowed directories:\n${allowed}`); |
| 93 | + const read = (await client.callTool({ |
| 94 | + name: "read_text_file", |
| 95 | + arguments: { path: path.join(other, "secret.txt") }, |
| 96 | + })) as CallToolResult; |
| 97 | + expect(read.isError).toBe(true); |
| 98 | + expect((read.content[0] as { text: string }).text).toContain( |
| 99 | + "Access denied", |
| 100 | + ); |
| 101 | + expect(rootsAsked()).toBe(0); |
| 102 | + }); |
| 103 | + |
| 104 | + it("explains on every call that a server with no directories cannot operate", async () => { |
| 105 | + const { client } = await connect([], "auto"); |
| 106 | + const result = (await client.callTool({ |
| 107 | + name: "list_allowed_directories", |
| 108 | + arguments: {}, |
| 109 | + })) as CallToolResult; |
| 110 | + expect(result).toMatchObject({ |
| 111 | + isError: true, |
| 112 | + content: [{ type: "text", text: NO_ALLOWED_DIRECTORIES_MODERN_ERROR }], |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe("on 2025-11-25", () => { |
| 118 | + it("still lets the client's roots replace the command-line directories", async () => { |
| 119 | + const { client, rootsAsked } = await connect([allowed], "legacy"); |
| 120 | + expect(client.getProtocolEra()).toBe("legacy"); |
| 121 | + await vi.waitFor(() => expect(rootsAsked()).toBe(1)); |
| 122 | + expect(await listAllowed(client)).toBe(`Allowed directories:\n${other}`); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe("main() over stdio", () => { |
| 127 | + const envelope = { |
| 128 | + "io.modelcontextprotocol/protocolVersion": "2026-07-28", |
| 129 | + "io.modelcontextprotocol/clientCapabilities": { roots: {} }, |
| 130 | + }; |
| 131 | + |
| 132 | + /** Start main() and send one raw 2026-07-28 tools/call; resolve its result. */ |
| 133 | + async function callOverStdio(args: string[]) { |
| 134 | + const stdin = new PassThrough(); |
| 135 | + const stdout = new PassThrough(); |
| 136 | + stdout.setEncoding("utf8"); |
| 137 | + const handle = await main(args, stdin, stdout); |
| 138 | + closers.push(() => handle.close()); |
| 139 | + const line = new Promise<string>((resolve) => { |
| 140 | + let buffered = ""; |
| 141 | + stdout.on("data", (chunk: string) => { |
| 142 | + buffered += chunk; |
| 143 | + const newline = buffered.indexOf("\n"); |
| 144 | + if (newline !== -1) resolve(buffered.slice(0, newline)); |
| 145 | + }); |
| 146 | + }); |
| 147 | + stdin.write( |
| 148 | + `${JSON.stringify({ |
| 149 | + jsonrpc: "2.0", |
| 150 | + id: 1, |
| 151 | + method: "tools/call", |
| 152 | + params: { |
| 153 | + _meta: envelope, |
| 154 | + name: "list_allowed_directories", |
| 155 | + arguments: {}, |
| 156 | + }, |
| 157 | + })}\n`, |
| 158 | + ); |
| 159 | + return JSON.parse(await line) as { |
| 160 | + result: CallToolResult & { resultType: string }; |
| 161 | + }; |
| 162 | + } |
| 163 | + |
| 164 | + it("answers a 2026-07-28 request with no initialize", async () => { |
| 165 | + const { result } = await callOverStdio([allowed]); |
| 166 | + expect(result.resultType).toBe("complete"); |
| 167 | + expect(result.content).toEqual([ |
| 168 | + { type: "text", text: `Allowed directories:\n${allowed}` }, |
| 169 | + ]); |
| 170 | + }); |
| 171 | + |
| 172 | + it("reports a transport error on stderr", async () => { |
| 173 | + const stderr = quietStderr(); |
| 174 | + const stdin = new PassThrough(); |
| 175 | + const handle = await main([allowed], stdin, new PassThrough()); |
| 176 | + closers.push(() => handle.close()); |
| 177 | + const failure = new Error("stdin broke"); |
| 178 | + stdin.emit("error", failure); |
| 179 | + expect(stderr).toHaveBeenCalledWith("Transport error:", failure); |
| 180 | + }); |
| 181 | + |
| 182 | + it("fails each 2026-07-28 call visibly when started with no directories", async () => { |
| 183 | + const { result } = await callOverStdio([]); |
| 184 | + expect(result.isError).toBe(true); |
| 185 | + expect(result.content).toEqual([ |
| 186 | + { type: "text", text: NO_ALLOWED_DIRECTORIES_MODERN_ERROR }, |
| 187 | + ]); |
| 188 | + }); |
| 189 | +}); |
0 commit comments