@@ -32,7 +32,7 @@ import {
32
32
ServerCapabilities ,
33
33
SubscribeRequest ,
34
34
SUPPORTED_PROTOCOL_VERSIONS ,
35
- UnsubscribeRequest
35
+ UnsubscribeRequest ,
36
36
} from "../types.js" ;
37
37
38
38
/**
@@ -82,34 +82,40 @@ export class Client<
82
82
override async connect ( transport : Transport ) : Promise < void > {
83
83
await super . connect ( transport ) ;
84
84
85
- const result = await this . request (
86
- {
87
- method : "initialize" ,
88
- params : {
89
- protocolVersion : LATEST_PROTOCOL_VERSION ,
90
- capabilities : { } ,
91
- clientInfo : this . _clientInfo ,
85
+ try {
86
+ const result = await this . request (
87
+ {
88
+ method : "initialize" ,
89
+ params : {
90
+ protocolVersion : LATEST_PROTOCOL_VERSION ,
91
+ capabilities : { } ,
92
+ clientInfo : this . _clientInfo ,
93
+ } ,
92
94
} ,
93
- } ,
94
- InitializeResultSchema ,
95
- ) ;
96
-
97
- if ( result === undefined ) {
98
- throw new Error ( `Server sent invalid initialize result: ${ result } ` ) ;
99
- }
100
-
101
- if ( ! SUPPORTED_PROTOCOL_VERSIONS . includes ( result . protocolVersion ) ) {
102
- throw new Error (
103
- `Server's protocol version is not supported: ${ result . protocolVersion } ` ,
95
+ InitializeResultSchema ,
104
96
) ;
105
- }
106
-
107
- this . _serverCapabilities = result . capabilities ;
108
- this . _serverVersion = result . serverInfo ;
109
97
110
- await this . notification ( {
111
- method : "notifications/initialized" ,
112
- } ) ;
98
+ if ( result === undefined ) {
99
+ throw new Error ( `Server sent invalid initialize result: ${ result } ` ) ;
100
+ }
101
+
102
+ if ( ! SUPPORTED_PROTOCOL_VERSIONS . includes ( result . protocolVersion ) ) {
103
+ throw new Error (
104
+ `Server's protocol version is not supported: ${ result . protocolVersion } ` ,
105
+ ) ;
106
+ }
107
+
108
+ this . _serverCapabilities = result . capabilities ;
109
+ this . _serverVersion = result . serverInfo ;
110
+
111
+ await this . notification ( {
112
+ method : "notifications/initialized" ,
113
+ } ) ;
114
+ } catch ( error ) {
115
+ // Disconnect if initialization fails.
116
+ void this . close ( ) ;
117
+ throw error ;
118
+ }
113
119
}
114
120
115
121
/**
@@ -219,7 +225,9 @@ export class Client<
219
225
220
226
async callTool (
221
227
params : CallToolRequest [ "params" ] ,
222
- resultSchema : typeof CallToolResultSchema | typeof CompatibilityCallToolResultSchema = CallToolResultSchema ,
228
+ resultSchema :
229
+ | typeof CallToolResultSchema
230
+ | typeof CompatibilityCallToolResultSchema = CallToolResultSchema ,
223
231
onprogress ?: ProgressCallback ,
224
232
) {
225
233
return this . request (
0 commit comments