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