@@ -8,6 +8,8 @@ import { mongoLogId } from "mongodb-log-writer";
8
8
import { ObjectId } from "mongodb" ;
9
9
import { Telemetry } from "./telemetry/telemetry.js" ;
10
10
import { UserConfig } from "./config.js" ;
11
+ import { type ServerEvent } from "./telemetry/types.js" ;
12
+ import { type ServerCommand } from "./telemetry/types.js" ;
11
13
import { CallToolRequestSchema , CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
12
14
import assert from "assert" ;
13
15
@@ -22,8 +24,10 @@ export class Server {
22
24
private readonly mcpServer : McpServer ;
23
25
private readonly telemetry : Telemetry ;
24
26
private readonly userConfig : UserConfig ;
27
+ private readonly startTime : number ;
25
28
26
29
constructor ( { session, mcpServer, userConfig } : ServerOptions ) {
30
+ this . startTime = Date . now ( ) ;
27
31
this . session = session ;
28
32
this . telemetry = new Telemetry ( session ) ;
29
33
this . mcpServer = mcpServer ;
@@ -71,6 +75,18 @@ export class Server {
71
75
"server" ,
72
76
`Server started with transport ${ transport . constructor . name } and agent runner ${ this . session . agentRunner ?. name } `
73
77
) ;
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 ) ;
74
90
} ;
75
91
}
76
92
@@ -79,6 +95,39 @@ export class Server {
79
95
await this . mcpServer . close ( ) ;
80
96
}
81
97
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
+
82
131
private registerTools ( ) {
83
132
for ( const tool of [ ...AtlasTools , ...MongoDbTools ] ) {
84
133
new tool ( this . session , this . userConfig , this . telemetry ) . register ( this . mcpServer ) ;
0 commit comments