11import { invariant } from '@epic-web/invariant'
22import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
4- import { test , beforeAll , afterAll , expect } from 'vitest'
4+ import { test , expect } from 'vitest'
55
6- let client : Client
7-
8- beforeAll ( async ( ) => {
9- client = new Client ( {
10- name : 'EpicMeTester' ,
11- version : '1.0.0' ,
12- } )
6+ async function setupClient ( { capabilities = { } } = { } ) {
7+ const client = new Client (
8+ {
9+ name : 'EpicMeTester' ,
10+ version : '1.0.0' ,
11+ } ,
12+ { capabilities } ,
13+ )
1314 const transport = new StdioClientTransport ( {
1415 command : 'tsx' ,
1516 args : [ 'src/index.ts' ] ,
17+ stderr : 'ignore' ,
1618 } )
1719 await client . connect ( transport )
18- } )
19-
20- afterAll ( async ( ) => {
21- await client . transport ?. close ( )
22- } )
20+ return {
21+ client,
22+ async [ Symbol . asyncDispose ] ( ) {
23+ await client . transport ?. close ( )
24+ } ,
25+ }
26+ }
2327
2428test ( 'Tool Definition' , async ( ) => {
29+ await using setup = await setupClient ( )
30+ const { client } = setup
2531 const list = await client . listTools ( )
2632 const [ firstTool ] = list . tools
2733 invariant ( firstTool , '🚨 No tools found' )
@@ -59,6 +65,8 @@ test('Tool Definition', async () => {
5965} )
6066
6167test ( 'Tool Call - Successful Addition' , async ( ) => {
68+ await using setup = await setupClient ( )
69+ const { client } = setup
6270 const result = await client . callTool ( {
6371 name : 'add' ,
6472 arguments : {
@@ -80,6 +88,8 @@ test('Tool Call - Successful Addition', async () => {
8088} )
8189
8290test ( 'Tool Call - Error with Negative Second Number' , async ( ) => {
91+ await using setup = await setupClient ( )
92+ const { client } = setup
8393 const result = await client . callTool ( {
8494 name : 'add' ,
8595 arguments : {
@@ -125,6 +135,8 @@ test('Tool Call - Error with Negative Second Number', async () => {
125135} )
126136
127137test ( 'Tool Call - Another Successful Addition' , async ( ) => {
138+ await using setup = await setupClient ( )
139+ const { client } = setup
128140 const result = await client . callTool ( {
129141 name : 'add' ,
130142 arguments : {
0 commit comments