@@ -81,12 +81,16 @@ export type TestConnectionManager = ConnectionManager & {
8181} ;
8282
8383export abstract class ConnectionManager {
84- protected clientName : string = "unknown" ;
84+ protected clientName : string ;
85+ protected readonly _events ;
86+ readonly events : Pick < EventEmitter < ConnectionManagerEvents > , "on" | "off" | "once" > ;
87+ private state : AnyConnectionState ;
8588
86- protected readonly _events = new EventEmitter < ConnectionManagerEvents > ( ) ;
87- readonly events : Pick < EventEmitter < ConnectionManagerEvents > , "on" | "off" | "once" > = this . _events ;
88-
89- protected state : AnyConnectionState = { tag : "disconnected" } ;
89+ constructor ( ) {
90+ this . clientName = "unknown" ;
91+ this . events = this . _events = new EventEmitter < ConnectionManagerEvents > ( ) ;
92+ this . state = { tag : "disconnected" } ;
93+ }
9094
9195 get currentConnectionState ( ) : AnyConnectionState {
9296 return this . state ;
@@ -132,9 +136,9 @@ export class MCPConnectionManager extends ConnectionManager {
132136 }
133137
134138 async connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > {
135- this . _events . emit ( "connection-requested" , this . state ) ;
139+ this . _events . emit ( "connection-requested" , this . currentConnectionState ) ;
136140
137- if ( this . state . tag === "connected" || this . state . tag === "connecting" ) {
141+ if ( this . currentConnectionState . tag === "connected" || this . currentConnectionState . tag === "connecting" ) {
138142 await this . disconnect ( ) ;
139143 }
140144
@@ -225,13 +229,13 @@ export class MCPConnectionManager extends ConnectionManager {
225229 }
226230
227231 async disconnect ( ) : Promise < ConnectionStateDisconnected | ConnectionStateErrored > {
228- if ( this . state . tag === "disconnected" || this . state . tag === "errored" ) {
229- return this . state ;
232+ if ( this . currentConnectionState . tag === "disconnected" || this . currentConnectionState . tag === "errored" ) {
233+ return this . currentConnectionState ;
230234 }
231235
232- if ( this . state . tag === "connected" || this . state . tag === "connecting" ) {
236+ if ( this . currentConnectionState . tag === "connected" || this . currentConnectionState . tag === "connecting" ) {
233237 try {
234- await this . state . serviceProvider ?. close ( true ) ;
238+ await this . currentConnectionState . serviceProvider ?. close ( true ) ;
235239 } finally {
236240 this . changeState ( "connection-closed" , {
237241 tag : "disconnected" ,
@@ -243,14 +247,20 @@ export class MCPConnectionManager extends ConnectionManager {
243247 }
244248
245249 private onOidcAuthFailed ( error : unknown ) : void {
246- if ( this . state . tag === "connecting" && this . state . connectionStringAuthType ?. startsWith ( "oidc" ) ) {
250+ if (
251+ this . currentConnectionState . tag === "connecting" &&
252+ this . currentConnectionState . connectionStringAuthType ?. startsWith ( "oidc" )
253+ ) {
247254 void this . disconnectOnOidcError ( error ) ;
248255 }
249256 }
250257
251258 private onOidcAuthSucceeded ( ) : void {
252- if ( this . state . tag === "connecting" && this . state . connectionStringAuthType ?. startsWith ( "oidc" ) ) {
253- this . changeState ( "connection-succeeded" , { ...this . state , tag : "connected" } ) ;
259+ if (
260+ this . currentConnectionState . tag === "connecting" &&
261+ this . currentConnectionState . connectionStringAuthType ?. startsWith ( "oidc" )
262+ ) {
263+ this . changeState ( "connection-succeeded" , { ...this . currentConnectionState , tag : "connected" } ) ;
254264 }
255265
256266 this . logger . info ( {
@@ -261,9 +271,12 @@ export class MCPConnectionManager extends ConnectionManager {
261271 }
262272
263273 private onOidcNotifyDeviceFlow ( flowInfo : { verificationUrl : string ; userCode : string } ) : void {
264- if ( this . state . tag === "connecting" && this . state . connectionStringAuthType ?. startsWith ( "oidc" ) ) {
274+ if (
275+ this . currentConnectionState . tag === "connecting" &&
276+ this . currentConnectionState . connectionStringAuthType ?. startsWith ( "oidc" )
277+ ) {
265278 this . changeState ( "connection-requested" , {
266- ...this . state ,
279+ ...this . currentConnectionState ,
267280 tag : "connecting" ,
268281 connectionStringAuthType : "oidc-device-flow" ,
269282 oidcLoginUrl : flowInfo . verificationUrl ,
0 commit comments