Skip to content

Commit 0ba9c48

Browse files
committed
use single http server insteance for each describe clock and reset state
for each state to fix MaxListenersExceededWarning
1 parent 77bc5b5 commit 0ba9c48

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/mcp/server.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ export class ActorsMcpServer {
8080
});
8181
}
8282

83+
/**
84+
* Resets the server to the default state.
85+
* This method clears all tools and loads the default tools.
86+
* Used primarily for testing purposes.
87+
*/
88+
public async reset(): Promise<void> {
89+
this.tools.clear();
90+
this.updateTools([searchTool, actorDefinitionTool, helpTool]);
91+
if (this.options.enableAddingActors) {
92+
this.loadToolsToAddActors();
93+
}
94+
95+
// Initialize automatically for backward compatibility
96+
this.initialize().catch((error) => {
97+
log.error('Failed to initialize server:', error);
98+
});
99+
}
100+
83101
/**
84102
* Initialize the server with default tools if enabled
85103
*/

tests/integration/actor.server.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Server as HttpServer } from 'node:http';
22

33
import type { Express } from 'express';
4-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
4+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
55

66
import log from '@apify/log';
77

@@ -19,9 +19,8 @@ describe('Actors MCP Server SSE', {
1919
let httpServer: HttpServer;
2020
const testPort = 50000;
2121
const testHost = `http://localhost:${testPort}`;
22-
const serverStartWaitTimeMillis = 100;
2322

24-
beforeEach(async () => {
23+
beforeAll(async () => {
2524
server = new ActorsMcpServer({
2625
enableDefaultActors: false,
2726
});
@@ -32,15 +31,16 @@ describe('Actors MCP Server SSE', {
3231

3332
// Start test server
3433
await new Promise<void>((resolve) => {
35-
httpServer = app.listen(testPort, () => {
36-
// Wait for the server to be fully initialized
37-
// TODO: figure out why this is needed
38-
setTimeout(() => resolve(), serverStartWaitTimeMillis);
39-
});
34+
httpServer = app.listen(testPort, () => resolve());
4035
});
4136
});
4237

43-
afterEach(async () => {
38+
beforeEach(async () => {
39+
// Reset the MCP server state before each test
40+
await server.reset();
41+
});
42+
43+
afterAll(async () => {
4444
await server.close();
4545
await new Promise<void>((resolve) => {
4646
httpServer.close(() => resolve());
@@ -216,7 +216,7 @@ describe('Actors MCP Server Streamable HTTP', {
216216
const testPort = 50001;
217217
const testHost = `http://localhost:${testPort}`;
218218

219-
beforeEach(async () => {
219+
beforeAll(async () => {
220220
server = new ActorsMcpServer({
221221
enableDefaultActors: false,
222222
});
@@ -234,7 +234,12 @@ describe('Actors MCP Server Streamable HTTP', {
234234
await new Promise<void>((resolve) => { setTimeout(resolve, 1000); });
235235
});
236236

237-
afterEach(async () => {
237+
beforeEach(async () => {
238+
// Reset the MCP server state before each test
239+
await server.reset();
240+
});
241+
242+
afterAll(async () => {
238243
await new Promise<void>((resolve) => {
239244
httpServer.close(() => resolve());
240245
});

0 commit comments

Comments
 (0)