@opentiny/next-sdk 0.1.14 → 0.1.15-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent/AgentModelProvider.ts +546 -22
- package/agent/type.ts +12 -6
- package/agent/utils/generateReActPrompt.ts +55 -0
- package/agent/utils/parseReActAction.ts +34 -0
- package/dist/McpSdk.d.ts +14 -0
- package/dist/WebAgent.d.ts +5 -0
- package/dist/WebMcp.d.ts +20 -0
- package/dist/WebMcpClient.d.ts +389 -1152
- package/dist/WebMcpServer.d.ts +79 -78
- package/dist/Zod.d.ts +1 -0
- package/dist/agent/AgentModelProvider.d.ts +40 -4
- package/dist/agent/type.d.ts +13 -3
- package/dist/agent/utils/generateReActPrompt.d.ts +9 -0
- package/dist/agent/utils/getAISDKTools.d.ts +1 -0
- package/dist/agent/utils/parseReActAction.d.ts +14 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.es.dev.js +36353 -34348
- package/dist/index.es.js +28821 -25466
- package/dist/index.js +3880 -25
- package/dist/index.umd.dev.js +34230 -32225
- package/dist/index.umd.js +220 -128
- package/dist/mcpsdk@1.24.3.dev.js +22539 -0
- package/dist/mcpsdk@1.24.3.es.dev.js +22537 -0
- package/dist/mcpsdk@1.24.3.es.js +16781 -0
- package/dist/mcpsdk@1.24.3.js +43 -0
- package/dist/remoter/createRemoter.d.ts +9 -0
- package/dist/remoter/tooltips.d.ts +36 -0
- package/dist/script/utils.d.ts +1 -0
- package/dist/transport/ExtensionClientTransport.d.ts +3 -2
- package/dist/transport/ExtensionContentServerTransport.d.ts +3 -2
- package/dist/transport/ExtensionPageServerTransport.d.ts +4 -4
- package/dist/vite-build-tsc.d.ts +2 -0
- package/dist/vite.config.d.ts +2 -0
- package/dist/vite.config.mcpSdk.d.ts +2 -0
- package/dist/vite.config.webAgent.d.ts +2 -0
- package/dist/vite.config.webMcp.d.ts +2 -0
- package/dist/vite.config.webMcpFull.d.ts +2 -0
- package/dist/vite.config.zod.d.ts +2 -0
- package/dist/webagent.dev.js +24569 -20836
- package/dist/webagent.es.dev.js +23907 -20174
- package/dist/webagent.es.js +25326 -20723
- package/dist/webagent.js +209 -110
- package/dist/webmcp-full.dev.js +21225 -20021
- package/dist/webmcp-full.es.dev.js +21223 -20019
- package/dist/webmcp-full.es.js +16710 -14437
- package/dist/webmcp-full.js +42 -15
- package/dist/webmcp.dev.js +14 -22
- package/dist/webmcp.es.dev.js +12 -20
- package/dist/webmcp.es.js +172 -179
- package/dist/webmcp.js +1 -1
- package/dist/zod@3.25.76.dev.js +30 -32
- package/dist/zod@3.25.76.es.dev.js +28 -30
- package/dist/zod@3.25.76.es.js +143 -145
- package/dist/zod@3.25.76.js +1 -1
- package/package.json +11 -9
- package/remoter/createRemoter.ts +126 -71
- package/remoter/tooltips.ts +260 -0
- package/transport/ExtensionPageServerTransport.ts +2 -4
- package/tsconfig.json +5 -3
- package/vite-build-tsc.ts +60 -0
- package/vite-env.d.ts +5 -0
- package/dist/WebMcpClient.js +0 -363
- package/dist/WebMcpServer.js +0 -283
- package/dist/agent/AgentModelProvider.js +0 -293
- package/dist/agent/type.js +0 -1
- package/dist/agent/utils/getAISDKTools.js +0 -36
- package/dist/mcpsdk@1.17.0.dev.js +0 -21391
- package/dist/mcpsdk@1.17.0.es.dev.js +0 -21389
- package/dist/mcpsdk@1.17.0.es.js +0 -14505
- package/dist/mcpsdk@1.17.0.js +0 -16
- package/dist/remoter/QrCode.js +0 -55
- package/dist/remoter/createRemoter.js +0 -743
- package/dist/transport/ExtensionClientTransport.js +0 -81
- package/dist/transport/ExtensionContentServerTransport.js +0 -128
- package/dist/transport/ExtensionPageServerTransport.js +0 -118
- package/dist/transport/messages.js +0 -51
- package/dist/utils/uuid.js +0 -10
package/dist/WebMcpClient.js
DELETED
|
@@ -1,363 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
11
|
-
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
12
|
-
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
13
|
-
import { WebSocketClientTransport } from '@modelcontextprotocol/sdk/client/websocket.js';
|
|
14
|
-
import { ElicitRequestSchema, CallToolResultSchema, ListRootsRequestSchema, CreateMessageRequestSchema, LoggingMessageNotificationSchema, ToolListChangedNotificationSchema, ResourceUpdatedNotificationSchema, PromptListChangedNotificationSchema, ResourceListChangedNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
15
|
-
import { MessageChannelClientTransport, sseOptions, streamOptions, createSseProxy, createStreamProxy, createSocketProxy } from '@opentiny/next';
|
|
16
|
-
/**
|
|
17
|
-
* An MCP client on top of a pluggable transport.
|
|
18
|
-
* The client will automatically begin the initialization flow with the server when connect() is called.
|
|
19
|
-
*/
|
|
20
|
-
export class WebMcpClient {
|
|
21
|
-
constructor(clientInfo, options) {
|
|
22
|
-
const info = {
|
|
23
|
-
name: 'web-mcp-client',
|
|
24
|
-
version: '1.0.0'
|
|
25
|
-
};
|
|
26
|
-
const capabilities = {
|
|
27
|
-
roots: { listChanged: true },
|
|
28
|
-
sampling: {},
|
|
29
|
-
elicitation: {}
|
|
30
|
-
};
|
|
31
|
-
this.client = new Client(clientInfo || info, options || { capabilities });
|
|
32
|
-
this.client.onclose = () => {
|
|
33
|
-
var _a;
|
|
34
|
-
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
35
|
-
};
|
|
36
|
-
this.client.onerror = (error) => {
|
|
37
|
-
var _a;
|
|
38
|
-
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Connects the client to a transport via the specified option.
|
|
43
|
-
*/
|
|
44
|
-
connect(options) {
|
|
45
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
if (typeof options['start'] === 'function') {
|
|
47
|
-
this.transport = options;
|
|
48
|
-
this.transport.onclose = undefined;
|
|
49
|
-
this.transport.onerror = undefined;
|
|
50
|
-
this.transport.onmessage = undefined;
|
|
51
|
-
yield this.client.connect(this.transport);
|
|
52
|
-
return { transport: this.transport, sessionId: this.transport.sessionId };
|
|
53
|
-
}
|
|
54
|
-
const { url, token, sessionId, type, agent, onError } = options;
|
|
55
|
-
if (agent === true) {
|
|
56
|
-
const proxyOptions = { client: this.client, url, token, sessionId };
|
|
57
|
-
let response;
|
|
58
|
-
const connectProxy = () => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
const { transport, sessionId } = type === 'sse'
|
|
60
|
-
? yield createSseProxy(proxyOptions)
|
|
61
|
-
: type === 'socket'
|
|
62
|
-
? yield createSocketProxy(proxyOptions)
|
|
63
|
-
: yield createStreamProxy(proxyOptions);
|
|
64
|
-
transport.onerror = (error) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
-
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
66
|
-
});
|
|
67
|
-
response = { transport, sessionId };
|
|
68
|
-
});
|
|
69
|
-
yield connectProxy();
|
|
70
|
-
return response;
|
|
71
|
-
}
|
|
72
|
-
const endpoint = new URL(url);
|
|
73
|
-
let transport;
|
|
74
|
-
if (type === 'channel') {
|
|
75
|
-
transport = new MessageChannelClientTransport(url);
|
|
76
|
-
yield this.client.connect(transport);
|
|
77
|
-
}
|
|
78
|
-
if (type === 'sse') {
|
|
79
|
-
const opts = sseOptions(token, sessionId);
|
|
80
|
-
transport = new SSEClientTransport(endpoint, opts);
|
|
81
|
-
yield this.client.connect(transport);
|
|
82
|
-
}
|
|
83
|
-
if (type === 'socket') {
|
|
84
|
-
transport = new WebSocketClientTransport(new URL(`${url}?sessionId=${sessionId}&token=${token}`));
|
|
85
|
-
transport.sessionId = sessionId;
|
|
86
|
-
yield this.client.connect(transport);
|
|
87
|
-
}
|
|
88
|
-
if (typeof transport === 'undefined') {
|
|
89
|
-
const opts = streamOptions(token, sessionId);
|
|
90
|
-
transport = new StreamableHTTPClientTransport(endpoint, opts);
|
|
91
|
-
yield this.client.connect(transport);
|
|
92
|
-
}
|
|
93
|
-
this.transport = transport;
|
|
94
|
-
return { transport: this.transport, sessionId: this.transport.sessionId };
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Closes the connection.
|
|
99
|
-
*/
|
|
100
|
-
close() {
|
|
101
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
-
yield this.client.close();
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* After initialization has completed, this will be populated with the server's reported capabilities.
|
|
107
|
-
*/
|
|
108
|
-
getServerCapabilities() {
|
|
109
|
-
return this.client.getServerCapabilities();
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* After initialization has completed, this will be populated with information about the server's name and version.
|
|
113
|
-
*/
|
|
114
|
-
getServerVersion() {
|
|
115
|
-
return this.client.getServerVersion();
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* After initialization has completed, this may be populated with information about the server's instructions.
|
|
119
|
-
*/
|
|
120
|
-
getInstructions() {
|
|
121
|
-
return this.client.getInstructions();
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Sends a ping to the server to check if it is still connected.
|
|
125
|
-
*/
|
|
126
|
-
ping(options) {
|
|
127
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
-
return yield this.client.ping(options);
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Sends a completion request to the server.
|
|
133
|
-
*/
|
|
134
|
-
complete(params, options) {
|
|
135
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
-
return yield this.client.complete(params, options);
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Sends a request for setting the logging level to the server.
|
|
141
|
-
*/
|
|
142
|
-
setLoggingLevel(level, options) {
|
|
143
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
144
|
-
return yield this.client.setLoggingLevel(level, options);
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Gets the prompt with the given params from the server.
|
|
149
|
-
*/
|
|
150
|
-
getPrompt(params, options) {
|
|
151
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
-
return yield this.client.getPrompt(params, options);
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Lists all prompts available on the server.
|
|
157
|
-
*/
|
|
158
|
-
listPrompts(params, options) {
|
|
159
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
-
return yield this.client.listPrompts(params, options);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Lists all resources available on the server.
|
|
165
|
-
*/
|
|
166
|
-
listResources(params, options) {
|
|
167
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
168
|
-
return yield this.client.listResources(params, options);
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Lists all resource templates available on the server.
|
|
173
|
-
*/
|
|
174
|
-
listResourceTemplates(params, options) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
-
return yield this.client.listResourceTemplates(params, options);
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Reads the resource with the given params from the server.
|
|
181
|
-
*/
|
|
182
|
-
readResource(params, options) {
|
|
183
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
-
return yield this.client.readResource(params, options);
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Subscribes to a resource on the server.
|
|
189
|
-
*/
|
|
190
|
-
subscribeResource(params, options) {
|
|
191
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
-
return yield this.client.subscribeResource(params, options);
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Unsubscribes from a resource on the server.
|
|
197
|
-
*/
|
|
198
|
-
unsubscribeResource(params, options) {
|
|
199
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
200
|
-
return yield this.client.unsubscribeResource(params, options);
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Calls a tool on the server with the given parameters.
|
|
205
|
-
*/
|
|
206
|
-
callTool(params, options) {
|
|
207
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
208
|
-
return yield this.client.callTool(params, CallToolResultSchema, options);
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Lists all tools available on the server.
|
|
213
|
-
*/
|
|
214
|
-
listTools(params, options) {
|
|
215
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
216
|
-
return yield this.client.listTools(params, options);
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Sends a notification for the roots list changed event to the server.
|
|
221
|
-
*/
|
|
222
|
-
sendRootsListChanged() {
|
|
223
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
224
|
-
return yield this.client.sendRootsListChanged();
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Sends a request and wait for a response.
|
|
229
|
-
*
|
|
230
|
-
* Do not use this method to emit notifications! Use notification() instead.
|
|
231
|
-
*/
|
|
232
|
-
request(request, resultSchema, options) {
|
|
233
|
-
return this.client.request(request, resultSchema, options);
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Emits a notification, which is a one-way message that does not expect a response.
|
|
237
|
-
*/
|
|
238
|
-
notification(notification, options) {
|
|
239
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
240
|
-
return yield this.client.notification(notification, options);
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Registers a handler to invoke when this protocol object receives a request with the given method.
|
|
245
|
-
*
|
|
246
|
-
* Note that this will replace any previous request handler for the same method.
|
|
247
|
-
*/
|
|
248
|
-
setRequestHandler(requestSchema, handler) {
|
|
249
|
-
this.client.setRequestHandler(requestSchema, handler);
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Removes the request handler for the given method.
|
|
253
|
-
*/
|
|
254
|
-
removeRequestHandler(method) {
|
|
255
|
-
this.client.removeRequestHandler(method);
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Registers a handler to invoke when this protocol object receives a notification with the given method.
|
|
259
|
-
*
|
|
260
|
-
* Note that this will replace any previous notification handler for the same method.
|
|
261
|
-
*/
|
|
262
|
-
setNotificationHandler(notificationSchema, handler) {
|
|
263
|
-
this.client.setNotificationHandler(notificationSchema, handler);
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Removes the notification handler for the given method.
|
|
267
|
-
*/
|
|
268
|
-
removeNotificationHandler(method) {
|
|
269
|
-
this.client.removeNotificationHandler(method);
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Registers a handler for the elicitation request.
|
|
273
|
-
*/
|
|
274
|
-
onElicit(handler) {
|
|
275
|
-
this.client.setRequestHandler(ElicitRequestSchema, handler);
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Registers a handler for the create LLM message request.
|
|
279
|
-
*/
|
|
280
|
-
onCreateMessage(handler) {
|
|
281
|
-
this.client.setRequestHandler(CreateMessageRequestSchema, handler);
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Registers a handler for the list roots request.
|
|
285
|
-
*/
|
|
286
|
-
onListRoots(handler) {
|
|
287
|
-
this.client.setRequestHandler(ListRootsRequestSchema, handler);
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Registers a handler for the tool list changed notification.
|
|
291
|
-
*/
|
|
292
|
-
onToolListChanged(handler) {
|
|
293
|
-
this.client.setNotificationHandler(ToolListChangedNotificationSchema, handler);
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Registers a handler for the prompt list changed notification.
|
|
297
|
-
*/
|
|
298
|
-
onPromptListChanged(handler) {
|
|
299
|
-
this.client.setNotificationHandler(PromptListChangedNotificationSchema, handler);
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* Registers a handler for the resource list changed notification.
|
|
303
|
-
*/
|
|
304
|
-
onResourceListChanged(handler) {
|
|
305
|
-
this.client.setNotificationHandler(ResourceListChangedNotificationSchema, handler);
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Registers a handler for the resource updated notification.
|
|
309
|
-
*/
|
|
310
|
-
onResourceUpdated(handler) {
|
|
311
|
-
this.client.setNotificationHandler(ResourceUpdatedNotificationSchema, handler);
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Registers a handler for the logging message notification.
|
|
315
|
-
*/
|
|
316
|
-
onLoggingMessage(handler) {
|
|
317
|
-
this.client.setNotificationHandler(LoggingMessageNotificationSchema, handler);
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Close the transport for window.addEventListener('pagehide')
|
|
321
|
-
*/
|
|
322
|
-
onPagehide(event) {
|
|
323
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
324
|
-
if (event.persisted) {
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
if (isStreamableHTTPClientTransport(this.transport)) {
|
|
328
|
-
yield this.transport.terminateSession();
|
|
329
|
-
}
|
|
330
|
-
else if (this.transport && typeof this.transport['close'] === 'function') {
|
|
331
|
-
yield this.transport.close();
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Creates a new SSEClientTransport instance.
|
|
338
|
-
*/
|
|
339
|
-
export const createSSEClientTransport = (url, opts) => new SSEClientTransport(url, opts);
|
|
340
|
-
/**
|
|
341
|
-
* Creates a new StreamableHTTPClientTransport instance.
|
|
342
|
-
*/
|
|
343
|
-
export const createStreamableHTTPClientTransport = (url, opts) => new StreamableHTTPClientTransport(url, opts);
|
|
344
|
-
/**
|
|
345
|
-
* Creates a new MessageChannelClientTransport instance.
|
|
346
|
-
*/
|
|
347
|
-
export const createMessageChannelClientTransport = (endpoint, globalObject) => new MessageChannelClientTransport(endpoint, globalObject);
|
|
348
|
-
/**
|
|
349
|
-
* Checks if the transport is a SSEClientTransport.
|
|
350
|
-
*/
|
|
351
|
-
export const isSSEClientTransport = (transport) => transport instanceof SSEClientTransport;
|
|
352
|
-
/**
|
|
353
|
-
* Checks if the transport is a StreamableHTTPClientTransport.
|
|
354
|
-
*/
|
|
355
|
-
export const isStreamableHTTPClientTransport = (transport) => transport instanceof StreamableHTTPClientTransport;
|
|
356
|
-
/**
|
|
357
|
-
* Checks if the transport is a MessageChannelClientTransport.
|
|
358
|
-
*/
|
|
359
|
-
export const isMessageChannelClientTransport = (transport) => transport instanceof MessageChannelClientTransport;
|
|
360
|
-
/**
|
|
361
|
-
* Checks if the client is an instance of MCP Client.
|
|
362
|
-
*/
|
|
363
|
-
export const isMcpClient = (client) => client instanceof Client;
|
package/dist/WebMcpServer.js
DELETED
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { MessageChannelServerTransport, createTransportPair } from '@opentiny/next';
|
|
11
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
12
|
-
import { SetLevelRequestSchema, SubscribeRequestSchema, UnsubscribeRequestSchema, ListResourcesRequestSchema, RootsListChangedNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
13
|
-
/**
|
|
14
|
-
* High-level Web MCP server that provides a simpler API for working with resources, tools, and prompts.
|
|
15
|
-
* For advanced usage (like sending notifications or setting custom request handlers), use the underlying
|
|
16
|
-
* Server instance available via the `server` property.
|
|
17
|
-
*/
|
|
18
|
-
export class WebMcpServer {
|
|
19
|
-
constructor(serverInfo, options) {
|
|
20
|
-
const info = {
|
|
21
|
-
name: 'web-mcp-server',
|
|
22
|
-
version: '1.0.0'
|
|
23
|
-
};
|
|
24
|
-
const capabilities = {
|
|
25
|
-
prompts: { listChanged: true },
|
|
26
|
-
resources: { subscribe: true, listChanged: true },
|
|
27
|
-
tools: { listChanged: true },
|
|
28
|
-
completions: {},
|
|
29
|
-
logging: {}
|
|
30
|
-
};
|
|
31
|
-
this.server = new McpServer(serverInfo || info, options || { capabilities });
|
|
32
|
-
this.server.server.oninitialized = () => {
|
|
33
|
-
var _a;
|
|
34
|
-
(_a = this.oninitialized) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
35
|
-
};
|
|
36
|
-
this.server.server.onclose = () => {
|
|
37
|
-
var _a;
|
|
38
|
-
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
39
|
-
};
|
|
40
|
-
this.server.server.onerror = (error) => {
|
|
41
|
-
var _a;
|
|
42
|
-
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
43
|
-
};
|
|
44
|
-
this.server.server.setRequestHandler(SetLevelRequestSchema, () => __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
return {};
|
|
46
|
-
}));
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Connects the server to a transport via the specified option.
|
|
50
|
-
*/
|
|
51
|
-
connect(options) {
|
|
52
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
if (typeof options['start'] === 'function') {
|
|
54
|
-
this.transport = options;
|
|
55
|
-
this.transport.onclose = undefined;
|
|
56
|
-
this.transport.onerror = undefined;
|
|
57
|
-
this.transport.onmessage = undefined;
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
this.transport = new MessageChannelServerTransport(options);
|
|
61
|
-
yield this.transport.listen();
|
|
62
|
-
}
|
|
63
|
-
yield this.server.connect(this.transport);
|
|
64
|
-
return this.transport;
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Closes the connection.
|
|
69
|
-
*/
|
|
70
|
-
close() {
|
|
71
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
yield this.server.close();
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Registers a tool with a config object and callback.
|
|
77
|
-
*/
|
|
78
|
-
registerTool(name, config, cb) {
|
|
79
|
-
return this.server.registerTool(name, config, cb);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Registers a prompt with a config object and callback.
|
|
83
|
-
*/
|
|
84
|
-
registerPrompt(name, config, cb) {
|
|
85
|
-
return this.server.registerPrompt(name, config, cb);
|
|
86
|
-
}
|
|
87
|
-
registerResource(name, uriOrTemplate, config, readCallback) {
|
|
88
|
-
if (typeof uriOrTemplate === 'string') {
|
|
89
|
-
return this.server.registerResource(name, uriOrTemplate, config, readCallback);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
return this.server.registerResource(name, uriOrTemplate, config, readCallback);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Checks if the server is connected to a transport.
|
|
97
|
-
* @returns True if the server is connected
|
|
98
|
-
*/
|
|
99
|
-
isConnected() {
|
|
100
|
-
return this.server.isConnected();
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Sends a resource list changed event to the client, if connected.
|
|
104
|
-
*/
|
|
105
|
-
sendResourceListChanged() {
|
|
106
|
-
this.server.sendResourceListChanged();
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Sends a tool list changed event to the client, if connected.
|
|
110
|
-
*/
|
|
111
|
-
sendToolListChanged() {
|
|
112
|
-
this.server.sendToolListChanged();
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Sends a prompt list changed event to the client, if connected.
|
|
116
|
-
*/
|
|
117
|
-
sendPromptListChanged() {
|
|
118
|
-
this.server.sendPromptListChanged();
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* After initialization has completed, this will be populated with the client's reported capabilities.
|
|
122
|
-
*/
|
|
123
|
-
getClientCapabilities() {
|
|
124
|
-
return this.server.server.getClientCapabilities();
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* After initialization has completed, this will be populated with information about the client's name and version.
|
|
128
|
-
*/
|
|
129
|
-
getClientVersion() {
|
|
130
|
-
return this.server.server.getClientVersion();
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Sends a ping to the client to check if it is still connected.
|
|
134
|
-
*/
|
|
135
|
-
ping() {
|
|
136
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
137
|
-
return yield this.server.server.ping();
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Creates a LLM message to be sent to the client.
|
|
142
|
-
*/
|
|
143
|
-
createMessage(params, options) {
|
|
144
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
-
return yield this.server.server.createMessage(params, options);
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Elicits input from the client, such as a prompt or resource.
|
|
150
|
-
*/
|
|
151
|
-
elicitInput(params, options) {
|
|
152
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
-
return yield this.server.server.elicitInput(params, options);
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Lists the root resources available to the client.
|
|
158
|
-
*/
|
|
159
|
-
listRoots(params, options) {
|
|
160
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
-
return yield this.server.server.listRoots(params, options);
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Sends a logging message to the client.
|
|
166
|
-
*/
|
|
167
|
-
sendLoggingMessage(params) {
|
|
168
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
return yield this.server.server.sendLoggingMessage(params);
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Sends a resource updated notification to the client.
|
|
174
|
-
*/
|
|
175
|
-
sendResourceUpdated(params) {
|
|
176
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
-
return yield this.server.server.sendResourceUpdated(params);
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Sends a request and wait for a response.
|
|
182
|
-
*
|
|
183
|
-
* Do not use this method to emit notifications! Use notification() instead.
|
|
184
|
-
*/
|
|
185
|
-
request(request, resultSchema, options) {
|
|
186
|
-
return this.server.server.request(request, resultSchema, options);
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Emits a notification, which is a one-way message that does not expect a response.
|
|
190
|
-
*/
|
|
191
|
-
notification(notification, options) {
|
|
192
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
193
|
-
return yield this.server.server.notification(notification, options);
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Registers a handler to invoke when this protocol object receives a request with the given method.
|
|
198
|
-
*
|
|
199
|
-
* Note that this will replace any previous request handler for the same method.
|
|
200
|
-
*/
|
|
201
|
-
setRequestHandler(requestSchema, handler) {
|
|
202
|
-
this.server.server.setRequestHandler(requestSchema, handler);
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Removes the request handler for the given method.
|
|
206
|
-
*/
|
|
207
|
-
removeRequestHandler(method) {
|
|
208
|
-
this.server.server.removeRequestHandler(method);
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Registers a handler to invoke when this protocol object receives a notification with the given method.
|
|
212
|
-
*
|
|
213
|
-
* Note that this will replace any previous notification handler for the same method.
|
|
214
|
-
*/
|
|
215
|
-
setNotificationHandler(notificationSchema, handler) {
|
|
216
|
-
this.server.server.setNotificationHandler(notificationSchema, handler);
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Removes the notification handler for the given method.
|
|
220
|
-
*/
|
|
221
|
-
removeNotificationHandler(method) {
|
|
222
|
-
this.server.server.removeNotificationHandler(method);
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Registers a handler for the subscribe request.
|
|
226
|
-
*/
|
|
227
|
-
onSubscribe(handler) {
|
|
228
|
-
this.server.server.setRequestHandler(SubscribeRequestSchema, handler);
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Registers a handler for the unsubscribe request.
|
|
232
|
-
*/
|
|
233
|
-
onUnsubscribe(handler) {
|
|
234
|
-
this.server.server.setRequestHandler(UnsubscribeRequestSchema, handler);
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Registers a handler for the set log level request.
|
|
238
|
-
*/
|
|
239
|
-
onSetLogLevel(handler) {
|
|
240
|
-
this.server.server.setRequestHandler(SetLevelRequestSchema, handler);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Registers a handler for the list tools request.
|
|
244
|
-
*/
|
|
245
|
-
onListResources(handler) {
|
|
246
|
-
this.server.server.setRequestHandler(ListResourcesRequestSchema, handler);
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Registers a handler for the roots list changed notification.
|
|
250
|
-
*/
|
|
251
|
-
onRootsListChanged(handler) {
|
|
252
|
-
this.server.server.setNotificationHandler(RootsListChangedNotificationSchema, handler);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Close the transport for window.addEventListener('pagehide')
|
|
256
|
-
*/
|
|
257
|
-
onPagehide(event) {
|
|
258
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
259
|
-
if (event.persisted) {
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
if (this.transport && typeof this.transport['close'] === 'function') {
|
|
263
|
-
yield this.transport.close();
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Creates a new MessageChannelServerTransport instance.
|
|
270
|
-
*/
|
|
271
|
-
export const createMessageChannelServerTransport = (endpoint, globalObject) => new MessageChannelServerTransport(endpoint, globalObject);
|
|
272
|
-
/**
|
|
273
|
-
* Creates a pair of transports for communication between a server and client using MessageChannel.
|
|
274
|
-
*/
|
|
275
|
-
export const createMessageChannelPairTransport = () => createTransportPair();
|
|
276
|
-
/**
|
|
277
|
-
* Checks if the transport is a MessageChannelServerTransport.
|
|
278
|
-
*/
|
|
279
|
-
export const isMessageChannelServerTransport = (transport) => transport instanceof MessageChannelServerTransport;
|
|
280
|
-
/**
|
|
281
|
-
* Checks if the server is an instance of MCP Server.
|
|
282
|
-
*/
|
|
283
|
-
export const isMcpServer = (server) => server instanceof McpServer;
|