-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathindex.ts
226 lines (205 loc) · 5.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { ProgressCallback, Protocol } from "../shared/protocol.js";
import { Transport } from "../shared/transport.js";
import {
ClientNotification,
ClientRequest,
ClientResult,
Implementation,
InitializeResultSchema,
Notification,
PROTOCOL_VERSION,
Request,
Result,
ServerCapabilities,
CompleteRequest,
GetPromptRequest,
ListPromptsRequest,
ListResourcesRequest,
ReadResourceRequest,
SubscribeRequest,
UnsubscribeRequest,
CallToolRequest,
ListToolsRequest,
CompleteResultSchema,
GetPromptResultSchema,
ListPromptsResultSchema,
ListResourcesResultSchema,
ReadResourceResultSchema,
CallToolResultSchema,
ListToolsResultSchema,
EmptyResultSchema,
LoggingLevel,
} from "../types.js";
/**
* An MCP client on top of a pluggable transport.
*
* The client will automatically begin the initialization flow with the server when connect() is called.
*
* To use with custom types, extend the base Request/Notification/Result types and pass them as type parameters:
*
* ```typescript
* // Custom schemas
* const CustomRequestSchema = RequestSchema.extend({...})
* const CustomNotificationSchema = NotificationSchema.extend({...})
* const CustomResultSchema = ResultSchema.extend({...})
*
* // Type aliases
* type CustomRequest = z.infer<typeof CustomRequestSchema>
* type CustomNotification = z.infer<typeof CustomNotificationSchema>
* type CustomResult = z.infer<typeof CustomResultSchema>
*
* // Create typed client
* const client = new Client<CustomRequest, CustomNotification, CustomResult>({
* name: "CustomClient",
* version: "1.0.0"
* })
* ```
*/
export class Client<
RequestT extends Request = Request,
NotificationT extends Notification = Notification,
ResultT extends Result = Result,
> extends Protocol<
ClientRequest | RequestT,
ClientNotification | NotificationT,
ClientResult | ResultT
> {
private _serverCapabilities?: ServerCapabilities;
private _serverVersion?: Implementation;
/**
* Initializes this client with the given name and version information.
*/
constructor(private _clientInfo: Implementation) {
super();
}
override async connect(transport: Transport): Promise<void> {
await super.connect(transport);
const result = await this.request(
{
method: "initialize",
params: {
protocolVersion: PROTOCOL_VERSION,
capabilities: {},
clientInfo: this._clientInfo,
},
},
InitializeResultSchema,
);
if (result === undefined) {
throw new Error(`Server sent invalid initialize result: ${result}`);
}
if (result.protocolVersion !== PROTOCOL_VERSION) {
throw new Error(
`Server's protocol version is not supported: ${result.protocolVersion}`,
);
}
this._serverCapabilities = result.capabilities;
this._serverVersion = result.serverInfo;
await this.notification({
method: "notifications/initialized",
});
}
/**
* After initialization has completed, this will be populated with the server's reported capabilities.
*/
getServerCapabilities(): ServerCapabilities | undefined {
return this._serverCapabilities;
}
/**
* After initialization has completed, this will be populated with information about the server's name and version.
*/
getServerVersion(): Implementation | undefined {
return this._serverVersion;
}
async ping() {
return this.request({ method: "ping" }, EmptyResultSchema);
}
async complete(
params: CompleteRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "completion/complete", params },
CompleteResultSchema,
onprogress,
);
}
async setLoggingLevel(level: LoggingLevel) {
return this.request(
{ method: "logging/setLevel", params: { level } },
EmptyResultSchema,
);
}
async getPrompt(
params: GetPromptRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "prompts/get", params },
GetPromptResultSchema,
onprogress,
);
}
async listPrompts(
params?: ListPromptsRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "prompts/list", params },
ListPromptsResultSchema,
onprogress,
);
}
async listResources(
params?: ListResourcesRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "resources/list", params },
ListResourcesResultSchema,
onprogress,
);
}
async readResource(
params: ReadResourceRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "resources/read", params },
ReadResourceResultSchema,
onprogress,
);
}
async subscribeResource(params: SubscribeRequest["params"]) {
return this.request(
{ method: "resources/subscribe", params },
EmptyResultSchema,
);
}
async unsubscribeResource(params: UnsubscribeRequest["params"]) {
return this.request(
{ method: "resources/unsubscribe", params },
EmptyResultSchema,
);
}
async callTool(
params: CallToolRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "tools/call", params },
CallToolResultSchema,
onprogress,
);
}
async listTools(
params?: ListToolsRequest["params"],
onprogress?: ProgressCallback,
) {
return this.request(
{ method: "tools/list", params },
ListToolsResultSchema,
onprogress,
);
}
}