@@ -11,7 +11,9 @@ declare module 'vitest' {
1111type OutputBuffer = Array < { channel : 'stdout' | 'stderr' ; data : Buffer } >
1212
1313export default async function setup ( project : TestProject ) {
14- const mcpServerPort = await getPort ( )
14+ const mcpServerPort = process . env . PORT
15+ ? Number ( process . env . PORT )
16+ : await getPort ( )
1517
1618 project . provide ( 'mcpServerPort' , mcpServerPort )
1719
@@ -122,13 +124,15 @@ export default async function setup(project: TestProject) {
122124 } )
123125 }
124126
125- async function startServers ( ) {
126- console . log ( 'Starting servers...' )
127-
128- // Start app server if necessary
129- await startAppServerIfNecessary ( )
127+ async function startMcpServerIfNecessary ( ) {
128+ const isMcpRunning = await fetch (
129+ `http://localhost:${ mcpServerPort } /healthcheck` ,
130+ ) . catch ( ( ) => ( { ok : false } ) )
131+ if ( isMcpRunning . ok ) {
132+ return
133+ }
130134
131- // Start the MCP server from the exercise directory
135+ // Start the MCP server if necessary
132136 console . log ( `Starting MCP server on port ${ mcpServerPort } ...` )
133137 mcpServerProcess = execa (
134138 'npx' ,
@@ -142,6 +146,16 @@ export default async function setup(project: TestProject) {
142146 } ,
143147 } ,
144148 )
149+ }
150+
151+ async function startServers ( ) {
152+ console . log ( 'Starting servers...' )
153+
154+ // Start app server if necessary
155+ await startAppServerIfNecessary ( )
156+
157+ // Start MCP server if necessary
158+ await startMcpServerIfNecessary ( )
145159
146160 try {
147161 // Wait for both servers to be ready simultaneously
@@ -161,7 +175,9 @@ export default async function setup(project: TestProject) {
161175 textMatch : mcpServerPort . toString ( ) ,
162176 name : '[MCP-SERVER]' ,
163177 outputBuffer : mcpServerOutput ,
164- } ) ,
178+ } ) . then ( ( ) =>
179+ waitForResourceReady ( `http://localhost:${ mcpServerPort } /healthcheck` ) ,
180+ ) ,
165181 ] )
166182
167183 console . log ( 'Servers started successfully' )
0 commit comments