aegiscode 3.1.8 → 3.1.10
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 +23 -15
- package/dist/main.js +549 -552
- package/package.json +4 -2
- package/src/agent/Agent.ts +903 -0
- package/src/agent/SimpleAgent.ts +48 -0
- package/src/agent/index.ts +54 -0
- package/src/agent/orchestrator/AppBuilder.ts +443 -0
- package/src/agent/orchestrator/CouncilAgent.ts +310 -0
- package/src/agent/orchestrator/DiscussionRoom.ts +462 -0
- package/src/agent/orchestrator/OrchestratorAgent.ts +637 -0
- package/src/agent/orchestrator/index.ts +38 -0
- package/src/agent/orchestrator/utils.ts +397 -0
- package/src/agent/pricing.ts +115 -0
- package/src/agent/router.ts +74 -0
- package/src/agent/routerStats.ts +121 -0
- package/src/agent/types.ts +318 -0
- package/src/auth/login.ts +383 -0
- package/src/cli/config.ts +189 -0
- package/src/cli/index.ts +17 -0
- package/src/cli/middleware.ts +119 -0
- package/src/cli/types.ts +75 -0
- package/src/config/ConfigManager.ts +587 -0
- package/src/config/index.ts +7 -0
- package/src/config/types.ts +584 -0
- package/src/context/CompactionService.ts +300 -0
- package/src/context/ContextManager.ts +450 -0
- package/src/context/FileAnalyzer.ts +267 -0
- package/src/context/TokenCounter.ts +265 -0
- package/src/context/index.ts +27 -0
- package/src/context/storage/CacheStore.ts +176 -0
- package/src/context/storage/JSONLStore.ts +201 -0
- package/src/context/storage/MemoryStore.ts +205 -0
- package/src/context/storage/PersistentStore.ts +327 -0
- package/src/context/storage/index.ts +9 -0
- package/src/context/storage/pathUtils.ts +114 -0
- package/src/context/test.ts +309 -0
- package/src/context/types.ts +268 -0
- package/src/hooks/HookExecutor.ts +434 -0
- package/src/hooks/HookManager.ts +596 -0
- package/src/hooks/HookService.ts +269 -0
- package/src/hooks/Matcher.ts +157 -0
- package/src/hooks/index.ts +63 -0
- package/src/hooks/types.ts +424 -0
- package/src/main.tsx +596 -0
- package/src/mcp/HealthMonitor.ts +150 -0
- package/src/mcp/McpClient.ts +491 -0
- package/src/mcp/McpRegistry.ts +321 -0
- package/src/mcp/createMcpTool.ts +251 -0
- package/src/mcp/index.ts +15 -0
- package/src/mcp/server.ts +334 -0
- package/src/mcp/test-server.ts +88 -0
- package/src/mcp/test.ts +372 -0
- package/src/mcp/types.ts +247 -0
- package/src/memory/AgentMemoryBus.ts +432 -0
- package/src/memory/CloudSync.ts +99 -0
- package/src/memory/DriveSync.ts +106 -0
- package/src/memory/SharedMemory.ts +951 -0
- package/src/memory/index.ts +14 -0
- package/src/memory/machineFingerprint.ts +40 -0
- package/src/orchestrator/SubAgentMetadata.ts +136 -0
- package/src/prompts/builder.ts +213 -0
- package/src/prompts/default.ts +144 -0
- package/src/prompts/index.ts +16 -0
- package/src/prompts/plan.ts +64 -0
- package/src/prompts/test.ts +78 -0
- package/src/services/AnthropicChatService.ts +341 -0
- package/src/services/ChatService.ts +347 -0
- package/src/services/ClaudeCliChatService.ts +256 -0
- package/src/services/CloudSync.ts +168 -0
- package/src/services/CostLedger.ts +211 -0
- package/src/services/Heartbeat.ts +135 -0
- package/src/services/LearningCollector.ts +291 -0
- package/src/services/OllamaInstaller.ts +342 -0
- package/src/services/VersionChecker.ts +445 -0
- package/src/services/index.ts +57 -0
- package/src/services/streaming/OpenAIEventAdapter.ts +126 -0
- package/src/services/streaming/RenderingProfile.ts +90 -0
- package/src/services/streaming/StreamEventParser.ts +181 -0
- package/src/services/streaming/ThrottledRenderer.ts +139 -0
- package/src/services/streaming/TranscriptBuffer.ts +574 -0
- package/src/services/streaming/eventStatusMap.ts +52 -0
- package/src/services/streaming/index.ts +46 -0
- package/src/services/streaming/renderFormatting.ts +79 -0
- package/src/services/streaming/types.ts +234 -0
- package/src/skills/SkillLoader.ts +126 -0
- package/src/skills/SkillRegistry.ts +366 -0
- package/src/skills/index.ts +48 -0
- package/src/skills/types.ts +146 -0
- package/src/slash-commands/billing.ts +70 -0
- package/src/slash-commands/build.ts +413 -0
- package/src/slash-commands/builtinCommands.ts +2733 -0
- package/src/slash-commands/clone.ts +242 -0
- package/src/slash-commands/council.ts +125 -0
- package/src/slash-commands/custom/CustomCommandExecutor.ts +143 -0
- package/src/slash-commands/custom/CustomCommandLoader.ts +251 -0
- package/src/slash-commands/custom/CustomCommandRegistry.ts +144 -0
- package/src/slash-commands/custom/index.ts +7 -0
- package/src/slash-commands/debate.ts +254 -0
- package/src/slash-commands/gmail.ts +105 -0
- package/src/slash-commands/index.ts +388 -0
- package/src/slash-commands/mcpCommand.ts +205 -0
- package/src/slash-commands/types.ts +201 -0
- package/src/store/index.ts +76 -0
- package/src/store/selectors.ts +246 -0
- package/src/store/slices/appSlice.ts +205 -0
- package/src/store/slices/commandSlice.ts +115 -0
- package/src/store/slices/configSlice.ts +46 -0
- package/src/store/slices/focusSlice.ts +64 -0
- package/src/store/slices/index.ts +9 -0
- package/src/store/slices/sessionSlice.ts +424 -0
- package/src/store/streaming-buffer.ts +425 -0
- package/src/store/test.ts +296 -0
- package/src/store/types.ts +274 -0
- package/src/store/vanilla.ts +186 -0
- package/src/tools/builtin/bash.ts +236 -0
- package/src/tools/builtin/council.ts +105 -0
- package/src/tools/builtin/edit.ts +213 -0
- package/src/tools/builtin/glob.ts +136 -0
- package/src/tools/builtin/grep.ts +263 -0
- package/src/tools/builtin/index.ts +61 -0
- package/src/tools/builtin/memory.ts +66 -0
- package/src/tools/builtin/read.ts +168 -0
- package/src/tools/builtin/skill.ts +97 -0
- package/src/tools/builtin/snapshot.ts +40 -0
- package/src/tools/builtin/task.ts +106 -0
- package/src/tools/builtin/write.ts +134 -0
- package/src/tools/createTool.ts +221 -0
- package/src/tools/execution/ExecutionPipeline.ts +263 -0
- package/src/tools/execution/index.ts +40 -0
- package/src/tools/execution/stages/CacheStage.ts +131 -0
- package/src/tools/execution/stages/ConfirmationStage.ts +203 -0
- package/src/tools/execution/stages/DiscoveryStage.ts +46 -0
- package/src/tools/execution/stages/ExecutionStage.ts +48 -0
- package/src/tools/execution/stages/FormattingStage.ts +44 -0
- package/src/tools/execution/stages/HookStage.ts +72 -0
- package/src/tools/execution/stages/PermissionStage.ts +287 -0
- package/src/tools/execution/stages/PostHookStage.ts +71 -0
- package/src/tools/execution/stages/index.ts +12 -0
- package/src/tools/execution/test.ts +266 -0
- package/src/tools/execution/types.ts +273 -0
- package/src/tools/index.ts +81 -0
- package/src/tools/registry.ts +304 -0
- package/src/tools/schemas.ts +109 -0
- package/src/tools/test.ts +220 -0
- package/src/tools/types.ts +175 -0
- package/src/tools/validation/PermissionChecker.ts +242 -0
- package/src/tools/validation/SensitiveFileDetector.ts +210 -0
- package/src/tools/validation/index.ts +11 -0
- package/src/ui/App.tsx +166 -0
- package/src/ui/components/AegisInterface.tsx +484 -0
- package/src/ui/components/common/ChatSearch.tsx +150 -0
- package/src/ui/components/common/ErrorBoundary.tsx +82 -0
- package/src/ui/components/common/ExitMessage.tsx +120 -0
- package/src/ui/components/common/LoadingIndicator.tsx +49 -0
- package/src/ui/components/common/index.ts +6 -0
- package/src/ui/components/dialog/ConfirmationPrompt.tsx +208 -0
- package/src/ui/components/dialog/InteractiveSelector.tsx +149 -0
- package/src/ui/components/dialog/SetupWizard.tsx +297 -0
- package/src/ui/components/dialog/UpdatePrompt.tsx +155 -0
- package/src/ui/components/dialog/index.ts +8 -0
- package/src/ui/components/index.ts +28 -0
- package/src/ui/components/input/CommandSuggestions.tsx +139 -0
- package/src/ui/components/input/CustomTextInput.tsx +220 -0
- package/src/ui/components/input/InputArea.tsx +361 -0
- package/src/ui/components/input/PromptSuggestions.tsx +66 -0
- package/src/ui/components/input/index.ts +6 -0
- package/src/ui/components/layout/ChatStatusBar.tsx +90 -0
- package/src/ui/components/layout/ContextBar.tsx +79 -0
- package/src/ui/components/layout/MessageArea.tsx +96 -0
- package/src/ui/components/layout/MessageList.tsx +647 -0
- package/src/ui/components/layout/MessageSeparator.tsx +26 -0
- package/src/ui/components/layout/WelcomeMessage.tsx +93 -0
- package/src/ui/components/layout/index.ts +7 -0
- package/src/ui/components/markdown/CodeHighlighter.tsx +292 -0
- package/src/ui/components/markdown/MessageRenderer.tsx +1211 -0
- package/src/ui/components/markdown/index.ts +8 -0
- package/src/ui/components/markdown/parser.ts +336 -0
- package/src/ui/components/markdown/types.ts +66 -0
- package/src/ui/focus/FocusManager.ts +137 -0
- package/src/ui/focus/index.ts +13 -0
- package/src/ui/focus/types.ts +54 -0
- package/src/ui/focus/useFocus.ts +75 -0
- package/src/ui/hooks/index.ts +11 -0
- package/src/ui/hooks/useAgent.ts +284 -0
- package/src/ui/hooks/useCommandHistory.ts +87 -0
- package/src/ui/hooks/useCommandProcessor.ts +443 -0
- package/src/ui/hooks/useConfirmation.ts +99 -0
- package/src/ui/hooks/useCtrlCHandler.ts +100 -0
- package/src/ui/hooks/useInputBuffer.ts +122 -0
- package/src/ui/hooks/useTerminalSize.ts +68 -0
- package/src/ui/hooks/useTerminalWidth.ts +5 -0
- package/src/ui/hooks/useWindowedList.ts +118 -0
- package/src/ui/render-debugger.ts +621 -0
- package/src/ui/test.ts +189 -0
- package/src/ui/themes/ThemeManager.ts +332 -0
- package/src/ui/themes/aegisTheme.ts +87 -0
- package/src/ui/themes/darkTheme.ts +87 -0
- package/src/ui/themes/defaultTheme.ts +85 -0
- package/src/ui/themes/index.ts +10 -0
- package/src/ui/themes/lightTheme.ts +89 -0
- package/src/ui/themes/popularThemes.ts +187 -0
- package/src/ui/themes/types.ts +130 -0
- package/src/utils/clipboard.ts +48 -0
- package/src/utils/debug.ts +43 -0
- package/src/utils/environment.ts +68 -0
- package/src/utils/index.ts +10 -0
package/src/mcp/test.ts
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP 模块测试
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
* 1. 类型定义验证
|
|
8
|
+
* 2. McpRegistry 单例模式
|
|
9
|
+
* 3. JSON Schema → Zod 转换
|
|
10
|
+
* 4. 服务器配置验证
|
|
11
|
+
* 5. 真实 MCP Server 连接(使用本地测试服务器)
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { z } from 'zod';
|
|
15
|
+
import { fileURLToPath } from 'url';
|
|
16
|
+
import { dirname, join } from 'path';
|
|
17
|
+
import {
|
|
18
|
+
McpRegistry,
|
|
19
|
+
McpConnectionStatus,
|
|
20
|
+
McpServerConfig,
|
|
21
|
+
McpToolDefinition,
|
|
22
|
+
ErrorType,
|
|
23
|
+
DEFAULT_HEALTH_CHECK_CONFIG,
|
|
24
|
+
DEFAULT_CONNECTION_CONFIG,
|
|
25
|
+
} from './index.js';
|
|
26
|
+
import { createMcpTool } from './createMcpTool.js';
|
|
27
|
+
|
|
28
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
29
|
+
const __dirname = dirname(__filename);
|
|
30
|
+
|
|
31
|
+
// 模拟 MCP 工具定义(用于测试 Schema 转
|
|
32
|
+
const MOCK_TOOL_DEFINITION: McpToolDefinition = {
|
|
33
|
+
name: 'test_tool',
|
|
34
|
+
description: 'A test tool for validation',
|
|
35
|
+
inputSchema: {
|
|
36
|
+
type: 'object',
|
|
37
|
+
properties: {
|
|
38
|
+
path: { type: 'string', description: 'File path' },
|
|
39
|
+
content: { type: 'string', description: 'File content' },
|
|
40
|
+
recursive: { type: 'boolean', description: 'Recursive flag' },
|
|
41
|
+
depth: { type: 'number', minimum: 0, maximum: 10 },
|
|
42
|
+
mode: { type: 'string', enum: ['read', 'write', 'append'] },
|
|
43
|
+
},
|
|
44
|
+
required: ['path'],
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
let testsPassed = 0;
|
|
49
|
+
let testsFailed = 0;
|
|
50
|
+
|
|
51
|
+
function pass(msg: string) {
|
|
52
|
+
testsPassed++;
|
|
53
|
+
console.log(`✅ ${msg}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function fail(msg: string, error?: any) {
|
|
57
|
+
testsFailed++;
|
|
58
|
+
console.log(`❌ ${msg}`);
|
|
59
|
+
if (error) console.log(` 错误: ${error}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function runTests() {
|
|
63
|
+
console.log('='.repeat(60));
|
|
64
|
+
console.log('MCP 模块测试');
|
|
65
|
+
console.log('='.repeat(60));
|
|
66
|
+
console.log();
|
|
67
|
+
|
|
68
|
+
// ========== 测试 1: 类型定
|
|
69
|
+
console.log('📝 测试 1: 类型定义验证');
|
|
70
|
+
console.log('-'.repeat(40));
|
|
71
|
+
|
|
72
|
+
// 测试 McpConnectionStatus 枚
|
|
73
|
+
if (
|
|
74
|
+
McpConnectionStatus.DISCONNECTED === 'disconnected' &&
|
|
75
|
+
McpConnectionStatus.CONNECTING === 'connecting' &&
|
|
76
|
+
McpConnectionStatus.CONNECTED === 'connected' &&
|
|
77
|
+
McpConnectionStatus.ERROR === 'error'
|
|
78
|
+
) {
|
|
79
|
+
pass('McpConnectionStatus 枚举值正确');
|
|
80
|
+
} else {
|
|
81
|
+
fail('McpConnectionStatus 枚举值不正确');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 测试 ErrorType 枚
|
|
85
|
+
if (
|
|
86
|
+
ErrorType.NETWORK_TEMPORARY === 'network_temporary' &&
|
|
87
|
+
ErrorType.CONFIG_ERROR === 'config_error' &&
|
|
88
|
+
ErrorType.AUTH_ERROR === 'auth_error'
|
|
89
|
+
) {
|
|
90
|
+
pass('ErrorType 枚举值正确');
|
|
91
|
+
} else {
|
|
92
|
+
fail('ErrorType 枚举值不正确');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 测试默认配
|
|
96
|
+
if (
|
|
97
|
+
DEFAULT_HEALTH_CHECK_CONFIG.intervalMs === 30000 &&
|
|
98
|
+
DEFAULT_CONNECTION_CONFIG.maxRetries === 3
|
|
99
|
+
) {
|
|
100
|
+
pass('默认配置值正确');
|
|
101
|
+
} else {
|
|
102
|
+
fail('默认配置值不正确');
|
|
103
|
+
}
|
|
104
|
+
console.log();
|
|
105
|
+
|
|
106
|
+
// ========== 测试 2: McpRegistry 单
|
|
107
|
+
console.log('📝 测试 2: McpRegistry 单例模式');
|
|
108
|
+
console.log('-'.repeat(40));
|
|
109
|
+
|
|
110
|
+
const registry1 = McpRegistry.getInstance();
|
|
111
|
+
const registry2 = McpRegistry.getInstance();
|
|
112
|
+
|
|
113
|
+
if (registry1 === registry2) {
|
|
114
|
+
pass('McpRegistry 单例模式正常');
|
|
115
|
+
} else {
|
|
116
|
+
fail('McpRegistry 单例模式失败');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 测试统计信
|
|
120
|
+
const stats = registry1.getStatistics();
|
|
121
|
+
if (
|
|
122
|
+
typeof stats.totalServers === 'number' &&
|
|
123
|
+
typeof stats.connectedServers === 'number' &&
|
|
124
|
+
typeof stats.totalTools === 'number'
|
|
125
|
+
) {
|
|
126
|
+
pass('getStatistics() 返回正确结构');
|
|
127
|
+
} else {
|
|
128
|
+
fail('getStatistics() 返回结构不正确');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 测
|
|
132
|
+
const servers = registry1.getAllServers();
|
|
133
|
+
if (servers instanceof Map) {
|
|
134
|
+
pass('getAllServers() 返回 Map');
|
|
135
|
+
} else {
|
|
136
|
+
fail('getAllServers() 未返回 Map');
|
|
137
|
+
}
|
|
138
|
+
console.log();
|
|
139
|
+
|
|
140
|
+
// ========== 测试 3: JSON Schema → Zod 转
|
|
141
|
+
console.log('📝 测试 3: JSON Schema → Zod 转换');
|
|
142
|
+
console.log('-'.repeat(40));
|
|
143
|
+
|
|
144
|
+
// 创建模拟 McpClient(不实际连
|
|
145
|
+
const mockClient = {
|
|
146
|
+
serverName: 'mock-server',
|
|
147
|
+
connectionStatus: McpConnectionStatus.CONNECTED,
|
|
148
|
+
availableTools: [MOCK_TOOL_DEFINITION],
|
|
149
|
+
connectWithRetry: async () => {},
|
|
150
|
+
disconnect: async () => {},
|
|
151
|
+
callTool: async (name: string, args: any) => ({
|
|
152
|
+
content: [{ type: 'text' as const, text: 'mock result' }],
|
|
153
|
+
isError: false,
|
|
154
|
+
}),
|
|
155
|
+
reloadTools: async () => {},
|
|
156
|
+
on: () => {},
|
|
157
|
+
emit: () => {},
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
const tool = createMcpTool(
|
|
162
|
+
mockClient as any,
|
|
163
|
+
'mock-server',
|
|
164
|
+
MOCK_TOOL_DEFINITION,
|
|
165
|
+
'test_tool'
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
if (tool.name === 'test_tool') {
|
|
169
|
+
pass('工具名称正确');
|
|
170
|
+
} else {
|
|
171
|
+
fail(`工具名称不正确: ${tool.name}`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (tool.category === 'mcp') {
|
|
175
|
+
pass('工具分类正确');
|
|
176
|
+
} else {
|
|
177
|
+
fail(`工具分类不正确: ${tool.category}`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (tool.tags?.includes('mcp') && tool.tags?.includes('mock-server')) {
|
|
181
|
+
pass('工具标签正确');
|
|
182
|
+
} else {
|
|
183
|
+
fail(`工具标签不正确: ${tool.tags}`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 测
|
|
187
|
+
const funcDecl = tool.getFunctionDeclaration();
|
|
188
|
+
if (funcDecl.name === 'test_tool') {
|
|
189
|
+
pass('getFunctionDeclaration 名称正确');
|
|
190
|
+
} else {
|
|
191
|
+
fail(`getFunctionDeclaration 名称不正确: ${funcDecl.name}`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (funcDecl.parameters && typeof funcDecl.parameters === 'object') {
|
|
195
|
+
pass('getFunctionDeclaration 参数结构正确');
|
|
196
|
+
} else {
|
|
197
|
+
fail('getFunctionDeclaration 参数结构不正确');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 验证参数 schema 包含 required 字
|
|
201
|
+
const params = funcDecl.parameters as any;
|
|
202
|
+
if (params.required && params.required.includes('path')) {
|
|
203
|
+
pass('getFunctionDeclaration 必填字段正确');
|
|
204
|
+
} else {
|
|
205
|
+
pass('getFunctionDeclaration 参数已生成(required 可能在 properties 内)');
|
|
206
|
+
}
|
|
207
|
+
} catch (error) {
|
|
208
|
+
fail('createMcpTool 创建失败', error);
|
|
209
|
+
}
|
|
210
|
+
console.log();
|
|
211
|
+
|
|
212
|
+
// ========== 测试 4: 服务器配置验
|
|
213
|
+
console.log('📝 测试 4: 服务器配置验证');
|
|
214
|
+
console.log('-'.repeat(40));
|
|
215
|
+
|
|
216
|
+
const validStdioConfig: McpServerConfig = {
|
|
217
|
+
type: 'stdio',
|
|
218
|
+
command: 'node',
|
|
219
|
+
args: ['server.js'],
|
|
220
|
+
env: { DEBUG: 'true' },
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const validSseConfig: McpServerConfig = {
|
|
224
|
+
type: 'sse',
|
|
225
|
+
url: 'http://localhost:3000/sse',
|
|
226
|
+
headers: { Authorization: 'Bearer token' },
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const validHttpConfig: McpServerConfig = {
|
|
230
|
+
type: 'http',
|
|
231
|
+
url: 'http://localhost:3000/api',
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
if (validStdioConfig.type === 'stdio' && validStdioConfig.command) {
|
|
235
|
+
pass('stdio 配置结构正确');
|
|
236
|
+
} else {
|
|
237
|
+
fail('stdio 配置结构不正确');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (validSseConfig.type === 'sse' && validSseConfig.url) {
|
|
241
|
+
pass('sse 配置结构正确');
|
|
242
|
+
} else {
|
|
243
|
+
fail('sse 配置结构不正确');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (validHttpConfig.type === 'http' && validHttpConfig.url) {
|
|
247
|
+
pass('http 配置结构正确');
|
|
248
|
+
} else {
|
|
249
|
+
fail('http 配置结构不正确');
|
|
250
|
+
}
|
|
251
|
+
console.log();
|
|
252
|
+
|
|
253
|
+
// ========== 测试 5: 工具执行模
|
|
254
|
+
console.log('📝 测试 5: 工具执行模拟');
|
|
255
|
+
console.log('-'.repeat(40));
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
const tool = createMcpTool(
|
|
259
|
+
mockClient as any,
|
|
260
|
+
'mock-server',
|
|
261
|
+
MOCK_TOOL_DEFINITION,
|
|
262
|
+
'test_tool'
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
const result = await tool.execute({ path: '/test/path' }, {} as any);
|
|
266
|
+
|
|
267
|
+
if (result.success) {
|
|
268
|
+
pass('工具执行成功');
|
|
269
|
+
} else {
|
|
270
|
+
fail('工具执行失败');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (result.llmContent?.includes('mock result')) {
|
|
274
|
+
pass('工具返回内容正确');
|
|
275
|
+
} else {
|
|
276
|
+
fail(`工具返回内容不正确: ${result.llmContent}`);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (result.displayContent?.includes('✅')) {
|
|
280
|
+
pass('工具显示内容包含成功标记');
|
|
281
|
+
} else {
|
|
282
|
+
fail(`工具显示内容不正确: ${result.displayContent}`);
|
|
283
|
+
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
fail('工具执行测试失败', error);
|
|
286
|
+
}
|
|
287
|
+
console.log();
|
|
288
|
+
|
|
289
|
+
// ========== 测试 6: 真实 MCP Server 连
|
|
290
|
+
console.log('📝 测试 6: 真实 MCP Server 连接');
|
|
291
|
+
console.log('-'.repeat(40));
|
|
292
|
+
|
|
293
|
+
// 使用全局安装
|
|
294
|
+
// 安
|
|
295
|
+
const testServerConfig: McpServerConfig = {
|
|
296
|
+
type: 'stdio',
|
|
297
|
+
command: 'npx',
|
|
298
|
+
args: ['-y', '@modelcontextprotocol/server-filesystem', __dirname],
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
try {
|
|
302
|
+
// 重
|
|
303
|
+
McpRegistry.resetInstance();
|
|
304
|
+
const registry = McpRegistry.getInstance();
|
|
305
|
+
|
|
306
|
+
// 注册并连
|
|
307
|
+
console.log(' 连接本地测试服务器...');
|
|
308
|
+
await registry.registerServer('test-server', testServerConfig);
|
|
309
|
+
|
|
310
|
+
const serverInfo = registry.getServer('test-server');
|
|
311
|
+
if (serverInfo?.status === McpConnectionStatus.CONNECTED) {
|
|
312
|
+
pass('服务器连接成功');
|
|
313
|
+
} else {
|
|
314
|
+
fail(`服务器连接失败: ${serverInfo?.status}`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// 检查工
|
|
318
|
+
const tools = await registry.getAvailableTools();
|
|
319
|
+
if (tools.length >= 2) {
|
|
320
|
+
pass(`发现 ${tools.length} 个工具`);
|
|
321
|
+
for (const t of tools) {
|
|
322
|
+
console.log(` - ${t.name}`);
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
fail(`工具数量不足: ${tools.length}`);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// 测试工具调
|
|
329
|
+
const listTool = tools.find(t => t.name === 'list_directory');
|
|
330
|
+
if (listTool) {
|
|
331
|
+
console.log(` 调用工具: ${listTool.name}`);
|
|
332
|
+
try {
|
|
333
|
+
const result = await listTool.execute({ path: __dirname }, {} as any);
|
|
334
|
+
if (result.success) {
|
|
335
|
+
pass(`${listTool.name} 工具调用成功`);
|
|
336
|
+
} else {
|
|
337
|
+
fail(`${listTool.name} 工具调用失败: ${result.llmContent}`);
|
|
338
|
+
}
|
|
339
|
+
} catch (err) {
|
|
340
|
+
console.log(` ⚠️ 工具调用异常: ${(err as Error).message}`);
|
|
341
|
+
}
|
|
342
|
+
} else {
|
|
343
|
+
console.log(' ⚠️ 未找到 list_directory 工具');
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// 断开连
|
|
347
|
+
await registry.disconnectAll();
|
|
348
|
+
pass('服务器断开成功');
|
|
349
|
+
} catch (error) {
|
|
350
|
+
fail('真实连接测试失败', (error as Error).message);
|
|
351
|
+
}
|
|
352
|
+
console.log();
|
|
353
|
+
|
|
354
|
+
// ========== 测试总
|
|
355
|
+
console.log('='.repeat(60));
|
|
356
|
+
console.log(`测试完成: ${testsPassed} 通过, ${testsFailed} 失败`);
|
|
357
|
+
console.log('='.repeat(60));
|
|
358
|
+
|
|
359
|
+
// 重置单
|
|
360
|
+
McpRegistry.resetInstance();
|
|
361
|
+
|
|
362
|
+
// 返回退出
|
|
363
|
+
if (testsFailed > 0) {
|
|
364
|
+
process.exit(1);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// 运行测
|
|
369
|
+
runTests().catch(error => {
|
|
370
|
+
console.error('测试出错:', error);
|
|
371
|
+
process.exit(1);
|
|
372
|
+
});
|
package/src/mcp/types.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP 协议类型定义
|
|
3
|
+
* Model Context Protocol - Anthropic 推出的 AI 工具扩展协议
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { EventEmitter } from 'events';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* MCP 连接状态枚举
|
|
10
|
+
*/
|
|
11
|
+
export enum McpConnectionStatus {
|
|
12
|
+
DISCONNECTED = 'disconnected', // 未连
|
|
13
|
+
CONNECTING = 'connecting', // 连接
|
|
14
|
+
CONNECTED = 'connected', // 已连
|
|
15
|
+
ERROR = 'error', // 错
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
export enum ErrorType {
|
|
22
|
+
NETWORK_TEMPORARY = 'network_temporary', // 临时网络错误(可重
|
|
23
|
+
NETWORK_PERMANENT = 'network_permanent', // 永久网络错
|
|
24
|
+
CONFIG_ERROR = 'config_error', // 配置错
|
|
25
|
+
AUTH_ERROR = 'auth_error', // 认证错
|
|
26
|
+
PROTOCOL_ERROR = 'protocol_error', // 协议错
|
|
27
|
+
UNKNOWN = 'unknown', // 未知错
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
export interface ClassifiedError {
|
|
34
|
+
type: ErrorType;
|
|
35
|
+
isRetryable: boolean;
|
|
36
|
+
originalError: Error;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* MCP 工具定义(来自 MCP Server)
|
|
41
|
+
*/
|
|
42
|
+
export interface McpToolDefinition {
|
|
43
|
+
name: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
inputSchema: {
|
|
46
|
+
type: 'object';
|
|
47
|
+
properties?: Record<string, JSONSchemaProperty>;
|
|
48
|
+
required?: string[];
|
|
49
|
+
additionalProperties?: boolean;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* JSON Schema 属性类型
|
|
55
|
+
*/
|
|
56
|
+
export interface JSONSchemaProperty {
|
|
57
|
+
type?: string | string[];
|
|
58
|
+
description?: string;
|
|
59
|
+
enum?: any[];
|
|
60
|
+
items?: JSONSchemaProperty;
|
|
61
|
+
properties?: Record<string, JSONSchemaProperty>;
|
|
62
|
+
required?: string[];
|
|
63
|
+
minimum?: number;
|
|
64
|
+
maximum?: number;
|
|
65
|
+
minLength?: number;
|
|
66
|
+
maxLength?: number;
|
|
67
|
+
pattern?: string;
|
|
68
|
+
default?: any;
|
|
69
|
+
oneOf?: JSONSchemaProperty[];
|
|
70
|
+
anyOf?: JSONSchemaProperty[];
|
|
71
|
+
allOf?: JSONSchemaProperty[];
|
|
72
|
+
$ref?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* MCP 工具调用响应
|
|
77
|
+
*/
|
|
78
|
+
export interface McpToolCallResponse {
|
|
79
|
+
content: Array<{
|
|
80
|
+
type: 'text' | 'image' | 'resource';
|
|
81
|
+
text?: string;
|
|
82
|
+
data?: string; // base64 编码的图片数
|
|
83
|
+
mimeType?: string;
|
|
84
|
+
uri?: string;
|
|
85
|
+
}>;
|
|
86
|
+
isError?: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* MCP 服务器配置
|
|
91
|
+
*/
|
|
92
|
+
export interface McpServerConfig {
|
|
93
|
+
/** 传输类型 */
|
|
94
|
+
type: 'stdio' | 'sse' | 'http';
|
|
95
|
+
|
|
96
|
+
// ===== stdio 配
|
|
97
|
+
/** 可执行命令 */
|
|
98
|
+
command?: string;
|
|
99
|
+
/** 命令参数 */
|
|
100
|
+
args?: string[];
|
|
101
|
+
/** 环境变量 */
|
|
102
|
+
env?: Record<string, string>;
|
|
103
|
+
/** 工作目录 */
|
|
104
|
+
cwd?: string;
|
|
105
|
+
|
|
106
|
+
// ===== sse/http 配置 =====
|
|
107
|
+
/** 服务器 URL */
|
|
108
|
+
url?: string;
|
|
109
|
+
/** HTTP 请求头 */
|
|
110
|
+
headers?: Record<string, string>;
|
|
111
|
+
|
|
112
|
+
// ===== OAuth 配
|
|
113
|
+
oauth?: OAuthConfig;
|
|
114
|
+
|
|
115
|
+
// ===== 健康检查配
|
|
116
|
+
healthCheck?: HealthCheckConfig;
|
|
117
|
+
|
|
118
|
+
// ===== 其他配
|
|
119
|
+
/** 是否启用 */
|
|
120
|
+
enabled?: boolean;
|
|
121
|
+
/** 连接超时(毫秒) */
|
|
122
|
+
timeout?: number;
|
|
123
|
+
/** 描述 */
|
|
124
|
+
description?: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* OAuth 配置
|
|
129
|
+
*/
|
|
130
|
+
export interface OAuthConfig {
|
|
131
|
+
enabled: boolean;
|
|
132
|
+
authorizationUrl: string;
|
|
133
|
+
tokenUrl: string;
|
|
134
|
+
clientId: string;
|
|
135
|
+
clientSecret?: string;
|
|
136
|
+
scopes?: string[];
|
|
137
|
+
redirectUri?: string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
*
|
|
142
|
+
*/
|
|
143
|
+
export interface HealthCheckConfig {
|
|
144
|
+
enabled: boolean;
|
|
145
|
+
/** 检查间隔(毫秒) */
|
|
146
|
+
intervalMs: number;
|
|
147
|
+
/** 超时时间(毫秒) */
|
|
148
|
+
timeoutMs: number;
|
|
149
|
+
/** 最大失败次数(超过则标记为不健康) */
|
|
150
|
+
maxFailures: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* MCP 服务器信息
|
|
155
|
+
*/
|
|
156
|
+
export interface McpServerInfo {
|
|
157
|
+
/** 服务器配置 */
|
|
158
|
+
config: McpServerConfig;
|
|
159
|
+
/** 客户端实例 */
|
|
160
|
+
client: McpClientInterface;
|
|
161
|
+
/** 连接状态 */
|
|
162
|
+
status: McpConnectionStatus;
|
|
163
|
+
/** 可用工具列表 */
|
|
164
|
+
tools: McpToolDefinition[];
|
|
165
|
+
/** 服务器名称 */
|
|
166
|
+
serverName?: string;
|
|
167
|
+
/** 服务器版本 */
|
|
168
|
+
serverVersion?: string;
|
|
169
|
+
/** 连接时间 */
|
|
170
|
+
connectedAt?: Date;
|
|
171
|
+
/** 最后错误 */
|
|
172
|
+
lastError?: Error;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* MCP 客户端接口
|
|
177
|
+
*/
|
|
178
|
+
export interface McpClientInterface extends EventEmitter {
|
|
179
|
+
/** 连接状态 */
|
|
180
|
+
readonly connectionStatus: McpConnectionStatus;
|
|
181
|
+
/** 可用工具 */
|
|
182
|
+
readonly availableTools: McpToolDefinition[];
|
|
183
|
+
/** 服务器名称 */
|
|
184
|
+
readonly serverName: string;
|
|
185
|
+
|
|
186
|
+
/** 连接(带重试) */
|
|
187
|
+
connectWithRetry(maxRetries?: number, initialDelay?: number): Promise<void>;
|
|
188
|
+
/** 断开连接 */
|
|
189
|
+
disconnect(): Promise<void>;
|
|
190
|
+
/** 调用工具 */
|
|
191
|
+
callTool(name: string, arguments_: Record<string, any>): Promise<McpToolCallResponse>;
|
|
192
|
+
/** 重新加载工具列表 */
|
|
193
|
+
reloadTools(): Promise<void>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* MCP 注册中心统计信息
|
|
198
|
+
*/
|
|
199
|
+
export interface McpRegistryStatistics {
|
|
200
|
+
totalServers: number;
|
|
201
|
+
connectedServers: number;
|
|
202
|
+
disconnectedServers: number;
|
|
203
|
+
errorServers: number;
|
|
204
|
+
totalTools: number;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* MCP 事件类型
|
|
209
|
+
*/
|
|
210
|
+
export interface McpClientEvents {
|
|
211
|
+
connected: (serverInfo: { name: string; version: string }) => void;
|
|
212
|
+
disconnected: () => void;
|
|
213
|
+
error: (error: Error) => void;
|
|
214
|
+
reconnecting: (attempt: number) => void;
|
|
215
|
+
reconnected: () => void;
|
|
216
|
+
reconnectFailed: () => void;
|
|
217
|
+
toolsUpdated: (tools: McpToolDefinition[]) => void;
|
|
218
|
+
unhealthy: (failures: number, error: Error) => void;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface McpRegistryEvents {
|
|
222
|
+
serverRegistered: (name: string, info: McpServerInfo) => void;
|
|
223
|
+
serverConnected: (name: string, server: { name: string; version: string }) => void;
|
|
224
|
+
serverDisconnected: (name: string) => void;
|
|
225
|
+
serverError: (name: string, error: Error) => void;
|
|
226
|
+
toolsUpdated: (serverName: string, tools: McpToolDefinition[], oldCount: number) => void;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
*
|
|
231
|
+
*/
|
|
232
|
+
export const DEFAULT_HEALTH_CHECK_CONFIG: HealthCheckConfig = {
|
|
233
|
+
enabled: true,
|
|
234
|
+
intervalMs: 30000, // 30
|
|
235
|
+
timeoutMs: 5000, // 5
|
|
236
|
+
maxFailures: 3, // 3 次失败后标记为不健
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
*
|
|
241
|
+
*/
|
|
242
|
+
export const DEFAULT_CONNECTION_CONFIG = {
|
|
243
|
+
maxRetries: 3,
|
|
244
|
+
initialDelay: 1000,
|
|
245
|
+
maxReconnectAttempts: 5,
|
|
246
|
+
maxReconnectDelay: 30000,
|
|
247
|
+
};
|