@@ -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 || [ ] ;
0 commit comments