@n0ts123/mcplink-core 0.0.2 → 0.0.3
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/README.md +167 -238
- package/dist/index.d.ts +100 -53
- package/dist/index.js +389 -68
- package/dist/index.js.map +1 -1
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -3,17 +3,22 @@
|
|
|
3
3
|
MCPLink 核心 SDK - AI Agent 工具调用框架,让 AI 轻松调用 MCP 工具。
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@n0ts123/mcplink-core)
|
|
6
|
-
[](https://github.com/
|
|
6
|
+
[](https://github.com/n0tssss/MCPLink/blob/master/LICENSE)
|
|
7
|
+
[](https://github.com/n0tssss/MCPLink)
|
|
7
8
|
|
|
8
|
-
## 特性
|
|
9
|
+
## ✨ 特性
|
|
9
10
|
|
|
10
11
|
- 🚀 **简单易用** - 几行代码即可让 AI 调用 MCP 工具
|
|
11
12
|
- 🔄 **流式响应** - 支持实时流式输出,体验更流畅
|
|
12
|
-
- 🤖 **多模型支持** - OpenAI、Claude、Gemini、DeepSeek、Qwen 等
|
|
13
|
-
- 🛠️ **MCP 协议** -
|
|
14
|
-
-
|
|
13
|
+
- 🤖 **多模型支持** - OpenAI GPT、Claude、Gemini、DeepSeek、Qwen、Llama 等
|
|
14
|
+
- 🛠️ **MCP 协议** - 支持 stdio、SSE、Streamable HTTP 三种连接方式
|
|
15
|
+
- ⚡ **并行调用** - 支持同时执行多个独立的工具调用
|
|
16
|
+
- 💭 **思考过程** - 展示 AI 推理过程,支持 `<think>` 标签和原生 reasoning
|
|
17
|
+
- 🎯 **即时结果** - 工具返回特定格式时立即推送(如卡片消息)
|
|
18
|
+
- 🔀 **智能路由** - 根据模型自动选择原生或 Prompt-Based 模式
|
|
19
|
+
- 📦 **TypeScript** - 完整的类型支持
|
|
15
20
|
|
|
16
|
-
## 安装
|
|
21
|
+
## 📦 安装
|
|
17
22
|
|
|
18
23
|
```bash
|
|
19
24
|
# npm
|
|
@@ -26,70 +31,46 @@ pnpm add @n0ts123/mcplink-core
|
|
|
26
31
|
yarn add @n0ts123/mcplink-core
|
|
27
32
|
```
|
|
28
33
|
|
|
29
|
-
|
|
34
|
+
> 💡 **内置 AI SDK**:本包已内置 `@ai-sdk/openai` 和 `@ai-sdk/anthropic`,无需额外安装即可直接使用。
|
|
35
|
+
>
|
|
36
|
+
> 如需使用 Google Gemini,需额外安装:`npm install @ai-sdk/google`
|
|
30
37
|
|
|
31
|
-
|
|
32
|
-
# OpenAI (GPT-4, GPT-3.5)
|
|
33
|
-
npm install @ai-sdk/openai
|
|
34
|
-
|
|
35
|
-
# Google (Gemini)
|
|
36
|
-
npm install @ai-sdk/google
|
|
37
|
-
|
|
38
|
-
# Anthropic (Claude)
|
|
39
|
-
npm install @ai-sdk/anthropic
|
|
40
|
-
|
|
41
|
-
# 兼容 OpenAI 格式的模型 (DeepSeek, Qwen, 等)
|
|
42
|
-
# 使用 @ai-sdk/openai 即可
|
|
43
|
-
```
|
|
38
|
+
## 🚀 快速开始
|
|
44
39
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
### TypeScript 示例
|
|
40
|
+
### TypeScript / JavaScript (ESM)
|
|
48
41
|
|
|
49
42
|
```typescript
|
|
50
|
-
import { MCPLink } from '@n0ts123/mcplink-core'
|
|
51
|
-
import { createOpenAI } from '@ai-sdk/openai'
|
|
43
|
+
import { MCPLink, createOpenAI } from '@n0ts123/mcplink-core'
|
|
52
44
|
|
|
53
45
|
// 创建模型
|
|
54
|
-
const openai = createOpenAI({
|
|
55
|
-
|
|
56
|
-
//
|
|
57
|
-
const agent = new MCPLink({
|
|
58
|
-
model: openai('gpt-4o'),
|
|
59
|
-
mcpServers: {
|
|
60
|
-
myTools: {
|
|
61
|
-
type: 'stdio',
|
|
62
|
-
command: 'node',
|
|
63
|
-
args: ['./my-mcp-server.js'],
|
|
64
|
-
},
|
|
65
|
-
},
|
|
46
|
+
const openai = createOpenAI({
|
|
47
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
48
|
+
baseURL: 'https://api.openai.com/v1', // 可选
|
|
66
49
|
})
|
|
67
50
|
|
|
68
|
-
// 初始化并对话
|
|
69
|
-
await agent.initialize()
|
|
70
|
-
const result = await agent.chat('你好')
|
|
71
|
-
console.log(result.content)
|
|
72
|
-
await agent.close()
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### JavaScript 示例 (ESM)
|
|
76
|
-
|
|
77
|
-
```javascript
|
|
78
|
-
import { MCPLink } from '@n0ts123/mcplink-core'
|
|
79
|
-
import { createOpenAI } from '@ai-sdk/openai'
|
|
80
|
-
|
|
81
|
-
// 创建模型
|
|
82
|
-
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY })
|
|
83
|
-
|
|
84
51
|
// 创建 Agent
|
|
85
52
|
const agent = new MCPLink({
|
|
86
53
|
model: openai('gpt-4o'),
|
|
54
|
+
systemPrompt: '你是一个智能助手',
|
|
55
|
+
maxIterations: 10,
|
|
56
|
+
parallelToolCalls: true, // 启用并行工具调用
|
|
87
57
|
mcpServers: {
|
|
58
|
+
// stdio 模式
|
|
88
59
|
myTools: {
|
|
89
60
|
type: 'stdio',
|
|
90
61
|
command: 'node',
|
|
91
62
|
args: ['./my-mcp-server.js'],
|
|
92
63
|
},
|
|
64
|
+
// SSE 模式
|
|
65
|
+
remote: {
|
|
66
|
+
type: 'sse',
|
|
67
|
+
url: 'http://localhost:8080/mcp',
|
|
68
|
+
},
|
|
69
|
+
// Streamable HTTP 模式
|
|
70
|
+
streamable: {
|
|
71
|
+
type: 'streamable-http',
|
|
72
|
+
url: 'http://localhost:8080/mcp/stream',
|
|
73
|
+
},
|
|
93
74
|
},
|
|
94
75
|
})
|
|
95
76
|
|
|
@@ -100,14 +81,13 @@ console.log(result.content)
|
|
|
100
81
|
await agent.close()
|
|
101
82
|
```
|
|
102
83
|
|
|
103
|
-
### JavaScript
|
|
84
|
+
### JavaScript (CommonJS)
|
|
104
85
|
|
|
105
86
|
> ⚠️ 注意:本包是 ES Module,在 CommonJS 环境中需要使用动态 import
|
|
106
87
|
|
|
107
88
|
```javascript
|
|
108
89
|
async function main() {
|
|
109
|
-
const { MCPLink } = await import('@n0ts123/mcplink-core')
|
|
110
|
-
const { createOpenAI } = await import('@ai-sdk/openai')
|
|
90
|
+
const { MCPLink, createOpenAI } = await import('@n0ts123/mcplink-core')
|
|
111
91
|
|
|
112
92
|
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY })
|
|
113
93
|
|
|
@@ -134,20 +114,23 @@ main()
|
|
|
134
114
|
### 流式响应
|
|
135
115
|
|
|
136
116
|
```typescript
|
|
137
|
-
import { MCPLink, MCPLinkEventType } from '@n0ts123/mcplink-core'
|
|
117
|
+
import { MCPLink, MCPLinkEventType, createOpenAI } from '@n0ts123/mcplink-core'
|
|
118
|
+
|
|
119
|
+
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY })
|
|
138
120
|
|
|
139
121
|
const agent = new MCPLink({
|
|
140
122
|
model: openai('gpt-4o'),
|
|
141
|
-
systemPrompt: '你是一个智能助手',
|
|
142
|
-
maxIterations: 10,
|
|
143
123
|
mcpServers: { /* ... */ },
|
|
144
124
|
})
|
|
145
125
|
|
|
146
126
|
await agent.initialize()
|
|
147
127
|
|
|
148
|
-
// 流式处理
|
|
149
128
|
for await (const event of agent.chatStream('帮我查询订单')) {
|
|
150
129
|
switch (event.type) {
|
|
130
|
+
case MCPLinkEventType.ITERATION_START:
|
|
131
|
+
console.log(`📍 开始第 ${event.data.iteration} 轮迭代`)
|
|
132
|
+
break
|
|
133
|
+
|
|
151
134
|
case MCPLinkEventType.THINKING_START:
|
|
152
135
|
console.log('💭 思考中...')
|
|
153
136
|
break
|
|
@@ -156,10 +139,6 @@ for await (const event of agent.chatStream('帮我查询订单')) {
|
|
|
156
139
|
process.stdout.write(event.data.content || '')
|
|
157
140
|
break
|
|
158
141
|
|
|
159
|
-
case MCPLinkEventType.THINKING_END:
|
|
160
|
-
console.log('\n')
|
|
161
|
-
break
|
|
162
|
-
|
|
163
142
|
case MCPLinkEventType.TOOL_CALL_START:
|
|
164
143
|
console.log(`🔧 调用工具: ${event.data.toolName}`)
|
|
165
144
|
console.log(` 参数: ${JSON.stringify(event.data.toolArgs)}`)
|
|
@@ -169,21 +148,18 @@ for await (const event of agent.chatStream('帮我查询订单')) {
|
|
|
169
148
|
const status = event.data.isError ? '❌' : '✅'
|
|
170
149
|
console.log(`${status} 结果 (${event.data.duration}ms)`)
|
|
171
150
|
break
|
|
172
|
-
|
|
173
|
-
case MCPLinkEventType.
|
|
174
|
-
|
|
151
|
+
|
|
152
|
+
case MCPLinkEventType.IMMEDIATE_RESULT:
|
|
153
|
+
// 即时结果,可用于渲染特殊 UI
|
|
154
|
+
console.log('🎯 即时结果:', event.data.immediateResult)
|
|
175
155
|
break
|
|
176
156
|
|
|
177
157
|
case MCPLinkEventType.TEXT_DELTA:
|
|
178
158
|
process.stdout.write(event.data.content || '')
|
|
179
159
|
break
|
|
180
160
|
|
|
181
|
-
case MCPLinkEventType.TEXT_END:
|
|
182
|
-
console.log('\n')
|
|
183
|
-
break
|
|
184
|
-
|
|
185
161
|
case MCPLinkEventType.COMPLETE:
|
|
186
|
-
console.log(
|
|
162
|
+
console.log(`\n✅ 完成! 耗时: ${event.data.totalDuration}ms, 迭代: ${event.data.totalIterations}`)
|
|
187
163
|
break
|
|
188
164
|
|
|
189
165
|
case MCPLinkEventType.ERROR:
|
|
@@ -193,50 +169,47 @@ for await (const event of agent.chatStream('帮我查询订单')) {
|
|
|
193
169
|
}
|
|
194
170
|
```
|
|
195
171
|
|
|
196
|
-
## 配置选项
|
|
172
|
+
## ⚙️ 配置选项
|
|
197
173
|
|
|
198
174
|
### MCPLinkConfig
|
|
199
175
|
|
|
200
176
|
```typescript
|
|
201
177
|
interface MCPLinkConfig {
|
|
202
|
-
/**
|
|
203
|
-
* AI 模型实例(必填)
|
|
204
|
-
* 使用 Vercel AI SDK 创建的模型
|
|
205
|
-
*/
|
|
178
|
+
/** AI 模型实例(必填)*/
|
|
206
179
|
model: LanguageModel
|
|
207
180
|
|
|
208
|
-
/**
|
|
209
|
-
* 模型名称
|
|
210
|
-
* 用于自动检测是否支持原生 function calling
|
|
211
|
-
* 如果不提供,会尝试从 model.modelId 获取
|
|
212
|
-
*/
|
|
181
|
+
/** 模型名称,用于自动检测是否支持原生 function calling */
|
|
213
182
|
modelName?: string
|
|
214
183
|
|
|
215
|
-
/**
|
|
216
|
-
* 系统提示词
|
|
217
|
-
* 定义 AI 的角色和行为
|
|
218
|
-
*/
|
|
184
|
+
/** 系统提示词 */
|
|
219
185
|
systemPrompt?: string
|
|
220
186
|
|
|
221
|
-
/**
|
|
222
|
-
* 最大迭代次数
|
|
223
|
-
* 防止无限循环,默认 10
|
|
224
|
-
*/
|
|
187
|
+
/** 最大迭代次数(默认 10)*/
|
|
225
188
|
maxIterations?: number
|
|
226
189
|
|
|
227
|
-
/**
|
|
228
|
-
* MCP 服务器配置
|
|
229
|
-
* key 是服务器 ID,value 是服务器配置
|
|
230
|
-
*/
|
|
190
|
+
/** MCP 服务器配置 */
|
|
231
191
|
mcpServers?: Record<string, MCPServerConfig>
|
|
232
192
|
|
|
233
|
-
/**
|
|
193
|
+
/** 是否并行执行工具调用(默认 true)*/
|
|
194
|
+
parallelToolCalls?: boolean
|
|
195
|
+
|
|
196
|
+
/**
|
|
234
197
|
* 是否强制使用 Prompt-Based 模式
|
|
235
198
|
* - true: 强制使用 PromptBasedAgent
|
|
236
199
|
* - false: 强制使用原生 Agent
|
|
237
|
-
* - 'auto'
|
|
200
|
+
* - 'auto': 自动检测(默认)
|
|
238
201
|
*/
|
|
239
202
|
usePromptBasedTools?: boolean | 'auto'
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 是否启用思考阶段(默认 true)
|
|
206
|
+
* 启用后每次迭代会先让 AI 思考分析,再执行工具调用
|
|
207
|
+
* 优点:Chain-of-Thought 效应,提高复杂任务准确性
|
|
208
|
+
*/
|
|
209
|
+
enableThinkingPhase?: boolean
|
|
210
|
+
|
|
211
|
+
/** 即时结果匹配器,匹配时触发 IMMEDIATE_RESULT 事件 */
|
|
212
|
+
immediateResultMatchers?: Array<Record<string, unknown>>
|
|
240
213
|
}
|
|
241
214
|
```
|
|
242
215
|
|
|
@@ -245,96 +218,94 @@ interface MCPLinkConfig {
|
|
|
245
218
|
```typescript
|
|
246
219
|
// Stdio 模式(本地进程)
|
|
247
220
|
interface MCPServerConfigStdio {
|
|
248
|
-
type
|
|
249
|
-
command: string
|
|
250
|
-
args?: string[]
|
|
251
|
-
env?: Record<string, string>
|
|
221
|
+
type?: 'stdio'
|
|
222
|
+
command: string
|
|
223
|
+
args?: string[]
|
|
224
|
+
env?: Record<string, string>
|
|
252
225
|
}
|
|
253
226
|
|
|
254
227
|
// SSE 模式(远程服务)
|
|
255
228
|
interface MCPServerConfigSSE {
|
|
256
229
|
type: 'sse'
|
|
257
|
-
url: string
|
|
258
|
-
headers?: Record<string, string>
|
|
230
|
+
url: string
|
|
231
|
+
headers?: Record<string, string>
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Streamable HTTP 模式
|
|
235
|
+
interface MCPServerConfigStreamableHTTP {
|
|
236
|
+
type: 'streamable-http'
|
|
237
|
+
url: string
|
|
238
|
+
headers?: Record<string, string>
|
|
259
239
|
}
|
|
260
240
|
```
|
|
261
241
|
|
|
262
|
-
## 多模型支持
|
|
242
|
+
## 🤖 多模型支持
|
|
243
|
+
|
|
244
|
+
MCPLink 会根据模型自动选择最佳的调用方式:
|
|
245
|
+
|
|
246
|
+
| 模型 | 模式 | 说明 |
|
|
247
|
+
|------|------|------|
|
|
248
|
+
| GPT-4o, GPT-4, GPT-3.5 | 原生 | 使用 function calling |
|
|
249
|
+
| Claude-3, Claude-3.5 | 原生 | 使用 function calling |
|
|
250
|
+
| Gemini Flash/Pro | 原生 | 使用 function calling |
|
|
251
|
+
| Mistral, Mixtral | 原生 | 使用 function calling |
|
|
252
|
+
| DeepSeek | Prompt-Based | 使用 prompt 引导 |
|
|
253
|
+
| Qwen, 通义千问 | Prompt-Based | 使用 prompt 引导 |
|
|
254
|
+
| Llama, Yi, GLM | Prompt-Based | 使用 prompt 引导 |
|
|
263
255
|
|
|
264
256
|
### OpenAI
|
|
265
257
|
|
|
266
258
|
```typescript
|
|
267
|
-
import { createOpenAI } from '@
|
|
259
|
+
import { MCPLink, createOpenAI } from '@n0ts123/mcplink-core'
|
|
268
260
|
|
|
269
|
-
const openai = createOpenAI({
|
|
270
|
-
|
|
271
|
-
})
|
|
272
|
-
|
|
273
|
-
const agent = new MCPLink({
|
|
274
|
-
model: openai('gpt-4o'), // 或 gpt-4o-mini, gpt-3.5-turbo
|
|
275
|
-
})
|
|
261
|
+
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY })
|
|
262
|
+
const agent = new MCPLink({ model: openai('gpt-4o') })
|
|
276
263
|
```
|
|
277
264
|
|
|
278
|
-
###
|
|
265
|
+
### Anthropic Claude
|
|
279
266
|
|
|
280
267
|
```typescript
|
|
281
|
-
import {
|
|
268
|
+
import { MCPLink, createAnthropic } from '@n0ts123/mcplink-core'
|
|
282
269
|
|
|
283
|
-
const
|
|
284
|
-
|
|
285
|
-
})
|
|
286
|
-
|
|
287
|
-
const agent = new MCPLink({
|
|
288
|
-
model: google('gemini-1.5-flash'), // 或 gemini-1.5-pro
|
|
289
|
-
})
|
|
270
|
+
const anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY })
|
|
271
|
+
const agent = new MCPLink({ model: anthropic('claude-3-5-sonnet-20241022') })
|
|
290
272
|
```
|
|
291
273
|
|
|
292
|
-
###
|
|
274
|
+
### Google Gemini
|
|
293
275
|
|
|
294
|
-
|
|
295
|
-
import { createAnthropic } from '@ai-sdk/anthropic'
|
|
276
|
+
> 需额外安装:`npm install @ai-sdk/google`
|
|
296
277
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
278
|
+
```typescript
|
|
279
|
+
import { MCPLink } from '@n0ts123/mcplink-core'
|
|
280
|
+
import { createGoogleGenerativeAI } from '@ai-sdk/google'
|
|
300
281
|
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
})
|
|
282
|
+
const google = createGoogleGenerativeAI({ apiKey: process.env.GOOGLE_API_KEY })
|
|
283
|
+
const agent = new MCPLink({ model: google('gemini-1.5-flash') })
|
|
304
284
|
```
|
|
305
285
|
|
|
306
|
-
###
|
|
307
|
-
|
|
308
|
-
DeepSeek、Qwen、GLM 等兼容 OpenAI 格式的模型:
|
|
286
|
+
### DeepSeek / 通义千问
|
|
309
287
|
|
|
310
288
|
```typescript
|
|
311
|
-
import { createOpenAI } from '@
|
|
289
|
+
import { MCPLink, createOpenAI } from '@n0ts123/mcplink-core'
|
|
312
290
|
|
|
313
291
|
// DeepSeek
|
|
314
292
|
const deepseek = createOpenAI({
|
|
315
293
|
apiKey: process.env.DEEPSEEK_API_KEY,
|
|
316
294
|
baseURL: 'https://api.deepseek.com/v1',
|
|
317
295
|
})
|
|
318
|
-
|
|
319
|
-
const agent = new MCPLink({
|
|
320
|
-
model: deepseek('deepseek-chat'),
|
|
321
|
-
})
|
|
296
|
+
const agent = new MCPLink({ model: deepseek('deepseek-chat') })
|
|
322
297
|
|
|
323
298
|
// 通义千问
|
|
324
299
|
const qwen = createOpenAI({
|
|
325
300
|
apiKey: process.env.QWEN_API_KEY,
|
|
326
301
|
baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
|
327
302
|
})
|
|
328
|
-
|
|
329
|
-
const agent = new MCPLink({
|
|
330
|
-
model: qwen('qwen-turbo'),
|
|
331
|
-
})
|
|
303
|
+
const agent = new MCPLink({ model: qwen('qwen-plus') })
|
|
332
304
|
```
|
|
333
305
|
|
|
334
|
-
## 多轮对话
|
|
306
|
+
## 💬 多轮对话
|
|
335
307
|
|
|
336
308
|
```typescript
|
|
337
|
-
// 方式一:手动管理历史
|
|
338
309
|
const history: Array<{ role: 'user' | 'assistant'; content: string }> = []
|
|
339
310
|
|
|
340
311
|
// 第一轮
|
|
@@ -353,39 +324,41 @@ for await (const event of agent.chatStream('第一个订单的详情', { history
|
|
|
353
324
|
}
|
|
354
325
|
```
|
|
355
326
|
|
|
356
|
-
## 工具过滤
|
|
327
|
+
## 🔧 工具过滤
|
|
357
328
|
|
|
358
329
|
```typescript
|
|
359
330
|
// 只允许使用特定工具
|
|
360
331
|
for await (const event of agent.chatStream('搜索产品', {
|
|
361
332
|
allowedTools: ['search_products', 'get_product_details'],
|
|
362
333
|
})) {
|
|
363
|
-
//
|
|
334
|
+
// 只会调用指定的工具
|
|
364
335
|
}
|
|
365
336
|
```
|
|
366
337
|
|
|
367
|
-
##
|
|
338
|
+
## 🎯 即时结果
|
|
368
339
|
|
|
369
|
-
|
|
370
|
-
// 获取所有可用工具
|
|
371
|
-
const tools = agent.getTools()
|
|
372
|
-
console.log(tools.map(t => t.name))
|
|
340
|
+
当 MCP 工具返回特定格式数据时,可立即推送给前端:
|
|
373
341
|
|
|
374
|
-
|
|
375
|
-
const
|
|
376
|
-
|
|
342
|
+
```typescript
|
|
343
|
+
const agent = new MCPLink({
|
|
344
|
+
model: openai('gpt-4o'),
|
|
345
|
+
mcpServers: { /* ... */ },
|
|
346
|
+
// 配置即时结果匹配器
|
|
347
|
+
immediateResultMatchers: [
|
|
348
|
+
{ type: 'card' }, // 匹配 { type: "card", ... }
|
|
349
|
+
{ type: 'product_list' }, // 匹配 { type: "product_list", ... }
|
|
350
|
+
],
|
|
377
351
|
})
|
|
378
352
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
await agent.stopMCPServer('myServer')
|
|
353
|
+
for await (const event of agent.chatStream('搜索产品')) {
|
|
354
|
+
if (event.type === MCPLinkEventType.IMMEDIATE_RESULT) {
|
|
355
|
+
// 立即展示卡片/特殊格式数据
|
|
356
|
+
showCard(event.data.immediateResult)
|
|
357
|
+
}
|
|
358
|
+
}
|
|
386
359
|
```
|
|
387
360
|
|
|
388
|
-
##
|
|
361
|
+
## 📋 事件类型
|
|
389
362
|
|
|
390
363
|
| 事件 | 说明 | 数据 |
|
|
391
364
|
|------|------|------|
|
|
@@ -394,113 +367,69 @@ await agent.stopMCPServer('myServer')
|
|
|
394
367
|
| `thinking_start` | 思考开始 | `{}` |
|
|
395
368
|
| `thinking_delta` | 思考内容 | `{ content }` |
|
|
396
369
|
| `thinking_end` | 思考结束 | `{}` |
|
|
370
|
+
| `thinking_content` | 完整思考内容 | `{ content }` |
|
|
397
371
|
| `text_start` | 文本开始 | `{}` |
|
|
398
372
|
| `text_delta` | 文本内容 | `{ content }` |
|
|
399
373
|
| `text_end` | 文本结束 | `{}` |
|
|
400
374
|
| `tool_call_start` | 工具调用开始 | `{ toolName, toolCallId, toolArgs }` |
|
|
375
|
+
| `tool_call_delta` | 工具参数流式 | `{ toolCallId, argsTextDelta }` |
|
|
401
376
|
| `tool_executing` | 工具执行中 | `{ toolName, toolCallId, toolArgs }` |
|
|
402
|
-
| `tool_result` |
|
|
403
|
-
| `
|
|
404
|
-
| `
|
|
405
|
-
|
|
406
|
-
## 高级用法
|
|
407
|
-
|
|
408
|
-
### 直接使用 Agent
|
|
409
|
-
|
|
410
|
-
如果你只需要使用特定的 Agent 实现:
|
|
411
|
-
|
|
412
|
-
```typescript
|
|
413
|
-
import { Agent, PromptBasedAgent, MCPManager } from '@n0ts123/mcplink-core'
|
|
414
|
-
import { createOpenAI } from '@ai-sdk/openai'
|
|
415
|
-
|
|
416
|
-
const openai = createOpenAI({ apiKey: '...' })
|
|
417
|
-
const mcpManager = new MCPManager()
|
|
418
|
-
|
|
419
|
-
// 添加 MCP 服务器
|
|
420
|
-
mcpManager.addServer('myTools', {
|
|
421
|
-
type: 'stdio',
|
|
422
|
-
command: 'node',
|
|
423
|
-
args: ['./server.js'],
|
|
424
|
-
})
|
|
425
|
-
|
|
426
|
-
// 启动服务器
|
|
427
|
-
await mcpManager.startAll()
|
|
428
|
-
|
|
429
|
-
// 使用原生 Agent(适用于支持 function calling 的模型)
|
|
430
|
-
const nativeAgent = new Agent(openai('gpt-4o'), mcpManager, {
|
|
431
|
-
systemPrompt: '你是一个智能助手',
|
|
432
|
-
maxIterations: 10,
|
|
433
|
-
})
|
|
434
|
-
|
|
435
|
-
// 使用 Prompt-Based Agent(适用于所有模型)
|
|
436
|
-
const promptAgent = new PromptBasedAgent(openai('gpt-4o'), mcpManager, {
|
|
437
|
-
systemPrompt: '你是一个智能助手',
|
|
438
|
-
maxIterations: 10,
|
|
439
|
-
})
|
|
377
|
+
| `tool_result` | 工具结果 | `{ toolName, toolResult, toolCallId, duration, isError }` |
|
|
378
|
+
| `immediate_result` | 即时结果 | `{ toolName, toolCallId, immediateResult }` |
|
|
379
|
+
| `complete` | 完成 | `{ totalDuration, totalIterations }` |
|
|
380
|
+
| `error` | 错误 | `{ error }` |
|
|
440
381
|
|
|
441
|
-
|
|
442
|
-
for await (const event of promptAgent.chatStream('你好')) {
|
|
443
|
-
console.log(event)
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// 关闭
|
|
447
|
-
await mcpManager.stopAll()
|
|
448
|
-
```
|
|
449
|
-
|
|
450
|
-
### 自定义 MCP 管理器
|
|
382
|
+
## 🔧 手动工具管理
|
|
451
383
|
|
|
452
384
|
```typescript
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
const mcpManager = new MCPManager()
|
|
456
|
-
|
|
457
|
-
// 添加多个服务器
|
|
458
|
-
mcpManager.addServer('business', {
|
|
459
|
-
type: 'stdio',
|
|
460
|
-
command: 'node',
|
|
461
|
-
args: ['./business-server.js'],
|
|
462
|
-
})
|
|
463
|
-
|
|
464
|
-
mcpManager.addServer('database', {
|
|
465
|
-
type: 'sse',
|
|
466
|
-
url: 'http://localhost:8080/mcp',
|
|
467
|
-
headers: { Authorization: 'Bearer xxx' },
|
|
468
|
-
})
|
|
469
|
-
|
|
470
|
-
// 按需启动
|
|
471
|
-
await mcpManager.startServer('business')
|
|
385
|
+
// 获取所有可用工具
|
|
386
|
+
const tools = agent.getTools()
|
|
472
387
|
|
|
473
|
-
//
|
|
474
|
-
const
|
|
388
|
+
// 手动调用工具
|
|
389
|
+
const result = await agent.callTool('search_products', { keyword: 'test' })
|
|
475
390
|
|
|
476
|
-
//
|
|
477
|
-
const
|
|
391
|
+
// 获取 MCP 服务器状态
|
|
392
|
+
const statuses = agent.getMCPServerStatuses()
|
|
478
393
|
|
|
479
|
-
//
|
|
480
|
-
|
|
394
|
+
// 手动控制 MCP 服务器
|
|
395
|
+
await agent.startMCPServer('myServer')
|
|
396
|
+
await agent.stopMCPServer('myServer')
|
|
481
397
|
```
|
|
482
398
|
|
|
483
|
-
## TypeScript 类型
|
|
399
|
+
## 📝 TypeScript 类型
|
|
484
400
|
|
|
485
401
|
```typescript
|
|
486
402
|
import type {
|
|
487
403
|
MCPLinkConfig,
|
|
488
404
|
MCPServerConfig,
|
|
405
|
+
MCPServerConfigStdio,
|
|
406
|
+
MCPServerConfigSSE,
|
|
407
|
+
MCPServerConfigStreamableHTTP,
|
|
489
408
|
MCPLinkEvent,
|
|
409
|
+
MCPLinkEventData,
|
|
490
410
|
MCPTool,
|
|
491
411
|
MCPServerStatus,
|
|
492
412
|
ChatResult,
|
|
413
|
+
ChatCallbacks,
|
|
414
|
+
ImmediateResultMatcher,
|
|
493
415
|
} from '@n0ts123/mcplink-core'
|
|
494
416
|
|
|
495
417
|
import { MCPLinkEventType } from '@n0ts123/mcplink-core'
|
|
496
418
|
```
|
|
497
419
|
|
|
498
|
-
## 环境要求
|
|
420
|
+
## 📋 环境要求
|
|
499
421
|
|
|
500
422
|
- **Node.js**: >= 18.0.0
|
|
501
423
|
- **模块系统**: ES Module(推荐)或 CommonJS(需使用动态 import)
|
|
502
424
|
|
|
503
|
-
##
|
|
425
|
+
## 🔗 相关链接
|
|
426
|
+
|
|
427
|
+
- [GitHub 仓库](https://github.com/n0tssss/MCPLink)
|
|
428
|
+
- [完整文档](https://github.com/n0tssss/MCPLink#readme)
|
|
429
|
+
- [问题反馈](https://github.com/n0tssss/MCPLink/issues)
|
|
430
|
+
- [MCP 协议规范](https://modelcontextprotocol.io/)
|
|
431
|
+
- [Vercel AI SDK](https://sdk.vercel.ai/)
|
|
504
432
|
|
|
505
|
-
|
|
433
|
+
## 📄 许可证
|
|
506
434
|
|
|
435
|
+
MIT License © [n0tssss](https://github.com/n0tssss)
|