@opentiny/agent 0.3.0

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.
Files changed (5) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/index.d.ts +542 -0
  4. package/index.js +964 -0
  5. package/package.json +28 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019-present, OpenTiny contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # OpenTiny Agent
2
+
3
+ OpenTiny Agent Server
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @opentiny/agent
9
+ ```
10
+
11
+ ## 许可证
12
+
13
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,542 @@
1
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
3
+ import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
4
+ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
5
+ import { AuthInfo } from '@modelcontextprotocol/sdk/server/auth/types.js';
6
+ import { Response, RequestHandler, Request, NextFunction } from 'express';
7
+ import { JwtPayload } from 'jsonwebtoken';
8
+ import { OAuthClientInformationFull, OAuthTokens, OAuthTokenRevocationRequest } from '@modelcontextprotocol/sdk/shared/auth.js';
9
+ import { AuthorizationParams, OAuthServerProvider } from '@modelcontextprotocol/sdk/server/auth/provider.js';
10
+ import { OAuthRegisteredClientsStore } from '@modelcontextprotocol/sdk/server/auth/clients.js';
11
+ import { AuthRouterOptions } from '@modelcontextprotocol/sdk/server/auth/router.js';
12
+
13
+ declare const ACCESS_TOKEN_EXPIRES_IN: number;
14
+ declare const REFRESH_TOKEN_EXPIRES_IN: number;
15
+ /**
16
+ * 内存中的 OAuth 客户端信息存储实现
17
+ */
18
+ declare class MemoryClientsStore implements OAuthRegisteredClientsStore {
19
+ /**
20
+ * 存储注册的 OAuth 客户端信息
21
+ * key: 客户端 ID
22
+ * value: OAuth 客户端信息
23
+ */
24
+ private clients;
25
+ /**
26
+ * 获取注册的 OAuth 客户端信息。
27
+ *
28
+ * @param clientId 客户端 ID
29
+ * @returns OAuth 客户端信息或 undefined
30
+ */
31
+ getClient(clientId: string): Promise<OAuthClientInformationFull>;
32
+ /**
33
+ * 保存或更新 OAuth 客户端信息。
34
+ *
35
+ * @param clientMetadata OAuth 客户端信息
36
+ * @returns 保存后的 OAuth 客户端信息
37
+ */
38
+ saveClient(clientMetadata: OAuthClientInformationFull): Promise<OAuthClientInformationFull>;
39
+ /**
40
+ * 删除 OAuth 客户端信息。
41
+ *
42
+ * @param clientId 客户端 ID
43
+ * @returns 是否删除成功
44
+ */
45
+ deleteClient(clientId: string): Promise<boolean>;
46
+ /**
47
+ * 注册新的 OAuth 客户端。
48
+ *
49
+ * @param clientMetadata OAuth 客户端信息
50
+ * @returns 保存后的 OAuth 客户端信息
51
+ */
52
+ registerClient(clientMetadata: OAuthClientInformationFull): Promise<OAuthClientInformationFull>;
53
+ }
54
+ /**
55
+ * OAuth 授权码存储接口
56
+ * 用于存储和管理 OAuth 授权码及其相关的授权参数和客户端信息。
57
+ * 该接口定义了获取、保存和删除授权码数据的方法。
58
+ */
59
+ interface OAuthAuthorizationCodeStore {
60
+ /**
61
+ * 获取授权码的相关数据。
62
+ *
63
+ * @param code 授权码
64
+ * @returns 授权码数据或 undefined
65
+ */
66
+ getCodeDate(code: string): Promise<{
67
+ params: AuthorizationParams;
68
+ client: OAuthClientInformationFull;
69
+ } | undefined>;
70
+ /**
71
+ * 保存授权码数据。
72
+ *
73
+ * @param code 授权码
74
+ * @param data 包含授权参数和客户端信息的对象
75
+ * @param data.params 授权参数
76
+ * @param data.client 客户端信息
77
+ * @returns 是否保存成功
78
+ */
79
+ saveCodeData(code: string, data: {
80
+ params: AuthorizationParams;
81
+ client: OAuthClientInformationFull;
82
+ }): Promise<boolean>;
83
+ /**
84
+ * 删除授权码数据。
85
+ *
86
+ * @param code 授权码
87
+ * @returns 是否删除成功
88
+ */
89
+ deleteCodeData(code: string): Promise<boolean>;
90
+ }
91
+ /**
92
+ * 内存中的授权码存储实现
93
+ */
94
+ declare class MemoryAuthorizationCodeStore implements OAuthAuthorizationCodeStore {
95
+ /**
96
+ * 存储授权码的相关数据
97
+ * key: 授权码
98
+ * value: 包含授权参数和客户端信息的对象
99
+ */
100
+ private codes;
101
+ /**
102
+ * 获取授权码的相关数据。
103
+ *
104
+ * @param code 授权码
105
+ * @returns 授权码数据或 undefined
106
+ */
107
+ getCodeDate(code: string): Promise<{
108
+ params: AuthorizationParams;
109
+ client: OAuthClientInformationFull;
110
+ }>;
111
+ /**
112
+ * 保存授权码数据。
113
+ *
114
+ * @param code 授权码
115
+ * @param data 包含授权参数和客户端信息的对象
116
+ * @param data.params 授权参数
117
+ * @param data.client 客户端信息
118
+ * @returns 是否保存成功
119
+ */
120
+ saveCodeData(code: string, data: {
121
+ params: AuthorizationParams;
122
+ client: OAuthClientInformationFull;
123
+ }): Promise<boolean>;
124
+ /**
125
+ * 删除授权码数据。
126
+ *
127
+ * @param code 授权码
128
+ * @returns 是否删除成功
129
+ */
130
+ deleteCodeData(code: string): Promise<boolean>;
131
+ }
132
+ /**
133
+ * OAuth 访问令牌存储接口
134
+ * 用于存储和管理 OAuth 访问令牌及其相关的认证信息。
135
+ * 该接口定义了获取、保存和删除访问令牌的方法。
136
+ */
137
+ interface OAuthAccessTokensStore {
138
+ /**
139
+ * 获取访问令牌的认证信息。
140
+ *
141
+ * @param token 访问令牌
142
+ * @returns 认证信息或 undefined
143
+ */
144
+ getAccessToken(token: string): Promise<AuthInfo | undefined>;
145
+ /**
146
+ * 保存访问令牌的认证信息。
147
+ *
148
+ * @param token 访问令牌
149
+ * @param info 认证信息
150
+ * @returns 是否保存成功
151
+ */
152
+ saveAccessToken(token: string, info: AuthInfo): Promise<boolean>;
153
+ /**
154
+ * 撤销访问令牌。
155
+ *
156
+ * @param token 访问令牌
157
+ * @returns 是否删除成功
158
+ */
159
+ revokeAccessToken(token: string): Promise<boolean>;
160
+ }
161
+ /**
162
+ * 内存中的访问令牌存储实现
163
+ */
164
+ declare class MemoryAccessTokensStore implements OAuthAccessTokensStore {
165
+ /**
166
+ * 存储访问令牌的认证信息
167
+ * key: 访问令牌
168
+ * value: 认证信息对象
169
+ */
170
+ private accessTokens;
171
+ /**
172
+ * 获取访问令牌的认证信息。
173
+ *
174
+ * @param token 访问令牌
175
+ * @returns 认证信息或 undefined
176
+ */
177
+ getAccessToken(token: string): Promise<AuthInfo>;
178
+ /**
179
+ * 保存访问令牌的认证信息。
180
+ *
181
+ * @param token 访问令牌
182
+ * @param info 认证信息
183
+ * @returns 是否保存成功
184
+ */
185
+ saveAccessToken(token: string, info: AuthInfo): Promise<boolean>;
186
+ /**
187
+ * 撤销访问令牌。
188
+ *
189
+ * @param token 访问令牌
190
+ * @returns 是否删除成功
191
+ */
192
+ revokeAccessToken(token: string): Promise<boolean>;
193
+ }
194
+ /**
195
+ * OAuth 刷新令牌存储接口
196
+ * 用于存储和管理 OAuth 刷新令牌及其相关的认证信息。
197
+ * 该接口定义了获取、保存和删除刷新令牌的方法。
198
+ */
199
+ interface OAuthRefreshTokensStore {
200
+ /**
201
+ * 获取刷新令牌的认证信息。
202
+ *
203
+ * @param token 刷新令牌
204
+ * @returns 认证信息或 undefined
205
+ */
206
+ getRefreshToken(token: string): Promise<AuthInfo | undefined>;
207
+ /**
208
+ * 保存刷新令牌的认证信息。
209
+ *
210
+ * @param token 刷新令牌
211
+ * @param info 认证信息
212
+ * @returns 是否保存成功
213
+ */
214
+ saveRefreshToken(token: string, info: AuthInfo): Promise<boolean>;
215
+ /**
216
+ * 撤销刷新令牌。
217
+ *
218
+ * @param token 刷新令牌
219
+ * @returns 是否删除成功
220
+ */
221
+ revokeRefreshToken(token: string): Promise<boolean>;
222
+ }
223
+ /**
224
+ * 内存中的刷新令牌存储实现
225
+ */
226
+ declare class MemoryRefreshTokensStore implements OAuthRefreshTokensStore {
227
+ /**
228
+ * 存储刷新令牌的认证信息
229
+ * key: 刷新令牌
230
+ * value: 认证信息对象
231
+ */
232
+ private refreshTokens;
233
+ /**
234
+ * 获取刷新令牌的认证信息。
235
+ *
236
+ * @param token 刷新令牌
237
+ * @returns 认证信息或 undefined
238
+ */
239
+ getRefreshToken(token: string): Promise<AuthInfo>;
240
+ /**
241
+ * 保存刷新令牌的认证信息。
242
+ *
243
+ * @param token 刷新令牌
244
+ * @param info 认证信息
245
+ * @returns 是否保存成功
246
+ */
247
+ saveRefreshToken(token: string, info: AuthInfo): Promise<boolean>;
248
+ /**
249
+ * 撤销刷新令牌。
250
+ *
251
+ * @param token 刷新令牌
252
+ * @returns 是否删除成功
253
+ */
254
+ revokeRefreshToken(token: string): Promise<boolean>;
255
+ }
256
+ /**
257
+ * 认证服务器提供者选项接口
258
+ * 用于配置认证服务器提供者的选项,包括存储、令牌过期时间等。
259
+ */
260
+ interface AuthServerProviderOptions {
261
+ clientsStore?: OAuthRegisteredClientsStore;
262
+ codesStore?: OAuthAuthorizationCodeStore;
263
+ accessTokensStore?: OAuthAccessTokensStore;
264
+ refreshTokensStore?: OAuthRefreshTokensStore;
265
+ generateAuthorizationCode?: () => string;
266
+ generateAccessToken?: () => string;
267
+ generateRefreshToken?: () => string;
268
+ accessTokenExpiresIn?: number;
269
+ refreshTokenExpiresIn?: number;
270
+ }
271
+ /**
272
+ * 认证服务器提供者
273
+ * 用于管理 OAuth 客户端信息、授权码、访问令牌和刷新令牌的存储和生成。
274
+ */
275
+ declare class AuthServerProvider implements OAuthServerProvider {
276
+ clientsStore: OAuthRegisteredClientsStore;
277
+ private codesStore;
278
+ private accessTokensStore;
279
+ private refreshTokensStore;
280
+ private generateAuthorizationCode;
281
+ private generateAccessToken;
282
+ private generateRefreshToken;
283
+ private accessTokenExpiresIn;
284
+ private refreshTokenExpiresIn;
285
+ constructor(options?: AuthServerProviderOptions);
286
+ /**
287
+ * 授权客户端。
288
+ *
289
+ * @param client 客户端信息
290
+ * @param params 授权参数
291
+ * @param res 响应对象
292
+ */
293
+ authorize(client: OAuthClientInformationFull, params: AuthorizationParams, res: Response): Promise<void>;
294
+ /**
295
+ * 获取授权码的 code challenge。
296
+ *
297
+ * @param client 客户端信息
298
+ * @param authorizationCode 授权码
299
+ * @returns 授权码的 code challenge
300
+ */
301
+ challengeForAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string): Promise<string>;
302
+ /**
303
+ * 交换授权码。
304
+ *
305
+ * @param client 客户端信息
306
+ * @param authorizationCode 授权码
307
+ * @returns 交换后的 OAuth 令牌
308
+ */
309
+ exchangeAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string): Promise<OAuthTokens>;
310
+ /**shared.js
311
+ * 交换刷新令牌。
312
+ *
313
+ * @param client 客户端信息
314
+ * @param refreshToken 刷新令牌
315
+ * @returns 交换后的 OAuth 令牌
316
+ */
317
+ exchangeRefreshToken(client: OAuthClientInformationFull, refreshToken: string): Promise<OAuthTokens>;
318
+ /**
319
+ * 验证访问令牌。
320
+ *
321
+ * @param token 访问令牌
322
+ * @returns 认证信息
323
+ */
324
+ verifyAccessToken(token: string): Promise<AuthInfo>;
325
+ /**
326
+ * 撤销访问令牌。
327
+ *
328
+ * @param client 客户端信息
329
+ * @param request 撤销令牌请求
330
+ */
331
+ revokeToken(client: OAuthClientInformationFull, request: OAuthTokenRevocationRequest): Promise<void>;
332
+ }
333
+ /**
334
+ * 创建 OAuth 认证中间件。
335
+ *
336
+ * @param authServerProvider 认证服务器提供者
337
+ * @param resourceUrl 资源标识符
338
+ * @returns Express 中间件函数
339
+ */
340
+ declare const createAuthMiddleware: (authServerProvider: AuthServerProvider, resourceUrl: string) => any;
341
+ /**
342
+ * 创建 OAuth 认证回调路由,用于处理 OAuth 授权回调请求,存储授权码,并提供获取授权码的接口。
343
+ *
344
+ * @param callbackEndpoint 回调端点路径
345
+ * @param middlewares 中间件,可选
346
+ * @returns Express 路由处理器
347
+ */
348
+ declare const createAuthCallbackRouter: (callbackEndpoint: string, ...middlewares: RequestHandler[]) => RequestHandler;
349
+ /**
350
+ * 创建 OAuth 认证元数据路由。
351
+ *
352
+ * @param authServerProvider 认证服务器提供者
353
+ * @param authUrl 认证 URL
354
+ * @param resourceUrl 资源 URL
355
+ * @returns Express 路由处理器
356
+ */
357
+ declare const createAuthMetadataRouter: (authServerProvider: AuthServerProvider, authUrl: string, resourceUrl: string) => RequestHandler;
358
+ /**
359
+ * 创建 OAuth 认证路由。
360
+ *
361
+ * @param options 认证路由选项
362
+ * @returns Express 路由处理器
363
+ */
364
+ declare const createAuthRouter: (options: AuthRouterOptions) => RequestHandler;
365
+
366
+ /**
367
+ * 客户端连接的信息,包括客户端实例、用户信息和 Transport 类型。
368
+ */
369
+ interface ClientInfo {
370
+ /**
371
+ * 客户端实例
372
+ */
373
+ client: Client;
374
+ /**
375
+ * 客户端连接的 Transport 实例
376
+ */
377
+ transport: SSEServerTransport | StreamableHTTPServerTransport;
378
+ /**
379
+ * 连接的用户信息
380
+ */
381
+ user?: AuthInfo | JwtPayload | string;
382
+ /**
383
+ * 用于识别不同连接的信息
384
+ */
385
+ device: {
386
+ /**
387
+ * 连接的来源 IP 地址,可能是 IPv4 或 IPv6 地址
388
+ */
389
+ ip: string | string[];
390
+ /**
391
+ * 识别浏览器类型和版本 User-Agent 字符串
392
+ */
393
+ userAgent: string;
394
+ /**
395
+ * 连接的语言设置,通常是浏览器的 Accept-Language 字段
396
+ */
397
+ acceptLanguage: string;
398
+ /**
399
+ * 连接的来源 URL,通常是 HTTP Referer 字段
400
+ */
401
+ referer: string;
402
+ };
403
+ /**
404
+ * 客户端连接的 Transport 类型
405
+ */
406
+ type: 'SSE' | 'StreamableHTTP';
407
+ }
408
+ /**
409
+ * 遥控端连接的信息,包括服务端实例、用户信息和 Transport 类型。
410
+ */
411
+ interface RemoterInfo {
412
+ /**
413
+ * 服务端实例
414
+ */
415
+ mcpServer: McpServer;
416
+ /**
417
+ * 服务端连接的 Transport 实例
418
+ */
419
+ transport: SSEServerTransport | StreamableHTTPServerTransport;
420
+ /**
421
+ * 所连接客户端的 Session ID
422
+ */
423
+ client: string;
424
+ /**
425
+ * 连接的用户信息
426
+ */
427
+ user?: AuthInfo | JwtPayload | string;
428
+ /**
429
+ * 用于识别不同连接的信息
430
+ */
431
+ device: {
432
+ /**
433
+ * 连接的来源 IP 地址,可能是 IPv4 或 IPv6 地址
434
+ */
435
+ ip: string | string[];
436
+ /**
437
+ * 识别浏览器类型和版本 User-Agent 字符串
438
+ */
439
+ userAgent: string;
440
+ /**
441
+ * 连接的语言设置,通常是浏览器的 Accept-Language 字段
442
+ */
443
+ acceptLanguage: string;
444
+ /**
445
+ * 连接的来源 URL,通常是 HTTP Referer 字段
446
+ */
447
+ referer: string;
448
+ };
449
+ /**
450
+ * 服务端连接的 Transport 类型
451
+ */
452
+ type: 'SSE' | 'StreamableHTTP';
453
+ }
454
+ /**
455
+ * ClientError 接口,在客户端连接或处理过程中发生的错误。
456
+ */
457
+ interface ClientError {
458
+ /**
459
+ * 客户端实例
460
+ */
461
+ client: Client;
462
+ /**
463
+ * 客户端连接的 Transport 实例
464
+ */
465
+ transport: SSEServerTransport | StreamableHTTPServerTransport;
466
+ /**
467
+ * 错误信息
468
+ */
469
+ error: Error;
470
+ /**
471
+ * 服务端连接的 Transport 类型
472
+ */
473
+ type: 'SSE' | 'StreamableHTTP';
474
+ }
475
+ /**
476
+ * RemoterError 接口,在遥控端连接或处理过程中发生的错误。
477
+ */
478
+ interface RemoterError {
479
+ /**
480
+ * 服务端实例
481
+ */
482
+ mcpServer: McpServer;
483
+ /**
484
+ * 客户端连接的 Transport 实例
485
+ */
486
+ transport: SSEServerTransport | StreamableHTTPServerTransport;
487
+ /**
488
+ * 所连接客户端的 Session ID
489
+ */
490
+ client: string;
491
+ /**
492
+ * 错误信息
493
+ */
494
+ error: Error;
495
+ /**
496
+ * 服务端连接的 Transport 类型
497
+ */
498
+ type: 'SSE' | 'StreamableHTTP';
499
+ }
500
+ /**
501
+ * Express 请求对象的扩展,添加了可选的用户信息属性
502
+ */
503
+ declare module 'express' {
504
+ interface Request {
505
+ user?: JwtPayload | string;
506
+ auth?: AuthInfo;
507
+ }
508
+ }
509
+ /**
510
+ * 认证中间件,用于校验 JWT Token。
511
+ *
512
+ * 检查请求头中的 Authorization 字段,提取并校验 Bearer Token。
513
+ * 校验通过后,将解码后的用户信息挂载到 req.user
514
+ * 校验失败或缺失 token 时,返回 401 或 403 错误。
515
+ */
516
+ declare const auth: ({ secret }: {
517
+ secret: string;
518
+ }) => (req: Request, res: Response, next: NextFunction) => void;
519
+ /**
520
+ * SSE 与 Streamable HTTP 连接的处理函数,同时返回所有客户端代理实例。
521
+ */
522
+ declare const useProxyHandles: () => {
523
+ handleSseProxy: (req: Request, res: Response, endpoint: string) => Promise<void>;
524
+ handleSseInspector: (req: Request, res: Response, endpoint: string) => Promise<void>;
525
+ handleSseMessage: (req: Request, res: Response) => Promise<void>;
526
+ handleStreamRequest: (req: Request, res: Response) => Promise<void>;
527
+ handleStreamInspector: (req: Request, res: Response) => Promise<void>;
528
+ clients: Record<string, ClientInfo>;
529
+ remoters: Record<string, RemoterInfo>;
530
+ transports: Record<string, any>;
531
+ inspectors: Record<string, any>;
532
+ reset: () => {};
533
+ ping: () => Promise<{
534
+ clientSessions: string[];
535
+ remoterSessions: string[];
536
+ }>;
537
+ onClientError: (onError: (error: ClientError) => void) => void;
538
+ onRemoterError: (onError: (error: RemoterError) => void) => void;
539
+ };
540
+
541
+ export { ACCESS_TOKEN_EXPIRES_IN, AuthServerProvider, MemoryAccessTokensStore, MemoryAuthorizationCodeStore, MemoryClientsStore, MemoryRefreshTokensStore, REFRESH_TOKEN_EXPIRES_IN, auth, createAuthCallbackRouter, createAuthMetadataRouter, createAuthMiddleware, createAuthRouter, useProxyHandles };
542
+ export type { AuthServerProviderOptions, ClientError, ClientInfo, OAuthAccessTokensStore, OAuthAuthorizationCodeStore, OAuthRefreshTokensStore, RemoterError, RemoterInfo };