@opentiny/agent 0.3.3 → 0.3.4
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/LICENSE +0 -0
- package/README.md +97 -3
- package/index.d.ts +14 -3
- package/index.js +839 -827
- package/package.json +2 -2
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -1,13 +1,107 @@
|
|
|
1
|
-
# OpenTiny
|
|
1
|
+
# OpenTiny NEXT
|
|
2
2
|
|
|
3
|
-
OpenTiny
|
|
3
|
+
OpenTiny NEXT 是一个基于 Model Context Protocol(MCP)的 TypeScript 库,提供了多种传输方式来支持 MCP 客户端与服务端的通信。本库支持三种主要的传输方式:
|
|
4
|
+
|
|
5
|
+
1. MessageChannel API - 用于浏览器内部不同上下文之间的通信
|
|
6
|
+
2. SSE (Server-Sent Events) Client Proxy - 基于 HTTP 长连接实现单向数据推送的 Client 连接代理
|
|
7
|
+
3. Streamable HTTP Client Proxy - 通过分块传输编码实现任意数据的流式传输的 Client 连接代理
|
|
4
8
|
|
|
5
9
|
## 安装
|
|
6
10
|
|
|
7
11
|
```bash
|
|
8
|
-
npm install @opentiny/
|
|
12
|
+
npm install @opentiny/next
|
|
9
13
|
```
|
|
10
14
|
|
|
15
|
+
## 客户端 API (client.js)
|
|
16
|
+
|
|
17
|
+
客户端 API 主要用于在浏览器环境中的 MCP 通信。
|
|
18
|
+
|
|
19
|
+
### MessageChannel API
|
|
20
|
+
|
|
21
|
+
#### 在同一浏览器窗口内互相通信的场景
|
|
22
|
+
|
|
23
|
+
使用 `createTransportPair` 创建一对可互通的 Transport 服务的和客户端实例来进行通信。
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
27
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
28
|
+
import { createTransportPair } from '@opentiny/next';
|
|
29
|
+
|
|
30
|
+
// 创建一对可互通的 Transport 实例
|
|
31
|
+
const [serverTransport, clientTransport] = createTransportPair();
|
|
32
|
+
|
|
33
|
+
// 创建 MCP 服务端和客户端实例
|
|
34
|
+
const serverCapabilities = {
|
|
35
|
+
prompts: { listChanged: true },
|
|
36
|
+
resources: { subscribe: true, listChanged: true },
|
|
37
|
+
tools: { listChanged: true },
|
|
38
|
+
completions: {},
|
|
39
|
+
logging: {}
|
|
40
|
+
};
|
|
41
|
+
const clientCapabilities = { roots: { listChanged: true }, sampling: {}, elicitation: {} };
|
|
42
|
+
const server = new McpServer({ name: 'mcp-server', version: '1.0.0' }, { capabilities: serverCapabilities });
|
|
43
|
+
const client = new Client({ name: 'mcp-client', version: '1.0.0' }, { capabilities: clientCapabilities });
|
|
44
|
+
|
|
45
|
+
// 建立服务端和客户端的通信连接
|
|
46
|
+
await server.connect(serverTransport);
|
|
47
|
+
await client.connect(clientTransport);
|
|
48
|
+
|
|
49
|
+
// 将客户端实例存储到状态中
|
|
50
|
+
state.client = client;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### 在浏览器主线程与iframe、Web Worker等互相通信的场景
|
|
54
|
+
|
|
55
|
+
使用 `MessageChannelServerTransport` 和 `MessageChannelClientTransport` 创建用于监听的 Transport 服务端实例,以及用于连接的 Transport 客户端实例来进行通信。
|
|
56
|
+
|
|
57
|
+
以下是在浏览器主线程的代码:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
61
|
+
import { MessageChannelServerTransport } from '@opentiny/next';
|
|
62
|
+
|
|
63
|
+
// 创建用于监听的 Transport 服务端实例
|
|
64
|
+
const serverTransport = new MessageChannelServerTransport('endpoint');
|
|
65
|
+
|
|
66
|
+
// 创建 MCP 服务端实例
|
|
67
|
+
const capabilities = {
|
|
68
|
+
prompts: { listChanged: true },
|
|
69
|
+
resources: { subscribe: true, listChanged: true },
|
|
70
|
+
tools: { listChanged: true },
|
|
71
|
+
completions: {},
|
|
72
|
+
logging: {}
|
|
73
|
+
};
|
|
74
|
+
const server = new McpServer({ name: 'mcp-server', version: '1.0.0' }, { capabilities });
|
|
75
|
+
|
|
76
|
+
// 监听 endpoint 端点,等待客户端连接
|
|
77
|
+
await serverTransport.listen();
|
|
78
|
+
|
|
79
|
+
// 建立服务端和客户端的通信连接
|
|
80
|
+
await server.connect(serverTransport);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
以下是在 iframe、Web Worker 的代码:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
87
|
+
import { MessageChannelClientTransport } from '@opentiny/next';
|
|
88
|
+
|
|
89
|
+
// 创建用于连接的 Transport 客户端实例
|
|
90
|
+
const clientTransport = new MessageChannelClientTransport('endpoint');
|
|
91
|
+
|
|
92
|
+
// 创建 MCP 客户端实例
|
|
93
|
+
const capabilities = { roots: { listChanged: true }, sampling: {}, elicitation: {} };
|
|
94
|
+
const client = new Client({ name: 'mcp-client', version: '1.0.0' }, { capabilities });
|
|
95
|
+
|
|
96
|
+
// 建立服务端和客户端的通信连接
|
|
97
|
+
await client.connect(clientTransport);
|
|
98
|
+
|
|
99
|
+
// 将客户端实例存储到状态中
|
|
100
|
+
state.client = client;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
请注意:创建 `MessageChannelServerTransport` 实例必须在创建 `MessageChannelClientTransport` 实例之前,确保客户端连接之前服务端已经开始监听。由于 iframe、Web Worker 等代码运行通常在浏览器主线程之后,所以上述示例代码执行顺序一般是先创建 `MessageChannelServerTransport` 实例,后创建 `MessageChannelClientTransport` 实例。
|
|
104
|
+
|
|
11
105
|
## 许可证
|
|
12
106
|
|
|
13
107
|
MIT
|
package/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { OAuthClientInformationFull, OAuthTokens, OAuthTokenRevocationRequest }
|
|
|
12
12
|
import { AuthorizationParams, OAuthServerProvider } from '@modelcontextprotocol/sdk/server/auth/provider.js';
|
|
13
13
|
import { OAuthRegisteredClientsStore } from '@modelcontextprotocol/sdk/server/auth/clients.js';
|
|
14
14
|
import { AuthRouterOptions } from '@modelcontextprotocol/sdk/server/auth/router.js';
|
|
15
|
+
import { CorsOptions } from 'cors';
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Server transport for WebSocket: this will listen to clients over the WebSocket protocol.
|
|
@@ -367,12 +368,13 @@ declare const createAuthCallbackRouter: (callbackEndpoint: string, ...middleware
|
|
|
367
368
|
/**
|
|
368
369
|
* 创建 OAuth 认证元数据路由。
|
|
369
370
|
*
|
|
370
|
-
* @param
|
|
371
|
+
* @param authProvider 认证服务器提供者
|
|
371
372
|
* @param authUrl 认证 URL
|
|
372
373
|
* @param resourceUrl 资源 URL
|
|
374
|
+
* @param issuerUrl 签发者 URL,可选
|
|
373
375
|
* @returns Express 路由处理器
|
|
374
376
|
*/
|
|
375
|
-
declare const createAuthMetadataRouter: (
|
|
377
|
+
declare const createAuthMetadataRouter: (authProvider: AuthServerProvider, authUrl: string, resourceUrl: string, issuerUrl?: string) => RequestHandler;
|
|
376
378
|
/**
|
|
377
379
|
* 创建 OAuth 认证路由。
|
|
378
380
|
*
|
|
@@ -380,6 +382,15 @@ declare const createAuthMetadataRouter: (authServerProvider: AuthServerProvider,
|
|
|
380
382
|
* @returns Express 路由处理器
|
|
381
383
|
*/
|
|
382
384
|
declare const createAuthRouter: (options: AuthRouterOptions) => RequestHandler;
|
|
385
|
+
/**
|
|
386
|
+
* 创建锁定 CORS 头中间件
|
|
387
|
+
* 配置的 CORS 规则,并锁定 Access-Control-Allow-Origin,
|
|
388
|
+
* 防止后续中间件(如第三方库)修改它。
|
|
389
|
+
*
|
|
390
|
+
* @param options CORS 选项
|
|
391
|
+
* @returns Express 中间件函数
|
|
392
|
+
*/
|
|
393
|
+
declare const lockCors: (options: CorsOptions) => (req: Request, res: Response, next: NextFunction) => void;
|
|
383
394
|
|
|
384
395
|
/**
|
|
385
396
|
* 客户端连接的信息,包括客户端实例、用户信息和 Transport 类型。
|
|
@@ -565,5 +576,5 @@ declare const useProxyHandles: () => {
|
|
|
565
576
|
onRemoterError: (onError: (error: RemoterError) => void) => void;
|
|
566
577
|
};
|
|
567
578
|
|
|
568
|
-
export { ACCESS_TOKEN_EXPIRES_IN, AuthServerProvider, MemoryAccessTokensStore, MemoryAuthorizationCodeStore, MemoryClientsStore, MemoryRefreshTokensStore, REFRESH_TOKEN_EXPIRES_IN, WebSocketServerTransport, auth, createAuthCallbackRouter, createAuthMetadataRouter, createAuthMiddleware, createAuthRouter, jwtAuth, useProxyHandles };
|
|
579
|
+
export { ACCESS_TOKEN_EXPIRES_IN, AuthServerProvider, MemoryAccessTokensStore, MemoryAuthorizationCodeStore, MemoryClientsStore, MemoryRefreshTokensStore, REFRESH_TOKEN_EXPIRES_IN, WebSocketServerTransport, auth, createAuthCallbackRouter, createAuthMetadataRouter, createAuthMiddleware, createAuthRouter, jwtAuth, lockCors, useProxyHandles };
|
|
569
580
|
export type { AuthServerProviderOptions, ClientError, ClientInfo, OAuthAccessTokensStore, OAuthAuthorizationCodeStore, OAuthRefreshTokensStore, RemoterError, RemoterInfo };
|