@@ -8,6 +8,8 @@ import { mongoLogId } from "mongodb-log-writer";
88import { ObjectId } from "mongodb" ;
99import { Telemetry } from "./telemetry/telemetry.js" ;
1010import { UserConfig } from "./config.js" ;
11+ import { type ServerEvent } from "./telemetry/types.js" ;
12+ import { type ServerCommand } from "./telemetry/types.js" ;
1113
1214export interface ServerOptions {
1315 session : Session ;
@@ -20,8 +22,10 @@ export class Server {
2022 private readonly mcpServer : McpServer ;
2123 private readonly telemetry : Telemetry ;
2224 private readonly userConfig : UserConfig ;
25+ private readonly startTime : number ;
2326
2427 constructor ( { session, mcpServer, userConfig } : ServerOptions ) {
28+ this . startTime = Date . now ( ) ;
2529 this . session = session ;
2630 this . telemetry = new Telemetry ( session ) ;
2731 this . mcpServer = mcpServer ;
@@ -46,12 +50,47 @@ export class Server {
4650 "server" ,
4751 `Server started with transport ${ transport . constructor . name } and agent runner ${ this . session . agentRunner ?. name } `
4852 ) ;
53+
54+ this . emitServerEvent ( "start" , Date . now ( ) - this . startTime ) ;
4955 } ;
5056 }
5157
5258 async close ( ) : Promise < void > {
59+ const closeTime = Date . now ( ) ;
5360 await this . session . close ( ) ;
5461 await this . mcpServer . close ( ) ;
62+
63+ this . emitServerEvent ( "stop" , Date . now ( ) - closeTime ) ;
64+ }
65+
66+
67+ /**
68+ * Emits a server event
69+ * @param command - The server command (e.g., "start", "stop", "register", "deregister")
70+ * @param additionalProperties - Additional properties specific to the event
71+ */
72+ async emitServerEvent ( command : ServerCommand , commandDuration : number ) : Promise < void > {
73+ const event : ServerEvent = {
74+ timestamp : new Date ( ) . toISOString ( ) ,
75+ source : "mdbmcp" ,
76+ properties : {
77+ ...this . telemetry . getCommonProperties ( ) ,
78+ result : "success" ,
79+ duration_ms : commandDuration ,
80+ component : "server" ,
81+ category : "other" ,
82+ command : command ,
83+ } ,
84+ } ;
85+
86+ if ( command === "start" ) {
87+ event . properties . startup_time_ms = commandDuration ;
88+ }
89+ if ( command === "stop" ) {
90+ event . properties . runtime_duration_ms = Date . now ( ) - this . startTime ;
91+ }
92+
93+ await this . telemetry . emitEvents ( [ event ] ) ;
5594 }
5695
5796 private registerTools ( ) {
0 commit comments