|
| 1 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 2 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 3 | +import { promises as fs } from 'node:fs'; |
| 4 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 5 | +import { tmpdir } from 'node:os'; |
| 6 | +import { join } from 'node:path'; |
| 7 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 8 | +import { buildMcpServer, MCP_SERVE_EXCLUDE, mcpServableTools } from './serve.js'; |
| 9 | + |
| 10 | +describe('mcpServableTools', () => { |
| 11 | + it('excludes interactive / host-coupled tools', () => { |
| 12 | + const names = mcpServableTools().map((t) => t.name); |
| 13 | + for (const excluded of MCP_SERVE_EXCLUDE) { |
| 14 | + expect(names).not.toContain(excluded); |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + it('includes the core file/shell tools', () => { |
| 19 | + const names = mcpServableTools().map((t) => t.name); |
| 20 | + expect(names).toEqual( |
| 21 | + expect.arrayContaining(['Read', 'Write', 'Edit', 'Bash', 'Grep', 'Glob']), |
| 22 | + ); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe('buildMcpServer over an in-memory transport', () => { |
| 27 | + let dir: string; |
| 28 | + let client: Client; |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + dir = await mkdtemp(join(tmpdir(), 'dc-mcp-serve-')); |
| 32 | + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); |
| 33 | + const server = buildMcpServer({ cwd: dir, name: 'deepcode-test', version: '9.9.9' }); |
| 34 | + client = new Client({ name: 'test-client', version: '0.0.0' }, { capabilities: {} }); |
| 35 | + await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]); |
| 36 | + }); |
| 37 | + afterEach(async () => { |
| 38 | + await client.close(); |
| 39 | + await rm(dir, { recursive: true, force: true }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('lists tools (and hides excluded ones)', async () => { |
| 43 | + const { tools } = await client.listTools(); |
| 44 | + const names = tools.map((t) => t.name); |
| 45 | + expect(names).toContain('Read'); |
| 46 | + expect(names).toContain('Write'); |
| 47 | + expect(names).not.toContain('AskUserQuestion'); |
| 48 | + expect(names).not.toContain('Task'); |
| 49 | + // every listed tool carries a description + object input schema |
| 50 | + for (const t of tools) { |
| 51 | + expect(typeof t.description).toBe('string'); |
| 52 | + expect(t.inputSchema).toMatchObject({ type: 'object' }); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + it('executes a tool round-trip (Write then Read)', async () => { |
| 57 | + const file = join(dir, 'note.txt'); |
| 58 | + const writeRes = await client.callTool({ |
| 59 | + name: 'Write', |
| 60 | + arguments: { file_path: file, content: 'hello from mcp' }, |
| 61 | + }); |
| 62 | + expect(writeRes.isError ?? false).toBe(false); |
| 63 | + expect(await fs.readFile(file, 'utf8')).toBe('hello from mcp'); |
| 64 | + |
| 65 | + const readRes = (await client.callTool({ |
| 66 | + name: 'Read', |
| 67 | + arguments: { file_path: file }, |
| 68 | + })) as { content: Array<{ type: string; text: string }>; isError?: boolean }; |
| 69 | + expect(readRes.isError ?? false).toBe(false); |
| 70 | + expect(readRes.content[0]!.text).toContain('hello from mcp'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns isError for an unknown tool', async () => { |
| 74 | + const res = (await client.callTool({ name: 'NoSuchTool', arguments: {} })) as { |
| 75 | + content: Array<{ text: string }>; |
| 76 | + isError?: boolean; |
| 77 | + }; |
| 78 | + expect(res.isError).toBe(true); |
| 79 | + expect(res.content[0]!.text).toMatch(/Unknown tool/); |
| 80 | + }); |
| 81 | + |
| 82 | + it('surfaces a tool-level error as isError (Read of a missing file)', async () => { |
| 83 | + const res = (await client.callTool({ |
| 84 | + name: 'Read', |
| 85 | + arguments: { file_path: join(dir, 'does-not-exist.txt') }, |
| 86 | + })) as { content: Array<{ text: string }>; isError?: boolean }; |
| 87 | + expect(res.isError).toBe(true); |
| 88 | + }); |
| 89 | +}); |
0 commit comments