|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import * as fs from 'fs/promises'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as os from 'os'; |
| 5 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 6 | +import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'; |
| 7 | + |
| 8 | +describe('directory_tree MCP SDK regression', () => { |
| 9 | + let client: Client; |
| 10 | + let transport: StdioClientTransport; |
| 11 | + let testDir: string; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'mcp-fs-tree-')); |
| 15 | + testDir = await fs.realpath(tmp); |
| 16 | + |
| 17 | + await fs.writeFile(path.join(testDir, 'root.txt'), 'root'); |
| 18 | + await fs.mkdir(path.join(testDir, 'nested')); |
| 19 | + await fs.writeFile(path.join(testDir, 'nested', 'child.txt'), 'child'); |
| 20 | + |
| 21 | + const serverPath = path.resolve(__dirname, '../dist/index.js'); |
| 22 | + transport = new StdioClientTransport({ |
| 23 | + command: 'node', |
| 24 | + args: [serverPath, testDir], |
| 25 | + }); |
| 26 | + |
| 27 | + client = new Client( |
| 28 | + { name: 'directory-tree-regression-test', version: '1.0.0' }, |
| 29 | + { capabilities: {} }, |
| 30 | + ); |
| 31 | + |
| 32 | + await client.connect(transport); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(async () => { |
| 36 | + await client?.close(); |
| 37 | + await fs.rm(testDir, { recursive: true, force: true }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns structuredContent.content as a string (not an array) when called via MCP SDK', async () => { |
| 41 | + const result = await client.callTool({ |
| 42 | + name: 'directory_tree', |
| 43 | + arguments: { path: testDir }, |
| 44 | + }); |
| 45 | + |
| 46 | + // Regression test for issues where structuredContent was returned as an array, |
| 47 | + // which causes MCP SDK validation to throw -32602 (invalid structured content). |
| 48 | + const structured = result.structuredContent as { content: unknown }; |
| 49 | + expect(structured).toBeDefined(); |
| 50 | + expect(typeof structured.content).toBe('string'); |
| 51 | + expect(Array.isArray(structured.content)).toBe(false); |
| 52 | + |
| 53 | + const parsed = JSON.parse(structured.content as string); |
| 54 | + expect(Array.isArray(parsed)).toBe(true); |
| 55 | + expect(parsed.length).toBeGreaterThan(0); |
| 56 | + }); |
| 57 | +}); |
0 commit comments