Skip to content

Commit a3b1a12

Browse files
committed
can use the presence of a connection instead of a first render flag
1 parent fbef1f8 commit a3b1a12

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/server/mcp.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ type RenderApi = {
3939
prompt: McpServer["prompt"];
4040
};
4141

42-
type McpServerOptions<T extends Record<string, any> = Record<string, any>> =
43-
ServerOptions & {
42+
type McpServerOptions<T> = ServerOptions & {
4443
render?: (api: RenderApi, args: T) => void | Promise<void>;
4544
};
4645

@@ -49,7 +48,7 @@ type McpServerOptions<T extends Record<string, any> = Record<string, any>> =
4948
* For advanced usage (like sending notifications or setting custom request handlers), use the underlying
5049
* Server instance available via the `server` property.
5150
*/
52-
export class McpServer<RenderArgs extends Record<string, any> = Record<string, any>> {
51+
export class McpServer<RenderArgs extends Record<string, unknown> | undefined = undefined> {
5352
/**
5453
* The underlying Server instance, useful for advanced operations like sending notifications.
5554
*/
@@ -67,7 +66,7 @@ export class McpServer<RenderArgs extends Record<string, any> = Record<string, a
6766
args: RenderArgs,
6867
) => void | Promise<void>;
6968
private readonly _locked: boolean;
70-
private _isFirstRender: boolean = true;
69+
private _isConnected: boolean = true;
7170

7271
constructor(serverInfo: Implementation, options?: McpServerOptions<RenderArgs>) {
7372
this.server = new Server(serverInfo, options);
@@ -81,6 +80,7 @@ export class McpServer<RenderArgs extends Record<string, any> = Record<string, a
8180
* The `server` object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.
8281
*/
8382
async connect(transport: Transport): Promise<void> {
83+
this._isConnected = true
8484
return await this.server.connect(transport);
8585
}
8686

@@ -529,15 +529,12 @@ export class McpServer<RenderArgs extends Record<string, any> = Record<string, a
529529
this.setPromptRequestHandlers();
530530
this.setResourceRequestHandlers();
531531

532-
// Emit change events only if state *actually* changed and it's not the first render
533-
if (!this._isFirstRender) {
532+
// Emit change events if we have a transport to emit to
533+
if (!this._isConnected) {
534534
if (toolsChanged) this.server.sendToolListChanged()
535535
if (promptsChanged) this.server.sendPromptListChanged()
536536
if (resourcesChanged) this.server.sendResourceListChanged()
537537
}
538-
539-
// --- 6. Mark first render as complete ---
540-
this._isFirstRender = false;
541538
}
542539

543540
/**

0 commit comments

Comments
 (0)