@@ -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" ;
1113import { CallToolRequestSchema , CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
1214import assert from "assert" ;
1315
@@ -22,8 +24,10 @@ export class Server {
2224 private readonly mcpServer : McpServer ;
2325 private readonly telemetry : Telemetry ;
2426 private readonly userConfig : UserConfig ;
27+ private readonly startTime : number ;
2528
2629 constructor ( { session, mcpServer, userConfig } : ServerOptions ) {
30+ this . startTime = Date . now ( ) ;
2731 this . session = session ;
2832 this . telemetry = new Telemetry ( session ) ;
2933 this . mcpServer = mcpServer ;
@@ -71,6 +75,18 @@ export class Server {
7175 "server" ,
7276 `Server started with transport ${ transport . constructor . name } and agent runner ${ this . session . agentRunner ?. name } `
7377 ) ;
78+
79+ this . emitServerEvent ( "start" , Date . now ( ) - this . startTime ) ;
80+ } ;
81+
82+ this . mcpServer . server . onclose = ( ) => {
83+ const closeTime = Date . now ( ) ;
84+ this . emitServerEvent ( "stop" , Date . now ( ) - closeTime ) ;
85+ } ;
86+
87+ this . mcpServer . server . onerror = ( error : Error ) => {
88+ const closeTime = Date . now ( ) ;
89+ this . emitServerEvent ( "stop" , Date . now ( ) - closeTime , error ) ;
7490 } ;
7591 }
7692
@@ -79,6 +95,39 @@ export class Server {
7995 await this . mcpServer . close ( ) ;
8096 }
8197
98+ /**
99+ * Emits a server event
100+ * @param command - The server command (e.g., "start", "stop", "register", "deregister")
101+ * @param additionalProperties - Additional properties specific to the event
102+ */
103+ emitServerEvent ( command : ServerCommand , commandDuration : number , error ?: Error ) {
104+ const event : ServerEvent = {
105+ timestamp : new Date ( ) . toISOString ( ) ,
106+ source : "mdbmcp" ,
107+ properties : {
108+ ...this . telemetry . getCommonProperties ( ) ,
109+ result : "success" ,
110+ duration_ms : commandDuration ,
111+ component : "server" ,
112+ category : "other" ,
113+ command : command ,
114+ } ,
115+ } ;
116+
117+ if ( command === "start" ) {
118+ event . properties . startup_time_ms = commandDuration ;
119+ }
120+ if ( command === "stop" ) {
121+ event . properties . runtime_duration_ms = Date . now ( ) - this . startTime ;
122+ if ( error ) {
123+ event . properties . result = "failure" ;
124+ event . properties . reason = error . message ;
125+ }
126+ }
127+
128+ this . telemetry . emitEvents ( [ event ] ) . catch ( ( ) => { } ) ;
129+ }
130+
82131 private registerTools ( ) {
83132 for ( const tool of [ ...AtlasTools , ...MongoDbTools ] ) {
84133 new tool ( this . session , this . userConfig , this . telemetry ) . register ( this . mcpServer ) ;
0 commit comments