|
| 1 | +/** |
| 2 | + * Bun integration test |
| 3 | + * |
| 4 | + * Verifies the MCP server and client packages work natively on Bun. |
| 5 | + * Run with: bun test test/server/bun.test.ts |
| 6 | + */ |
| 7 | + |
| 8 | +import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client'; |
| 9 | +import { McpServer, WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/server'; |
| 10 | +// eslint-disable-next-line import/no-unresolved |
| 11 | +import { afterAll, beforeAll, describe, expect, it } from 'bun:test'; |
| 12 | +import * as z from 'zod/v4'; |
| 13 | + |
| 14 | +describe('MCP on Bun', () => { |
| 15 | + let httpServer: ReturnType<typeof Bun.serve>; |
| 16 | + let transport: WebStandardStreamableHTTPServerTransport; |
| 17 | + |
| 18 | + beforeAll(async () => { |
| 19 | + const mcpServer = new McpServer({ name: 'test-server', version: '1.0.0' }); |
| 20 | + |
| 21 | + mcpServer.registerTool( |
| 22 | + 'greet', |
| 23 | + { |
| 24 | + description: 'Greet someone', |
| 25 | + inputSchema: z.object({ name: z.string() }) |
| 26 | + }, |
| 27 | + async ({ name }) => ({ |
| 28 | + content: [{ type: 'text' as const, text: `Hello, ${name}!` }] |
| 29 | + }) |
| 30 | + ); |
| 31 | + |
| 32 | + transport = new WebStandardStreamableHTTPServerTransport(); |
| 33 | + await mcpServer.connect(transport); |
| 34 | + |
| 35 | + httpServer = Bun.serve({ |
| 36 | + port: 0, |
| 37 | + fetch: req => transport.handleRequest(req) |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + afterAll(async () => { |
| 42 | + await transport?.close(); |
| 43 | + httpServer?.stop(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle MCP tool calls', async () => { |
| 47 | + const client = new Client({ name: 'test-client', version: '1.0.0' }); |
| 48 | + const clientTransport = new StreamableHTTPClientTransport(new URL(`http://localhost:${httpServer.port}`)); |
| 49 | + |
| 50 | + await client.connect(clientTransport); |
| 51 | + |
| 52 | + const result = await client.callTool({ name: 'greet', arguments: { name: 'Bun' } }); |
| 53 | + expect(result.content).toEqual([{ type: 'text', text: 'Hello, Bun!' }]); |
| 54 | + |
| 55 | + await client.close(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments