Skip to content

Commit e9b7ced

Browse files
committed
DEV stash
1 parent 026feb4 commit e9b7ced

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/server.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type McpTool = [string, { description: string; inputSchema: any }, (args: any) =
88

99
type McpToolCreator = () => McpTool;
1010

11+
type StopServerOptions = { exitProcess?: boolean };
12+
1113
/**
1214
* Server instance with shutdown capability
1315
*/
@@ -16,7 +18,7 @@ interface ServerInstance {
1618
/**
1719
* Stop the server gracefully
1820
*/
19-
stop(): Promise<void>;
21+
stop(options?: StopServerOptions): Promise<void>;
2022

2123
/**
2224
* Check if server is running
@@ -43,12 +45,16 @@ const runServer = async (options = OPTIONS, {
4345
let transport: StdioServerTransport | null = null;
4446
let running = false;
4547

46-
const stopServer = async () => {
48+
const stopServer = async ({ exitProcess = true }: StopServerOptions = {}) => {
4749
if (server && running) {
4850
await server?.close();
4951
running = false;
52+
transport = null;
5053
console.log('PatternFly MCP server stopped');
51-
process.exit(0);
54+
55+
if (exitProcess === true) {
56+
process.exit(0);
57+
}
5258

5359
/*
5460
try {
@@ -112,8 +118,8 @@ const runServer = async (options = OPTIONS, {
112118
}
113119

114120
return {
115-
async stop(): Promise<void> {
116-
return await stopServer();
121+
async stop(options?: StopServerOptions): Promise<void> {
122+
return await stopServer(options);
117123
},
118124

119125
isRunning(): boolean {

tests/programmatic.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@
88
import { start, type CliOptions, type ServerInstance } from '../src/index';
99
import { OPTIONS } from '../src/options';
1010

11+
describe('Programmatic API Usage', () => {
12+
it('should handle multiple start() calls with different options and unique sessionIds', async () => {
13+
// First start() call
14+
const firstOptions: Partial<CliOptions> = { docsHost: true };
15+
const server1 = await start(firstOptions);
16+
17+
expect(OPTIONS.docsHost).toBe(true);
18+
expect(OPTIONS.sessionId).toBeDefined();
19+
const firstSessionId = OPTIONS.sessionId;
20+
21+
expect(server1.isRunning()).toBe(true);
22+
23+
// Second start() call with different options
24+
const secondOptions: Partial<CliOptions> = { docsHost: false };
25+
const server2 = await start(secondOptions);
26+
27+
expect(OPTIONS.docsHost).toBe(false);
28+
expect(OPTIONS.sessionId).toBeDefined();
29+
expect(OPTIONS.sessionId).not.toBe(firstSessionId);
30+
expect(server2.isRunning()).toBe(true);
31+
32+
// Third start() call with no options
33+
const thirdOptions: Partial<CliOptions> = {};
34+
const server3 = await start(thirdOptions);
35+
36+
expect(OPTIONS.docsHost).toBe(false);
37+
expect(OPTIONS.sessionId).toBeDefined();
38+
expect(OPTIONS.sessionId).not.toBe(firstSessionId);
39+
expect(server3.isRunning()).toBe(true);
40+
41+
await server1.stop({ exitProcess: false });
42+
await server2.stop({ exitProcess: false });
43+
await server3.stop({ exitProcess: false });
44+
});
45+
});
46+
47+
/*
1148
describe('Programmatic API Usage', () => {
1249
let originalArgv: string[];
1350
let serverInstances: ServerInstance[] = [];
@@ -324,3 +361,4 @@ describe('Programmatic API Usage', () => {
324361
});
325362
});
326363
});
364+
*/

0 commit comments

Comments
 (0)