|
| 1 | +// Every property of every tool input schema the server advertises in |
| 2 | +// tools/list carries a description (#3669). A model reads these to decide what |
| 3 | +// to pass, and with no description it guesses from the property name alone. |
| 4 | +// This is the reference implementation other server authors copy, so the rule |
| 5 | +// is pinned for every tool, existing or added later, and for nested |
| 6 | +// properties (array items, object fields) as well as top-level ones. The |
| 7 | +// excludePatterns descriptions state matching rules that differ between |
| 8 | +// directory_tree and search_files, so those rules are pinned here as well. |
| 9 | + |
| 10 | +import fs from "fs/promises"; |
| 11 | +import path from "path"; |
| 12 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 13 | +import { |
| 14 | + call, |
| 15 | + connect, |
| 16 | + makeTempDir, |
| 17 | + quietStderr, |
| 18 | + textOf, |
| 19 | + type Connected, |
| 20 | +} from "./helpers.js"; |
| 21 | + |
| 22 | +/** A JSON Schema node, as much of it as the walk reads. */ |
| 23 | +type SchemaNode = { |
| 24 | + description?: unknown; |
| 25 | + properties?: Record<string, SchemaNode>; |
| 26 | + items?: SchemaNode | SchemaNode[]; |
| 27 | + anyOf?: SchemaNode[]; |
| 28 | + oneOf?: SchemaNode[]; |
| 29 | + allOf?: SchemaNode[]; |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * The path of every property under `node`, at any depth, that has no |
| 34 | + * non-empty description: the properties of nested objects and of array |
| 35 | + * items count, since a model fills those in too. |
| 36 | + */ |
| 37 | +function undescribed(node: SchemaNode, path: string): string[] { |
| 38 | + const missing: string[] = []; |
| 39 | + for (const [key, child] of Object.entries(node.properties ?? {})) { |
| 40 | + const at = `${path}.${key}`; |
| 41 | + if ( |
| 42 | + typeof child.description !== "string" || |
| 43 | + child.description.trim() === "" |
| 44 | + ) { |
| 45 | + missing.push(at); |
| 46 | + } |
| 47 | + missing.push(...undescribed(child, at)); |
| 48 | + } |
| 49 | + const items = node.items; |
| 50 | + for (const item of Array.isArray(items) ? items : items ? [items] : []) { |
| 51 | + missing.push(...undescribed(item, `${path}[]`)); |
| 52 | + } |
| 53 | + for (const branch of [ |
| 54 | + ...(node.anyOf ?? []), |
| 55 | + ...(node.oneOf ?? []), |
| 56 | + ...(node.allOf ?? []), |
| 57 | + ]) { |
| 58 | + missing.push(...undescribed(branch, path)); |
| 59 | + } |
| 60 | + return missing; |
| 61 | +} |
| 62 | + |
| 63 | +let dir: string; |
| 64 | +let conn: Connected; |
| 65 | + |
| 66 | +beforeEach(async () => { |
| 67 | + // The server logs its roots decision to stderr on every connect. |
| 68 | + quietStderr(); |
| 69 | + dir = await makeTempDir("mcp-fs-descriptions-"); |
| 70 | + conn = await connect([dir]); |
| 71 | +}); |
| 72 | + |
| 73 | +afterEach(async () => { |
| 74 | + vi.restoreAllMocks(); |
| 75 | + await conn.close(); |
| 76 | + await fs.rm(dir, { recursive: true, force: true }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe("tool input descriptions (#3669)", () => { |
| 80 | + it("describes every input property of every tool", async () => { |
| 81 | + const { tools } = await conn.client.listTools(); |
| 82 | + expect(tools.length).toBeGreaterThan(0); |
| 83 | + const missing = tools.flatMap((tool) => |
| 84 | + undescribed(tool.inputSchema as SchemaNode, tool.name), |
| 85 | + ); |
| 86 | + expect(missing).toEqual([]); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe("excludePatterns, as the descriptions state it (#3669)", () => { |
| 91 | + beforeEach(async () => { |
| 92 | + // A node_modules at the top level and one nested a level down. |
| 93 | + await fs.mkdir(path.join(dir, "node_modules")); |
| 94 | + await fs.writeFile(path.join(dir, "node_modules", "top.txt"), ""); |
| 95 | + await fs.mkdir(path.join(dir, "pkg", "node_modules"), { recursive: true }); |
| 96 | + await fs.writeFile(path.join(dir, "pkg", "node_modules", "deep.txt"), ""); |
| 97 | + await fs.writeFile(path.join(dir, "pkg", "keep.txt"), ""); |
| 98 | + }); |
| 99 | + |
| 100 | + async function search(excludePatterns: string[]): Promise<string[]> { |
| 101 | + const result = await call(conn.client, "search_files", { |
| 102 | + path: dir, |
| 103 | + pattern: "**/*.txt", |
| 104 | + excludePatterns, |
| 105 | + }); |
| 106 | + return textOf(result).split("\n").sort(); |
| 107 | + } |
| 108 | + |
| 109 | + it("directory_tree: a name with no '*' is left out at any depth", async () => { |
| 110 | + const result = await call(conn.client, "directory_tree", { |
| 111 | + path: dir, |
| 112 | + excludePatterns: ["node_modules"], |
| 113 | + }); |
| 114 | + const text = textOf(result); |
| 115 | + expect(text).not.toContain("node_modules"); |
| 116 | + expect(text).toContain("keep.txt"); |
| 117 | + }); |
| 118 | + |
| 119 | + it("search_files: a bare name skips only the top-level entry", async () => { |
| 120 | + expect(await search(["node_modules"])).toEqual([ |
| 121 | + path.join(dir, "pkg", "keep.txt"), |
| 122 | + path.join(dir, "pkg", "node_modules", "deep.txt"), |
| 123 | + ]); |
| 124 | + }); |
| 125 | + |
| 126 | + it("search_files: '**/node_modules' skips that name at any depth", async () => { |
| 127 | + expect(await search(["**/node_modules"])).toEqual([ |
| 128 | + path.join(dir, "pkg", "keep.txt"), |
| 129 | + ]); |
| 130 | + }); |
| 131 | +}); |
0 commit comments