Skip to content

Commit 896ea18

Browse files
committed
refactor(server): use builtInTools const, logging, annotations
1 parent 83c6f2d commit 896ea18

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

src/server.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './options.context';
1717
import { DEFAULT_OPTIONS } from './options.defaults';
1818

19-
type McpTool = [string, { description: string; inputSchema: any }, (args: any) => Promise<any>];
19+
type McpTool = [string, { description: string; inputSchema: any }, (args: any) => Promise<any> | any];
2020

2121
type McpToolCreator = (options?: GlobalOptions) => McpTool;
2222

@@ -54,6 +54,9 @@ type ServerOnLogHandler = (entry: ServerLogEvent) => void;
5454

5555
/**
5656
* Subscribes a handler function to server logs. Automatically unsubscribed on server shutdown.
57+
*
58+
* @param {ServerOnLogHandler} handler - The function responsible for handling server log events.
59+
* @returns A cleanup function that unregisters the logging handler when called.
5760
*/
5861
type ServerOnLog = (handler: ServerOnLogHandler) => () => void;
5962

@@ -71,21 +74,31 @@ interface ServerInstance {
7174
}
7275

7376
/**
74-
* Create and run a server with shutdown, register tool and errors.
77+
* Built-in tools.
78+
*
79+
* Array of built-in tools
80+
*/
81+
const builtinTools: McpToolCreator[] = [
82+
usePatternFlyDocsTool,
83+
fetchDocsTool,
84+
componentSchemasTool
85+
];
86+
87+
/**
88+
* Create and run the MCP server, register tools, and return a handle.
89+
*
90+
* - Built-in and inline tools are realized in-process
91+
* - External plugins are realized in the Tools Host (child).
7592
*
7693
* @param [options] Server options
7794
* @param [settings] Server settings (tools, signal handling, etc.)
78-
* @param [settings.tools]
79-
* @param [settings.enableSigint]
80-
* @param [settings.allowProcessExit]
81-
* @returns Server instance
95+
* @param [settings.tools] - Built-in tools to register.
96+
* @param [settings.enableSigint] - Indicates whether SIGINT signal handling is enabled.
97+
* @param [settings.allowProcessExit] - Determines if the process is allowed to exit explicitly, useful for testing.
98+
* @returns Server instance with `stop()`, `isRunning()`, and `onLog()` subscription.
8299
*/
83100
const runServer = async (options: ServerOptions = getOptions(), {
84-
tools = [
85-
usePatternFlyDocsTool,
86-
fetchDocsTool,
87-
componentSchemasTool
88-
],
101+
tools = builtinTools,
89102
enableSigint = true,
90103
allowProcessExit = true
91104
}: ServerSettings = {}): Promise<ServerInstance> => {
@@ -99,18 +112,18 @@ const runServer = async (options: ServerOptions = getOptions(), {
99112
let onLogSetup: ServerOnLog = () => () => {};
100113

101114
const stopServer = async () => {
102-
log.info(`\n${options.name} server shutting down... `);
115+
log.debug(`${options.name} attempting shutdown.`);
103116

104117
if (server && running) {
105118
log.info(`${options.name} shutting down...`);
106119

107120
if (httpHandle) {
108-
log.info('...closing HTTP transport');
121+
log.debug('...closing HTTP transport');
109122
await httpHandle.close();
110123
httpHandle = null;
111124
}
112125

113-
log.info('...closing Server');
126+
log.debug('...closing Server');
114127
await server?.close();
115128
running = false;
116129

@@ -139,8 +152,11 @@ const runServer = async (options: ServerOptions = getOptions(), {
139152
}
140153
);
141154

155+
// Setup server logging.
142156
const subUnsub = createServerLogger.memo(server);
143157

158+
log.debug(`Server logging enabled: isStderr = ${options?.logging?.stderr} isProtocol = ${enableProtocolLogging};`);
159+
144160
if (subUnsub) {
145161
const { subscribe, unsubscribe } = subUnsub;
146162

@@ -194,6 +210,9 @@ const runServer = async (options: ServerOptions = getOptions(), {
194210
},
195211

196212
onLog(handler: ServerOnLogHandler): () => void {
213+
// Simple one-off log event to notify the handler of the server startup.
214+
handler({ level: 'info', msg: `${options.name} running!`, transport: options.logging?.transport } as LogEvent);
215+
197216
return onLogSetup(handler);
198217
}
199218
};
@@ -223,10 +242,12 @@ runServer.memo = memo(
223242
try {
224243
await server.stop();
225244
} catch (error) {
245+
// Avoid engaging the contextual log channel on rollout.
226246
console.error(`Error stopping server: ${error}`);
227247
}
228248
}
229249
} else {
250+
// Avoid engaging the contextual log channel on rollout.
230251
console.error(`Error cleaning up server: ${result?.reason?.message || result?.reason || 'Unknown error'}`);
231252
}
232253
}
@@ -236,6 +257,7 @@ runServer.memo = memo(
236257

237258
export {
238259
runServer,
260+
builtinTools,
239261
type McpTool,
240262
type McpToolCreator,
241263
type ServerInstance,

0 commit comments

Comments
 (0)