Skip to content

Commit 13201fe

Browse files
committed
wip
1 parent 7b033ad commit 13201fe

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/server.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@ export class Server {
3535
}
3636

3737
async connect(transport: Transport): Promise<void> {
38+
try {
39+
await this.setupAndConnect(transport);
40+
} catch (error) {
41+
this.emitServerEvent("stop", Date.now() - this.startTime, error as Error);
42+
throw error;
43+
}
44+
}
45+
46+
private async setupAndConnect(transport: Transport): Promise<void> {
3847
this.mcpServer.server.registerCapabilities({ logging: {} });
48+
await this.setServerCallbacks(transport);
49+
this.emitServerEvent("start", Date.now() - this.startTime);
3950

4051
this.registerTools();
4152
this.registerResources();
@@ -64,20 +75,27 @@ export class Server {
6475
});
6576

6677
await initializeLogger(this.mcpServer, this.userConfig.logPath);
78+
await this.validateConfig();
6779

6880
await this.mcpServer.connect(transport);
81+
}
6982

83+
/**
84+
* Sets up the MCP serve instance by registering capabilities and setting up event listeners.
85+
* @param transport - The transport to use for connecting to the server.
86+
*/
87+
async setServerCallbacks(transport: Transport) {
7088
this.mcpServer.server.oninitialized = () => {
7189
this.session.setAgentRunner(this.mcpServer.server.getClientVersion());
7290
this.session.sessionId = new ObjectId().toString();
7391

7492
logger.info(
7593
LogId.serverInitialized,
7694
"server",
77-
`Server started with transport ${transport.constructor.name} and agent runner ${this.session.agentRunner?.name}`
95+
`Server connected with transport ${transport.constructor.name} and agent runner ${this.session.agentRunner?.name}`
7896
);
7997

80-
this.emitServerEvent("start", Date.now() - this.startTime);
98+
this.emitServerEvent("connect", Date.now() - this.startTime);
8199
};
82100

83101
this.mcpServer.server.onclose = () => {
@@ -88,9 +106,7 @@ export class Server {
88106
this.mcpServer.server.onerror = (error: Error) => {
89107
const closeTime = Date.now();
90108
this.emitServerEvent("stop", Date.now() - closeTime, error);
91-
};
92-
93-
await this.validateConfig();
109+
};
94110
}
95111

96112
async close(): Promise<void> {
@@ -101,7 +117,7 @@ export class Server {
101117

102118
/**
103119
* Emits a server event
104-
* @param command - The server command (e.g., "start", "stop", "register", "deregister")
120+
* @param command - The server command (e.g., "start", "stop", "connect")
105121
* @param additionalProperties - Additional properties specific to the event
106122
*/
107123
private emitServerEvent(command: ServerCommand, commandDuration: number, error?: Error) {
@@ -117,7 +133,7 @@ export class Server {
117133
},
118134
};
119135

120-
if (command === "start") {
136+
if (command === "start" || command === "connect") {
121137
event.properties.startup_time_ms = commandDuration;
122138
event.properties.read_only_mode = this.userConfig.readOnly || false;
123139
event.properties.disabled_tools = this.userConfig.disabledTools || [];

src/telemetry/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Result type constants for telemetry events
33
*/
44
export type TelemetryResult = "success" | "failure";
5-
export type ServerCommand = "start" | "stop";
5+
export type ServerCommand = "start" | "stop" | "connect";
66
export type TelemetryBoolSet = "true" | "false";
77

88
/**

0 commit comments

Comments
 (0)