@n0ts123/mcplink-core 0.0.12 → 0.0.14

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 CHANGED
@@ -1,709 +1,490 @@
1
- # @n0ts123/mcplink-core
2
-
3
- MCPLink 核心 SDK - AI Agent 工具调用框架,让 AI 轻松调用 MCP 工具。
4
-
5
- [![npm version](https://img.shields.io/npm/v/@n0ts123/mcplink-core.svg)](https://www.npmjs.com/package/@n0ts123/mcplink-core)
6
- [![license](https://img.shields.io/npm/l/@n0ts123/mcplink-core.svg)](https://github.com/n0tssss/MCPLink/blob/master/LICENSE)
7
- [![GitHub](https://img.shields.io/badge/GitHub-n0tssss%2FMCPLink-blue)](https://github.com/n0tssss/MCPLink)
8
-
9
- ## ✨ 特性
10
-
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** - 完整的类型支持
20
-
21
- ## 📦 安装
22
-
23
- ```bash
24
- # npm
25
- npm install @n0ts123/mcplink-core
26
-
27
- # pnpm
28
- pnpm add @n0ts123/mcplink-core
29
-
30
- # yarn
31
- yarn add @n0ts123/mcplink-core
32
- ```
33
-
34
- > 💡 **内置 AI SDK**:本包已内置 `@ai-sdk/openai` 和 `@ai-sdk/anthropic`,无需额外安装即可直接使用。
35
- >
36
- > 如需使用 Google Gemini,需额外安装:`npm install @ai-sdk/google`
37
-
38
- ## 🚀 快速开始
39
-
40
- ### TypeScript / JavaScript (ESM)
41
-
42
- ```typescript
43
- import { MCPLink, createOpenAI } from '@n0ts123/mcplink-core'
44
-
45
- // 创建模型
46
- const openai = createOpenAI({
47
- apiKey: process.env.OPENAI_API_KEY,
48
- baseURL: 'https://api.openai.com/v1', // 可选
49
- })
50
-
51
- // 创建 Agent
52
- const agent = new MCPLink({
53
- model: openai('gpt-4o'),
54
- systemPrompt: '你是一个智能助手',
55
- maxIterations: 10,
56
- parallelToolCalls: true, // 启用并行工具调用
57
- mcpServers: {
58
- // stdio 模式
59
- myTools: {
60
- type: 'stdio',
61
- command: 'node',
62
- args: ['./my-mcp-server.js'],
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
- },
74
- },
75
- })
76
-
77
- // 初始化并对话
78
- await agent.initialize()
79
- const result = await agent.chat('你好')
80
- console.log(result.content)
81
- await agent.close()
82
- ```
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
-
114
- ### 流式响应
115
-
116
- ```typescript
117
- import { MCPLink, MCPLinkEventType, createOpenAI } from '@n0ts123/mcplink-core'
118
-
119
- const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY })
120
-
121
- const agent = new MCPLink({
122
- model: openai('gpt-4o'),
123
- mcpServers: { /* ... */ },
124
- })
125
-
126
- await agent.initialize()
127
-
128
- for await (const event of agent.chatStream('帮我查询订单')) {
129
- switch (event.type) {
130
- case MCPLinkEventType.ITERATION_START:
131
- console.log(`📍 开始第 ${event.data.iteration} 轮迭代`)
132
- break
133
-
134
- case MCPLinkEventType.THINKING_START:
135
- console.log('💭 思考中...')
136
- break
137
-
138
- case MCPLinkEventType.THINKING_DELTA:
139
- process.stdout.write(event.data.content || '')
140
- break
141
-
142
- case MCPLinkEventType.TOOL_CALL_START:
143
- console.log(`🔧 调用工具: ${event.data.toolName}`)
144
- console.log(` 参数: ${JSON.stringify(event.data.toolArgs)}`)
145
- break
146
-
147
- case MCPLinkEventType.TOOL_RESULT:
148
- const status = event.data.isError ? '❌' : '✅'
149
- console.log(`${status} 结果 (${event.data.duration}ms)`)
150
- break
151
-
152
- case MCPLinkEventType.IMMEDIATE_RESULT:
153
- // 即时结果,可用于渲染特殊 UI
154
- console.log('🎯 即时结果:', event.data.immediateResult)
155
- break
156
-
157
- case MCPLinkEventType.TEXT_DELTA:
158
- process.stdout.write(event.data.content || '')
159
- break
160
-
161
- case MCPLinkEventType.COMPLETE:
162
- console.log(`\n✅ 完成! 耗时: ${event.data.totalDuration}ms, 迭代: ${event.data.totalIterations}`)
163
- break
164
-
165
- case MCPLinkEventType.ERROR:
166
- console.error(`❌ 错误: ${event.data.error}`)
167
- break
168
- }
169
- }
170
- ```
171
-
172
- ## ⚙️ 配置选项
173
-
174
- ### MCPLinkConfig
175
-
176
- ```typescript
177
- interface MCPLinkConfig {
178
- /** AI 模型实例(必填)*/
179
- model: LanguageModel
180
-
181
- /** 模型名称,用于自动检测是否支持原生 function calling */
182
- modelName?: string
183
-
184
- /** 系统提示词 */
185
- systemPrompt?: string
186
-
187
- /** 最大迭代次数(默认 10)*/
188
- maxIterations?: number
189
-
190
- /** MCP 服务器配置 */
191
- mcpServers?: Record<string, MCPServerConfig>
192
-
193
- /** 是否并行执行工具调用(默认 true)*/
194
- parallelToolCalls?: boolean
195
-
196
- /**
197
- * 是否强制使用 Prompt-Based 模式
198
- * - true: 强制使用 PromptBasedAgent
199
- * - false: 强制使用原生 Agent
200
- * - 'auto': 自动检测(默认)
201
- */
202
- usePromptBasedTools?: boolean | 'auto'
203
-
204
- /**
205
- * 是否启用思考阶段(默认 true)
206
- * 启用后每次迭代会先让 AI 思考分析,再执行工具调用
207
- * 优点:Chain-of-Thought 效应,提高复杂任务准确性
208
- */
209
- enableThinkingPhase?: boolean
210
-
211
- /**
212
- * 思考阶段提示词(可选)
213
- * 自定义 AI 在调用工具前的思考分析提示
214
- * 不配置则使用内置的默认提示词
215
- */
216
- thinkingPhasePrompt?: string
217
-
218
- /** 即时结果匹配器,匹配时触发 IMMEDIATE_RESULT 事件 */
219
- immediateResultMatchers?: Array<Record<string, unknown>>
220
- }
221
- ```
222
-
223
- ### 配置项详解
224
-
225
- #### 1. `systemPrompt` - 系统提示词
226
-
227
- 定义 AI 的角色和行为规范:
228
-
229
- ```typescript
230
- import { MCPLink, DEFAULT_SYSTEM_PROMPT } from '@n0ts123/mcplink-core'
231
-
232
- const agent = new MCPLink({
233
- model: openai('gpt-4o'),
234
- // 完全自定义
235
- systemPrompt: `你是一个电商客服助手。
236
-
237
- ## 你的职责
238
- - 帮助用户查询订单
239
- - 解答产品问题
240
- - 处理售后服务
241
-
242
- ## 回复风格
243
- - 专业、简洁、热情
244
- - 使用 emoji 增加亲和力`,
245
- mcpServers: { /* ... */ },
246
- })
247
-
248
- // 也可以基于默认提示词扩展
249
- const agent2 = new MCPLink({
250
- model: openai('gpt-4o'),
251
- systemPrompt: DEFAULT_SYSTEM_PROMPT + `
252
-
253
- ## 额外规则
254
- - 回复不超过 200 字
255
- - 重要信息用加粗标注`,
256
- mcpServers: { /* ... */ },
257
- })
258
- ```
259
-
260
- #### 2. `thinkingPhasePrompt` - 思考阶段提示词
261
-
262
- 自定义 AI 在调用工具前的思考分析过程:
263
-
264
- ```typescript
265
- import { MCPLink, DEFAULT_THINKING_PHASE_PROMPT } from '@n0ts123/mcplink-core'
266
-
267
- const agent = new MCPLink({
268
- model: openai('gpt-4o'),
269
- enableThinkingPhase: true,
270
- // 完全自定义思考提示词
271
- thinkingPhasePrompt: `请分析用户的需求:
272
-
273
- 1. 用户想做什么?
274
- 2. 需要调用哪些工具?
275
- 3. 执行顺序是什么?
276
-
277
- 注意事项:
278
- - 用自然语言表达思考过程
279
- - 不要暴露任何系统内部信息
280
- - 不要展示技术细节或数据结构`,
281
- mcpServers: { /* ... */ },
282
- })
283
-
284
- // 基于默认提示词扩展
285
- const agent2 = new MCPLink({
286
- model: openai('gpt-4o'),
287
- enableThinkingPhase: true,
288
- thinkingPhasePrompt: DEFAULT_THINKING_PHASE_PROMPT + `
289
- - 优先考虑用户体验
290
- - 复杂任务要拆解步骤`,
291
- mcpServers: { /* ... */ },
292
- })
293
- ```
294
-
295
- **安全说明**:默认的思考提示词已包含安全规则,防止 AI 在思考过程中暴露敏感信息(如用户 token、ID 等)。自定义时请确保包含类似的安全约束。
296
-
297
- #### 3. `maxIterations` - 最大迭代次数
298
-
299
- 控制 Agent 循环的最大轮数,防止无限循环:
300
-
301
- ```typescript
302
- const agent = new MCPLink({
303
- model: openai('gpt-4o'),
304
- // 简单任务,减少迭代
305
- maxIterations: 5,
306
- mcpServers: { /* ... */ },
307
- })
308
-
309
- const complexAgent = new MCPLink({
310
- model: openai('gpt-4o'),
311
- // 复杂任务,允许更多迭代
312
- maxIterations: 20,
313
- mcpServers: { /* ... */ },
314
- })
315
- ```
316
-
317
- #### 4. `parallelToolCalls` - 并行工具调用
318
-
319
- 控制是否同时执行多个独立的工具调用:
320
-
321
- ```typescript
322
- const agent = new MCPLink({
323
- model: openai('gpt-4o'),
324
- // 启用并行调用(默认)- 多个独立工具同时执行
325
- parallelToolCalls: true,
326
- mcpServers: { /* ... */ },
327
- })
328
-
329
- const serialAgent = new MCPLink({
330
- model: openai('gpt-4o'),
331
- // 禁用并行 - 工具依次执行,适合有依赖关系的场景
332
- parallelToolCalls: false,
333
- mcpServers: { /* ... */ },
334
- })
335
- ```
336
-
337
- #### 5. `enableThinkingPhase` - 启用思考阶段
338
-
339
- 控制是否在工具调用前进行思考分析:
340
-
341
- ```typescript
342
- const agent = new MCPLink({
343
- model: openai('gpt-4o'),
344
- // 启用思考阶段(默认)- 提高准确性
345
- enableThinkingPhase: true,
346
- mcpServers: { /* ... */ },
347
- })
348
-
349
- const fastAgent = new MCPLink({
350
- model: openai('gpt-4o'),
351
- // 禁用思考阶段 - 减少延迟,适合简单任务
352
- enableThinkingPhase: false,
353
- mcpServers: { /* ... */ },
354
- })
355
- ```
356
-
357
- #### 6. `immediateResultMatchers` - 即时结果匹配器
358
-
359
- 定义哪些工具返回结果需要立即推送给前端:
360
-
361
- ```typescript
362
- const agent = new MCPLink({
363
- model: openai('gpt-4o'),
364
- immediateResultMatchers: [
365
- { type: 'card' }, // 匹配 { type: "card", ... }
366
- { type: 'product_list' }, // 匹配 { type: "product_list", ... }
367
- { format: 'table' }, // 匹配 { format: "table", ... }
368
- { action: 'redirect' }, // 匹配 { action: "redirect", url: "..." }
369
- ],
370
- mcpServers: { /* ... */ },
371
- })
372
- ```
373
-
374
- #### 7. `usePromptBasedTools` - 强制模式选择
375
-
376
- 强制指定使用原生或 Prompt-Based 模式:
377
-
378
- ```typescript
379
- // 自动检测(默认)
380
- const autoAgent = new MCPLink({
381
- model: openai('gpt-4o'),
382
- usePromptBasedTools: 'auto',
383
- mcpServers: { /* ... */ },
384
- })
385
-
386
- // 强制使用 Prompt-Based 模式
387
- const promptAgent = new MCPLink({
388
- model: openai('gpt-4o'),
389
- usePromptBasedTools: true,
390
- mcpServers: { /* ... */ },
391
- })
392
-
393
- // 强制使用原生 Function Calling
394
- const nativeAgent = new MCPLink({
395
- model: openai('gpt-4o'),
396
- usePromptBasedTools: false,
397
- mcpServers: { /* ... */ },
398
- })
399
- ```
400
-
401
- ### 完整配置示例
402
-
403
- ```typescript
404
- import {
405
- MCPLink,
406
- createOpenAI,
407
- DEFAULT_SYSTEM_PROMPT,
408
- DEFAULT_THINKING_PHASE_PROMPT
409
- } from '@n0ts123/mcplink-core'
410
-
411
- const openai = createOpenAI({
412
- apiKey: process.env.OPENAI_API_KEY,
413
- baseURL: 'https://api.openai.com/v1',
414
- })
415
-
416
- const agent = new MCPLink({
417
- // 必填:AI 模型
418
- model: openai('gpt-4o'),
419
-
420
- // 可选:模型名称(用于自动检测能力)
421
- modelName: 'gpt-4o',
422
-
423
- // 可选:系统提示词
424
- systemPrompt: `你是一个智能客服助手。
425
-
426
- ## 职责
427
- - 帮助用户查询和管理订单
428
- - 解答产品相关问题
429
- - 提供专业的购物建议
430
-
431
- ## 回复规范
432
- - 简洁明了,重点突出
433
- - 使用列表展示多条信息
434
- - 金额用 ¥ 符号标注`,
435
-
436
- // 可选:思考阶段提示词
437
- thinkingPhasePrompt: `分析用户需求:
438
- 1. 用户的核心诉求是什么?
439
- 2. 需要获取哪些信息?
440
- 3. 应该调用什么工具?
441
-
442
- 规则:
443
- - 不要暴露任何内部信息
444
- - 用自然语言表达
445
- - 专注于解决用户问题`,
446
-
447
- // 可选:最大迭代次数
448
- maxIterations: 10,
449
-
450
- // 可选:并行工具调用
451
- parallelToolCalls: true,
452
-
453
- // 可选:启用思考阶段
454
- enableThinkingPhase: true,
455
-
456
- // 可选:模式选择
457
- usePromptBasedTools: 'auto',
458
-
459
- // 可选:即时结果匹配器
460
- immediateResultMatchers: [
461
- { type: 'card' },
462
- { type: 'product_list' },
463
- ],
464
-
465
- // MCP 服务器配置
466
- mcpServers: {
467
- // stdio 模式 - 本地进程
468
- business: {
469
- type: 'stdio',
470
- command: 'node',
471
- args: ['./mcp-server.js'],
472
- env: { DEBUG: 'true' },
473
- },
474
- // SSE 模式 - 远程服务
475
- remote: {
476
- type: 'sse',
477
- url: 'http://localhost:8080/mcp',
478
- headers: { 'Authorization': 'Bearer token' },
479
- },
480
- // Streamable HTTP 模式
481
- streamable: {
482
- type: 'streamable-http',
483
- url: 'http://localhost:8080/mcp/stream',
484
- headers: { 'X-API-Key': 'key' },
485
- },
486
- },
487
- })
488
- ```
489
-
490
- ### MCP 服务器配置
491
-
492
- ```typescript
493
- // Stdio 模式(本地进程)
494
- interface MCPServerConfigStdio {
495
- type?: 'stdio'
496
- command: string
497
- args?: string[]
498
- env?: Record<string, string>
499
- }
500
-
501
- // SSE 模式(远程服务)
502
- interface MCPServerConfigSSE {
503
- type: 'sse'
504
- url: string
505
- headers?: Record<string, string>
506
- }
507
-
508
- // Streamable HTTP 模式
509
- interface MCPServerConfigStreamableHTTP {
510
- type: 'streamable-http'
511
- url: string
512
- headers?: Record<string, string>
513
- }
514
- ```
515
-
516
- ## 🤖 多模型支持
517
-
518
- MCPLink 会根据模型自动选择最佳的调用方式:
519
-
520
- | 模型 | 模式 | 说明 |
521
- |------|------|------|
522
- | GPT-4o, GPT-4, GPT-3.5 | 原生 | 使用 function calling |
523
- | Claude-3, Claude-3.5 | 原生 | 使用 function calling |
524
- | Gemini Flash/Pro | 原生 | 使用 function calling |
525
- | Mistral, Mixtral | 原生 | 使用 function calling |
526
- | DeepSeek | Prompt-Based | 使用 prompt 引导 |
527
- | Qwen, 通义千问 | Prompt-Based | 使用 prompt 引导 |
528
- | Llama, Yi, GLM | Prompt-Based | 使用 prompt 引导 |
529
-
530
- ### OpenAI
531
-
532
- ```typescript
533
- import { MCPLink, createOpenAI } from '@n0ts123/mcplink-core'
534
-
535
- const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY })
536
- const agent = new MCPLink({ model: openai('gpt-4o') })
537
- ```
538
-
539
- ### Anthropic Claude
540
-
541
- ```typescript
542
- import { MCPLink, createAnthropic } from '@n0ts123/mcplink-core'
543
-
544
- const anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY })
545
- const agent = new MCPLink({ model: anthropic('claude-3-5-sonnet-20241022') })
546
- ```
547
-
548
- ### Google Gemini
549
-
550
- > 需额外安装:`npm install @ai-sdk/google`
551
-
552
- ```typescript
553
- import { MCPLink } from '@n0ts123/mcplink-core'
554
- import { createGoogleGenerativeAI } from '@ai-sdk/google'
555
-
556
- const google = createGoogleGenerativeAI({ apiKey: process.env.GOOGLE_API_KEY })
557
- const agent = new MCPLink({ model: google('gemini-1.5-flash') })
558
- ```
559
-
560
- ### DeepSeek / 通义千问
561
-
562
- ```typescript
563
- import { MCPLink, createOpenAI } from '@n0ts123/mcplink-core'
564
-
565
- // DeepSeek
566
- const deepseek = createOpenAI({
567
- apiKey: process.env.DEEPSEEK_API_KEY,
568
- baseURL: 'https://api.deepseek.com/v1',
569
- })
570
- const agent = new MCPLink({ model: deepseek('deepseek-chat') })
571
-
572
- // 通义千问
573
- const qwen = createOpenAI({
574
- apiKey: process.env.QWEN_API_KEY,
575
- baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
576
- })
577
- const agent = new MCPLink({ model: qwen('qwen-plus') })
578
- ```
579
-
580
- ## 💬 多轮对话
581
-
582
- ```typescript
583
- const history: Array<{ role: 'user' | 'assistant'; content: string }> = []
584
-
585
- // 第一轮
586
- let response = ''
587
- for await (const event of agent.chatStream('帮我查订单')) {
588
- if (event.type === MCPLinkEventType.TEXT_DELTA) {
589
- response += event.data.content || ''
590
- }
591
- }
592
- history.push({ role: 'user', content: '帮我查订单' })
593
- history.push({ role: 'assistant', content: response })
594
-
595
- // 第二轮(携带历史)
596
- for await (const event of agent.chatStream('第一个订单的详情', { history })) {
597
- // ...
598
- }
599
- ```
600
-
601
- ## 🔧 工具过滤
602
-
603
- ```typescript
604
- // 只允许使用特定工具
605
- for await (const event of agent.chatStream('搜索产品', {
606
- allowedTools: ['search_products', 'get_product_details'],
607
- })) {
608
- // 只会调用指定的工具
609
- }
610
- ```
611
-
612
- ## 🎯 即时结果
613
-
614
- 当 MCP 工具返回特定格式数据时,可立即推送给前端:
615
-
616
- ```typescript
617
- const agent = new MCPLink({
618
- model: openai('gpt-4o'),
619
- mcpServers: { /* ... */ },
620
- // 配置即时结果匹配器
621
- immediateResultMatchers: [
622
- { type: 'card' }, // 匹配 { type: "card", ... }
623
- { type: 'product_list' }, // 匹配 { type: "product_list", ... }
624
- ],
625
- })
626
-
627
- for await (const event of agent.chatStream('搜索产品')) {
628
- if (event.type === MCPLinkEventType.IMMEDIATE_RESULT) {
629
- // 立即展示卡片/特殊格式数据
630
- showCard(event.data.immediateResult)
631
- }
632
- }
633
- ```
634
-
635
- ## 📋 事件类型
636
-
637
- | 事件 | 说明 | 数据 |
638
- |------|------|------|
639
- | `iteration_start` | 迭代开始 | `{ iteration, maxIterations }` |
640
- | `iteration_end` | 迭代结束 | `{ iteration }` |
641
- | `thinking_start` | 思考开始 | `{}` |
642
- | `thinking_delta` | 思考内容 | `{ content }` |
643
- | `thinking_end` | 思考结束 | `{}` |
644
- | `thinking_content` | 完整思考内容 | `{ content }` |
645
- | `text_start` | 文本开始 | `{}` |
646
- | `text_delta` | 文本内容 | `{ content }` |
647
- | `text_end` | 文本结束 | `{}` |
648
- | `tool_call_start` | 工具调用开始 | `{ toolName, toolCallId, toolArgs }` |
649
- | `tool_call_delta` | 工具参数流式 | `{ toolCallId, argsTextDelta }` |
650
- | `tool_executing` | 工具执行中 | `{ toolName, toolCallId, toolArgs }` |
651
- | `tool_result` | 工具结果 | `{ toolName, toolResult, toolCallId, duration, isError }` |
652
- | `immediate_result` | 即时结果 | `{ toolName, toolCallId, immediateResult }` |
653
- | `complete` | 完成 | `{ totalDuration, totalIterations }` |
654
- | `error` | 错误 | `{ error }` |
655
-
656
- ## 🔧 手动工具管理
657
-
658
- ```typescript
659
- // 获取所有可用工具
660
- const tools = agent.getTools()
661
-
662
- // 手动调用工具
663
- const result = await agent.callTool('search_products', { keyword: 'test' })
664
-
665
- // 获取 MCP 服务器状态
666
- const statuses = agent.getMCPServerStatuses()
667
-
668
- // 手动控制 MCP 服务器
669
- await agent.startMCPServer('myServer')
670
- await agent.stopMCPServer('myServer')
671
- ```
672
-
673
- ## 📝 TypeScript 类型
674
-
675
- ```typescript
676
- import type {
677
- MCPLinkConfig,
678
- MCPServerConfig,
679
- MCPServerConfigStdio,
680
- MCPServerConfigSSE,
681
- MCPServerConfigStreamableHTTP,
682
- MCPLinkEvent,
683
- MCPLinkEventData,
684
- MCPTool,
685
- MCPServerStatus,
686
- ChatResult,
687
- ChatCallbacks,
688
- ImmediateResultMatcher,
689
- } from '@n0ts123/mcplink-core'
690
-
691
- import { MCPLinkEventType } from '@n0ts123/mcplink-core'
692
- ```
693
-
694
- ## 📋 环境要求
695
-
696
- - **Node.js**: >= 18.0.0
697
- - **模块系统**: ES Module(推荐)或 CommonJS(需使用动态 import)
698
-
699
- ## 🔗 相关链接
700
-
701
- - [GitHub 仓库](https://github.com/n0tssss/MCPLink)
702
- - [完整文档](https://github.com/n0tssss/MCPLink#readme)
703
- - [问题反馈](https://github.com/n0tssss/MCPLink/issues)
704
- - [MCP 协议规范](https://modelcontextprotocol.io/)
705
- - [Vercel AI SDK](https://sdk.vercel.ai/)
706
-
707
- ## 📄 许可证
708
-
709
- MIT License © [n0tssss](https://github.com/n0tssss)
1
+ # @n0ts123/mcplink-core
2
+
3
+ 更方便的在代码里集成 MCP 工具直接让 AI 使用。
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@n0ts123/mcplink-core.svg)](https://www.npmjs.com/package/@n0ts123/mcplink-core)
6
+ [![license](https://img.shields.io/npm/l/@n0ts123/mcplink-core.svg)](https://github.com/n0tssss/MCPLink/blob/master/LICENSE)
7
+
8
+ ## 这是什么
9
+
10
+ 用代码让 AI 调用 [MCP](https://modelcontextprotocol.io/) 工具,自动完成多步任务。
11
+
12
+ ```
13
+ 你:"帮我查一下最近订单"
14
+ AI:调用 get_orders 工具 收到结果 "您有3个订单..."
15
+ ```
16
+
17
+ **特点**:流式响应、支持任意 AI 参数、你自己掌控消息历史。
18
+
19
+ ## 安装
20
+
21
+ ```bash
22
+ npm install @n0ts123/mcplink-core
23
+ ```
24
+
25
+ ## 用法
26
+
27
+ ```bash
28
+ # npm
29
+ npm install @n0ts123/mcplink-core
30
+
31
+ # pnpm
32
+ pnpm add @n0ts123/mcplink-core
33
+
34
+ # yarn
35
+ yarn add @n0ts123/mcplink-core
36
+ ```
37
+
38
+ ## 🚀 快速开始
39
+
40
+ ### 基础用法
41
+
42
+ ```typescript
43
+ import { MCPLink } from '@n0ts123/mcplink-core'
44
+
45
+ // 创建 MCPLink 实例
46
+ const mcpLink = new MCPLink({
47
+ // AI 配置(OpenAI 兼容格式)
48
+ ai: {
49
+ baseURL: 'https://api.openai.com/v1',
50
+ apiKey: process.env.OPENAI_API_KEY,
51
+ model: 'gpt-4o',
52
+ // 支持任意自定义参数
53
+ temperature: 0.7,
54
+ enable_thinking: false,
55
+ },
56
+ // MCP 服务器配置
57
+ mcpServers: {
58
+ myTools: {
59
+ type: 'stdio',
60
+ command: 'node',
61
+ args: ['./my-mcp-server.js'],
62
+ },
63
+ },
64
+ })
65
+
66
+ // 初始化
67
+ await mcpLink.initialize()
68
+
69
+ // 发起对话(用户自己处理流式响应)
70
+ for await (const event of mcpLink.chatStream([
71
+ { role: 'user', content: '你好' }
72
+ ])) {
73
+ switch (event.type) {
74
+ case 'text':
75
+ process.stdout.write(event.content)
76
+ break
77
+ case 'tool_call':
78
+ console.log('调用工具:', event.toolCall.name)
79
+ break
80
+ case 'done':
81
+ console.log('\n完成!')
82
+ break
83
+ }
84
+ }
85
+
86
+ await mcpLink.close()
87
+ ```
88
+
89
+ ### 使用标准事件流
90
+
91
+ 如果你希望使用更丰富的标准事件,可以使用 `toStandardStream`:
92
+
93
+ ```typescript
94
+ import { MCPLink, toStandardStream, type StandardStreamEvent } from '@n0ts123/mcplink-core'
95
+
96
+ const mcpLink = new MCPLink({
97
+ ai: { baseURL: '...', apiKey: '...', model: 'gpt-4o' },
98
+ mcpServers: { /* ... */ },
99
+ })
100
+
101
+ await mcpLink.initialize()
102
+
103
+ // 创建底层流
104
+ async function* rawStream() {
105
+ for await (const event of mcpLink.chatStream([
106
+ { role: 'user', content: '查询订单' }
107
+ ])) {
108
+ yield event
109
+ }
110
+ }
111
+
112
+ // 转换为标准事件流
113
+ for await (const event of toStandardStream(rawStream(), {
114
+ maxIterations: 10,
115
+ executeTool: async (name, args) => {
116
+ return await mcpLink.callTool(name, args)
117
+ },
118
+ })) {
119
+ switch (event.type) {
120
+ case 'text_start':
121
+ console.log('开始输出文本...')
122
+ break
123
+ case 'text_delta':
124
+ process.stdout.write(event.content)
125
+ break
126
+ case 'text_end':
127
+ console.log('\n文本输出结束')
128
+ break
129
+ case 'tool_call_start':
130
+ console.log(`调用工具: ${event.toolName}`)
131
+ break
132
+ case 'tool_executing':
133
+ console.log(`执行中: ${event.toolName}...`)
134
+ break
135
+ case 'tool_result':
136
+ console.log(`结果: ${JSON.stringify(event.toolResult)}`)
137
+ break
138
+ case 'complete':
139
+ console.log(`完成! 耗时: ${event.totalDuration}ms`)
140
+ break
141
+ }
142
+ }
143
+ ```
144
+
145
+ ## ⚙️ 配置选项
146
+
147
+ ### MCPLinkConfig
148
+
149
+ ```typescript
150
+ interface MCPLinkConfig {
151
+ /** AI 配置(OpenAI 兼容格式) */
152
+ ai: {
153
+ baseURL: string
154
+ apiKey: string
155
+ model: string
156
+ // 支持任意自定义参数
157
+ temperature?: number
158
+ max_tokens?: number
159
+ enable_thinking?: boolean
160
+ // 其他参数...
161
+ [key: string]: unknown
162
+ }
163
+
164
+ /** 适配器类型(默认 'openai') */
165
+ adapter?: 'openai' | AIAdapter
166
+
167
+ /** MCP 服务器配置 */
168
+ mcpServers?: Record<string, MCPServerConfig>
169
+
170
+ /** 最大迭代次数(默认 10) */
171
+ maxIterations?: number
172
+ }
173
+ ```
174
+
175
+ ### 完全自定义参数
176
+
177
+ 所有在 `ai` 中的参数都会原封不动传递给 AI 提供商:
178
+
179
+ ```typescript
180
+ const mcpLink = new MCPLink({
181
+ ai: {
182
+ baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
183
+ apiKey: process.env.QWEN_API_KEY,
184
+ model: 'qwen3.5-plus',
185
+ // 完全自定义参数
186
+ temperature: 0.8,
187
+ max_tokens: 2048,
188
+ enable_thinking: true, // Qwen 思考模式
189
+ top_p: 0.9,
190
+ // 任意其他参数...
191
+ custom_param: 'value',
192
+ },
193
+ mcpServers: { /* ... */ },
194
+ })
195
+ ```
196
+
197
+ ### MCP 服务器配置
198
+
199
+ ```typescript
200
+ // Stdio 模式(本地进程)
201
+ interface MCPServerConfigStdio {
202
+ type: 'stdio'
203
+ command: string
204
+ args?: string[]
205
+ env?: Record<string, string>
206
+ }
207
+
208
+ // Streamable HTTP 模式
209
+ interface MCPServerConfigStreamableHTTP {
210
+ type: 'streamable-http'
211
+ url: string
212
+ headers?: Record<string, string>
213
+ }
214
+ ```
215
+
216
+ ## 🎯 核心概念
217
+
218
+ ### 底层事件(Raw Events)
219
+
220
+ MCPLink 返回最原始的 AI 事件:
221
+
222
+ ```typescript
223
+ type AIStreamEvent =
224
+ | { type: 'text'; content: string }
225
+ | { type: 'tool_call'; toolCall: { id: string; name: string; arguments: Record<string, unknown> } }
226
+ | { type: 'done' }
227
+ | { type: 'error'; error: Error }
228
+ ```
229
+
230
+ **设计哲学**:用户自己处理 AI 响应,框架不做多余的事情。
231
+
232
+ ### 标准事件流(Standard Events)
233
+
234
+ 提供可选的 `toStandardStream` 转换器,将底层事件转换为更丰富的标准事件:
235
+
236
+ ```typescript
237
+ type StandardStreamEvent =
238
+ | { type: 'text_start' }
239
+ | { type: 'text_delta'; content: string }
240
+ | { type: 'text_end' }
241
+ | { type: 'thinking_start' }
242
+ | { type: 'thinking_delta'; content: string }
243
+ | { type: 'thinking_end' }
244
+ | { type: 'tool_call_start'; toolCallId: string; toolName: string; toolArgs: Record<string, unknown> }
245
+ | { type: 'tool_executing'; toolCallId: string; toolName: string }
246
+ | { type: 'tool_result'; toolCallId: string; toolName: string; toolResult: unknown; duration: number; isError?: boolean }
247
+ | { type: 'complete'; totalIterations: number; totalDuration: number }
248
+ | { type: 'error'; error: Error }
249
+ ```
250
+
251
+ ## 🤖 多模型支持
252
+
253
+ MCPLink 使用 OpenAI 兼容格式,支持任意 AI 提供商:
254
+
255
+ ### OpenAI
256
+
257
+ ```typescript
258
+ const mcpLink = new MCPLink({
259
+ ai: {
260
+ baseURL: 'https://api.openai.com/v1',
261
+ apiKey: process.env.OPENAI_API_KEY,
262
+ model: 'gpt-4o',
263
+ },
264
+ })
265
+ ```
266
+
267
+ ### DeepSeek
268
+
269
+ ```typescript
270
+ const mcpLink = new MCPLink({
271
+ ai: {
272
+ baseURL: 'https://api.deepseek.com/v1',
273
+ apiKey: process.env.DEEPSEEK_API_KEY,
274
+ model: 'deepseek-chat',
275
+ },
276
+ })
277
+ ```
278
+
279
+ ### 通义千问 (Qwen)
280
+
281
+ ```typescript
282
+ const mcpLink = new MCPLink({
283
+ ai: {
284
+ baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
285
+ apiKey: process.env.QWEN_API_KEY,
286
+ model: 'qwen3.5-plus',
287
+ enable_thinking: false, // 关闭思考
288
+ },
289
+ })
290
+ ```
291
+
292
+ ### Claude (OpenAI 兼容)
293
+
294
+ ```typescript
295
+ const mcpLink = new MCPLink({
296
+ ai: {
297
+ baseURL: 'https://api.anthropic.com/v1',
298
+ apiKey: process.env.ANTHROPIC_API_KEY,
299
+ model: 'claude-3-5-sonnet',
300
+ },
301
+ })
302
+ ```
303
+
304
+ ### Gemini (OpenAI 兼容)
305
+
306
+ ```typescript
307
+ const mcpLink = new MCPLink({
308
+ ai: {
309
+ baseURL: 'https://generativelanguage.googleapis.com/v1beta/openai',
310
+ apiKey: process.env.GOOGLE_API_KEY,
311
+ model: 'gemini-1.5-flash',
312
+ },
313
+ })
314
+ ```
315
+
316
+ ## 🛠️ API 参考
317
+
318
+ ### MCPLink
319
+
320
+ ```typescript
321
+ class MCPLink {
322
+ constructor(config: MCPLinkConfig)
323
+
324
+ /** 初始化 MCP 连接 */
325
+ async initialize(): Promise<void>
326
+
327
+ /** 关闭所有连接 */
328
+ async close(): Promise<void>
329
+
330
+ /** 发起对话(非流式) */
331
+ async chat(options: ChatOptions): Promise<ChatResult>
332
+
333
+ /** 发起对话(流式,返回底层事件) */
334
+ async chatStream(
335
+ messages: Message[],
336
+ onStream?: (event: AIStreamEvent) => boolean | void
337
+ ): Promise<ChatResult>
338
+
339
+ /** 获取所有可用工具 */
340
+ getTools(): MCPTool[]
341
+
342
+ /** 调用指定工具 */
343
+ async callTool(toolName: string, args: Record<string, unknown>): Promise<unknown>
344
+
345
+ /** 获取 MCP 服务器状态 */
346
+ getMCPServerStatuses(): MCPServerStatus[]
347
+
348
+ /** 启动/停止 MCP 服务器 */
349
+ async startMCPServer(id: string): Promise<void>
350
+ async stopMCPServer(id: string): Promise<void>
351
+ }
352
+ ```
353
+
354
+ ### toStandardStream
355
+
356
+ 将底层事件流转换为标准事件流:
357
+
358
+ ```typescript
359
+ async function* toStandardStream(
360
+ rawStream: AsyncGenerator<AIStreamEvent>,
361
+ options: {
362
+ maxIterations?: number
363
+ executeTool: (name: string, args: Record<string, unknown>) => Promise<unknown>
364
+ onRawEvent?: (event: AIStreamEvent) => boolean | void
365
+ }
366
+ ): AsyncGenerator<StandardStreamEvent>
367
+ ```
368
+
369
+ ### collectStandardResponse
370
+
371
+ 收集流式响应为完整结果(非流式场景):
372
+
373
+ ```typescript
374
+ async function collectStandardResponse(
375
+ stream: AsyncGenerator<StandardStreamEvent>
376
+ ): Promise<{
377
+ content: string
378
+ toolCalls: Array<{
379
+ id: string
380
+ name: string
381
+ arguments: Record<string, unknown>
382
+ result?: unknown
383
+ duration?: number
384
+ isError?: boolean
385
+ }>
386
+ iterations: number
387
+ duration: number
388
+ }>
389
+ ```
390
+
391
+ ## 📋 事件类型对比
392
+
393
+ | 底层事件 | 标准事件 | 说明 |
394
+ |---------|---------|------|
395
+ | `text` | `text_start` / `text_delta` / `text_end` | 文本输出拆分 |
396
+ | `tool_call` | `tool_call_start` / `tool_executing` / `tool_result` | 工具调用拆分 |
397
+ | - | `thinking_start` / `thinking_delta` / `thinking_end` | 思考过程(可选) |
398
+ | `done` | `complete` | 完成事件 |
399
+ | `error` | `error` | 错误事件 |
400
+
401
+ ## 🏗️ 架构设计
402
+
403
+ ```
404
+ ┌─────────────────────────────────────────────────────────────┐
405
+ │ 你的应用 │
406
+ ┌─────────────────┐ ┌─────────────────────────────────┐ │
407
+ │ 底层事件流处理 │ │ 标准事件流处理 │ │
408
+ │ (完全自定义) │ │ (使用 toStandardStream) │ │
409
+ │ └────────┬────────┘ └───────────────┬─────────────────┘ │
410
+ │ │ │ │
411
+ │ ▼ ▼ │
412
+ ┌──────────────────────────────────────────────────────┐ │
413
+ │ MCPLink Core │ │
414
+ │ │ ┌─────────────────────────────────────────────────┐ │ │
415
+ │ │ │ HTTP Client (axios + SSE) │ │ │
416
+ │ │ └─────────────────────┬───────────────────────────┘ │ │
417
+ │ │ │ │
418
+ │ ┌────▼────┐ │ │
419
+ │ │ OpenAI │ ← 可扩展其他适配器 │ │
420
+ │ │ Adapter │ │ │
421
+ │ └────┬────┘ │ │
422
+ │ │ │ │
423
+ │ ┌─────────────────────▼───────────────────────────┐ │ │
424
+ │ │ MCP Manager │ │ │
425
+ │ │ └─────────────────────┬───────────────────────────┘ │ │
426
+ │ └────────────────────────┼──────────────────────────────┘ │
427
+ └──────────────────────────┼──────────────────────────────────┘
428
+
429
+ ┌────────────────┼────────────────┐
430
+ ▼ ▼ ▼
431
+ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
432
+ │ MCP Server │ │ MCP Server │ │ MCP Server │
433
+ │ (stdio) │ │ (streamable-http)│ │ (stdio) │
434
+ └─────────────────┘ └─────────────────┘ └─────────────────┘
435
+ ```
436
+
437
+ **设计原则**:
438
+ - **最小侵入**:只负责 AI 调用和 MCP 工具执行
439
+ - **用户掌控**:用户自己处理响应,维护消息历史
440
+ - **完全透明**:所有 AI 参数原封透传
441
+ - **可选增强**:标准事件流是可选的转换层
442
+
443
+ ## 📦 导出列表
444
+
445
+ ```typescript
446
+ // 核心类
447
+ export { MCPLink } from './MCPLink.js'
448
+ export { MCPManager } from './MCPManager.js'
449
+ export { HttpClient } from './http-client.js'
450
+
451
+ // 适配器
452
+ export { OpenAIAdapter, openaiAdapter } from './adapters/openai.js'
453
+
454
+ // 标准事件流
455
+ export {
456
+ toStandardStream,
457
+ collectStandardResponse,
458
+ type StandardStreamEvent,
459
+ type StandardStreamOptions,
460
+ type StandardResponse,
461
+ } from './standard-stream.js'
462
+
463
+ // 类型
464
+ export type {
465
+ MCPLinkConfig,
466
+ MCPServerConfig,
467
+ AIRequestConfig,
468
+ Message,
469
+ AIStreamEvent,
470
+ ChatResult,
471
+ MCPTool,
472
+ MCPServerStatus,
473
+ } from './types.js'
474
+ ```
475
+
476
+ ## 📝 环境要求
477
+
478
+ - **Node.js**: >= 18.0.0
479
+ - **模块系统**: ES Module
480
+
481
+ ## 🔗 相关链接
482
+
483
+ - [GitHub 仓库](https://github.com/n0tssss/MCPLink)
484
+ - [完整文档](https://github.com/n0tssss/MCPLink#readme)
485
+ - [问题反馈](https://github.com/n0tssss/MCPLink/issues)
486
+ - [MCP 协议规范](https://modelcontextprotocol.io/)
487
+
488
+ ## 📄 许可证
489
+
490
+ MIT License © [n0tssss](https://github.com/n0tssss)