Skip to content

Commit c82d633

Browse files
committed
Disconnect from client side if initialization fails
Resolves #24.
1 parent 8ea66e7 commit c82d633

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/client/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ test("should reject unsupported protocol version", async () => {
122122
await expect(client.connect(clientTransport)).rejects.toThrow(
123123
"Server's protocol version is not supported: invalid-version",
124124
);
125+
126+
expect(clientTransport.close).toHaveBeenCalled();
125127
});
126128

127129
/*

src/client/index.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
ServerCapabilities,
3333
SubscribeRequest,
3434
SUPPORTED_PROTOCOL_VERSIONS,
35-
UnsubscribeRequest
35+
UnsubscribeRequest,
3636
} from "../types.js";
3737

3838
/**
@@ -82,34 +82,40 @@ export class Client<
8282
override async connect(transport: Transport): Promise<void> {
8383
await super.connect(transport);
8484

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+
},
9294
},
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,
10496
);
105-
}
106-
107-
this._serverCapabilities = result.capabilities;
108-
this._serverVersion = result.serverInfo;
10997

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+
}
113119
}
114120

115121
/**
@@ -219,7 +225,9 @@ export class Client<
219225

220226
async callTool(
221227
params: CallToolRequest["params"],
222-
resultSchema: typeof CallToolResultSchema | typeof CompatibilityCallToolResultSchema = CallToolResultSchema,
228+
resultSchema:
229+
| typeof CallToolResultSchema
230+
| typeof CompatibilityCallToolResultSchema = CallToolResultSchema,
223231
onprogress?: ProgressCallback,
224232
) {
225233
return this.request(

0 commit comments

Comments
 (0)