snow-flow 2.0.2 → 2.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/{.claude/mcp-config.json → .mcp.json} +13 -12
- package/.releaserc.json +47 -0
- package/README.md +6 -0
- package/package.json +1 -1
- package/.claude/claude-settings.json +0 -15
- package/.claude/commands/analysis/README.md +0 -9
- package/.claude/commands/automation/README.md +0 -9
- package/.claude/commands/github/README.md +0 -11
- package/.claude/commands/helpers/standard-checkpoint-hooks.sh +0 -179
- package/.claude/commands/hooks/README.md +0 -11
- package/.claude/commands/monitoring/README.md +0 -9
- package/.claude/commands/optimization/README.md +0 -9
- package/.claude/commands/training/README.md +0 -9
- package/.claude/commands/workflows/README.md +0 -9
- package/.claude/config.json +0 -36
- package/.claude/helpers/github-setup.sh +0 -28
- package/.claude/helpers/quick-start.sh +0 -19
- package/.claude/helpers/setup-mcp.sh +0 -18
- package/.claude/settings.json +0 -114
- package/.claude/settings.local.json +0 -33
- package/.claude-flow/metrics/agent-metrics.json +0 -1
- package/.claude-flow/metrics/performance.json +0 -9
- package/.claude-flow/metrics/system-metrics.json +0 -15986
- package/.claude-flow/metrics/task-metrics.json +0 -10
- package/.roo/README.md +0 -402
- package/.roo/mcp-list.txt +0 -257
- package/.roo/mcp.json +0 -32
- package/.roo/workflows/basic-tdd.json +0 -32
- package/.roomodes +0 -201
- package/CLAUDE.md +0 -393
- package/bin/snow-flow.js +0 -3
- package/dist/cli.js.bak +0 -2052
- package/dist/config/snow-flow-config.d.ts +0 -1492
- package/dist/config/snow-flow-config.js +0 -938
- package/scripts/create-env.js +0 -91
- package/scripts/postinstall.js +0 -30
- package/scripts/register-mcp-servers.js +0 -210
- package/scripts/reset-mcp-servers.js +0 -246
- package/scripts/setup-mcp.js +0 -106
- package/scripts/update-version.js +0 -31
- package/src/agents/README.md +0 -192
- package/src/agents/base-agent.ts +0 -251
- package/src/agents/coordinator.ts +0 -971
- package/src/agents/index.ts +0 -18
- package/src/agents/queen-403-handler.ts +0 -230
- package/src/agents/queen-agent.ts +0 -1399
- package/src/agents/script-writer-agent.ts +0 -769
- package/src/agents/security-agent.ts +0 -726
- package/src/agents/widget-creator-agent.ts +0 -568
- package/src/api/error-handling.ts +0 -1758
- package/src/api/natural-language-mapper.ts +0 -630
- package/src/api/performance-optimizer.ts +0 -891
- package/src/api/transaction-manager.ts +0 -754
- package/src/cli/deploy-artifact.ts +0 -304
- package/src/cli/snow-flow-cli-integration.ts +0 -434
- package/src/cli.ts +0 -4455
- package/src/cli.ts.bak +0 -5616
- package/src/compliance/advanced-compliance-system.ts +0 -1268
- package/src/compliance/index.ts +0 -19
- package/src/config/snow-flow-config.ts +0 -1019
- package/src/documentation/index.ts +0 -16
- package/src/documentation/self-documenting-system.ts +0 -1421
- package/src/dynamic-version.ts +0 -44
- package/src/healing/index.ts +0 -20
- package/src/healing/self-healing-system.ts +0 -1435
- package/src/health/README.md +0 -161
- package/src/health/system-health.ts +0 -995
- package/src/index.ts +0 -60
- package/src/intelligence/acl-analyzer.ts +0 -377
- package/src/intelligence/auto-resolution-engine.ts +0 -643
- package/src/intelligence/gap-analysis-engine.ts +0 -488
- package/src/intelligence/manual-instructions-generator.ts +0 -744
- package/src/intelligence/mcp-coverage-analyzer.ts +0 -708
- package/src/intelligence/multi-pass-requirements-analyzer.ts +0 -603
- package/src/intelligence/performance-recommendations-engine.ts +0 -1223
- package/src/intelligence/requirements-analyzer.ts +0 -1148
- package/src/intelligence/task-analyzer.ts +0 -628
- package/src/managers/scope-manager.ts +0 -555
- package/src/mcp/advanced/servicenow-advanced-features-mcp.ts +0 -16110
- package/src/mcp/base-mcp-server.ts +0 -660
- package/src/mcp/http-transport-wrapper.ts +0 -426
- package/src/mcp/service-discovery-client.ts +0 -312
- package/src/mcp/servicenow-automation-mcp-refactored.ts +0 -678
- package/src/mcp/servicenow-automation-mcp.ts +0 -838
- package/src/mcp/servicenow-deployment-mcp-refactored.ts +0 -1292
- package/src/mcp/servicenow-deployment-mcp.ts +0 -7244
- package/src/mcp/servicenow-graph-memory-mcp-refactored.ts +0 -721
- package/src/mcp/servicenow-graph-memory-mcp.ts +0 -832
- package/src/mcp/servicenow-integration-mcp-refactored.ts +0 -655
- package/src/mcp/servicenow-integration-mcp.ts +0 -819
- package/src/mcp/servicenow-intelligent-mcp-refactored.ts +0 -977
- package/src/mcp/servicenow-intelligent-mcp.ts +0 -4638
- package/src/mcp/servicenow-mcp-server.ts +0 -728
- package/src/mcp/servicenow-memory-mcp.ts +0 -474
- package/src/mcp/servicenow-operations-mcp-refactored.ts +0 -1995
- package/src/mcp/servicenow-operations-mcp.ts +0 -3384
- package/src/mcp/servicenow-platform-development-mcp-refactored.ts +0 -611
- package/src/mcp/servicenow-platform-development-mcp.ts +0 -970
- package/src/mcp/servicenow-progressive-indexer.ts +0 -370
- package/src/mcp/servicenow-reporting-analytics-mcp-refactored.ts +0 -695
- package/src/mcp/servicenow-reporting-analytics-mcp.ts +0 -1123
- package/src/mcp/servicenow-security-compliance-mcp-refactored.ts +0 -642
- package/src/mcp/servicenow-security-compliance-mcp.ts +0 -1157
- package/src/mcp/servicenow-update-set-mcp-refactored.ts +0 -751
- package/src/mcp/servicenow-update-set-mcp.ts +0 -811
- package/src/mcp/shared/agent-context-provider.ts +0 -365
- package/src/mcp/shared/base-mcp-server.ts +0 -2463
- package/src/mcp/shared/mcp-memory-manager.ts +0 -540
- package/src/mcp/shared/mcp-resource-manager.ts +0 -367
- package/src/mcp/snow-flow-mcp.ts +0 -911
- package/src/mcp/start-all-mcp-servers.ts +0 -102
- package/src/mcp/start-servicenow-mcp.ts +0 -67
- package/src/monitoring/enhanced-monitoring-system.ts +0 -1345
- package/src/monitoring/performance-tracker.ts +0 -777
- package/src/optimization/cost-optimization-engine.ts +0 -1125
- package/src/optimization/index.ts +0 -10
- package/src/queen/README.md +0 -403
- package/src/queen/agent-factory.ts +0 -574
- package/src/queen/index.ts +0 -94
- package/src/queen/mcp-execution-bridge.ts +0 -681
- package/src/queen/neural-learning.ts +0 -319
- package/src/queen/parallel-agent-engine.ts +0 -848
- package/src/queen/queen-memory-system.ts +0 -490
- package/src/queen/queen-memory.ts +0 -456
- package/src/queen/servicenow-queen.ts +0 -1029
- package/src/queen/types.ts +0 -87
- package/src/rollback/smart-rollback-system.ts +0 -866
- package/src/schemas/deployment.schema.json +0 -58
- package/src/schemas/flow.schema.json +0 -79
- package/src/schemas/widget.schema.json +0 -72
- package/src/snow-flow-system.ts +0 -541
- package/src/sparc/sparc-help.ts +0 -49
- package/src/sparc/team-sparc.ts +0 -88
- package/src/strategies/global-scope-strategy.ts +0 -736
- package/src/templates/base/application.template.json +0 -45
- package/src/templates/base/business_rule.template.json +0 -33
- package/src/templates/base/script_include.template.json +0 -18
- package/src/templates/base/table.template.json +0 -64
- package/src/templates/base/widget.template.json +0 -25
- package/src/templates/patterns/composite.incident-management.template.json +0 -140
- package/src/templates/patterns/widget.dashboard.template.json +0 -238
- package/src/templates/patterns/widget.datatable.template.json +0 -292
- package/src/testing/integration-test-suite.ts +0 -1276
- package/src/types/index.ts +0 -183
- package/src/types/neo4j-driver.d.ts +0 -23
- package/src/types/servicenow.types.ts +0 -307
- package/src/types/snow-flow.types.ts +0 -186
- package/src/types/todo.types.ts +0 -70
- package/src/utils/action-type-cache.ts +0 -286
- package/src/utils/agent-detector.ts +0 -573
- package/src/utils/artifact-tracker.ts +0 -379
- package/src/utils/dependency-detector.ts +0 -233
- package/src/utils/deployment-metadata-handler.ts +0 -518
- package/src/utils/error-recovery.ts +0 -680
- package/src/utils/logger.ts +0 -60
- package/src/utils/mcp-auth-middleware.ts +0 -262
- package/src/utils/mcp-config-manager.ts +0 -270
- package/src/utils/mcp-server-manager.ts +0 -446
- package/src/utils/mcp-tool-registry.ts +0 -298
- package/src/utils/migrate-snow-flow.ts +0 -98
- package/src/utils/oauth-html-templates.ts +0 -188
- package/src/utils/scope-utils.ts +0 -443
- package/src/utils/servicenow-client.ts +0 -3321
- package/src/utils/servicenow-id-generator.ts +0 -181
- package/src/utils/snow-memory-manager.ts +0 -605
- package/src/utils/snow-oauth.ts +0 -797
- package/src/utils/template-engine.ts +0 -463
- package/src/utils/theme-manager.ts +0 -295
- package/src/utils/unified-auth-store.ts +0 -222
- package/src/utils/update-set-importer.ts +0 -422
- package/src/utils/update-set-xml-packager.ts +0 -371
- package/src/utils/widget-template-generator.ts +0 -1744
- package/src/version.ts +0 -1950
|
@@ -1,681 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MCP Execution Bridge
|
|
3
|
-
*
|
|
4
|
-
* Bridges the gap between agent recommendations and actual MCP tool execution
|
|
5
|
-
* Enables Queen Agent and specialized agents to directly execute ServiceNow operations
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { EventEmitter } from 'eventemitter3';
|
|
9
|
-
import { ServiceNowOAuth } from '../utils/snow-oauth.js';
|
|
10
|
-
import { QueenMemorySystem } from './queen-memory.js';
|
|
11
|
-
import { Logger } from '../utils/logger.js';
|
|
12
|
-
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
13
|
-
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
14
|
-
import { spawn, ChildProcess } from 'child_process';
|
|
15
|
-
|
|
16
|
-
export interface AgentRecommendation {
|
|
17
|
-
agentId: string;
|
|
18
|
-
agentType: string;
|
|
19
|
-
action: string;
|
|
20
|
-
tool: string;
|
|
21
|
-
server: string;
|
|
22
|
-
params: any;
|
|
23
|
-
reasoning: string;
|
|
24
|
-
confidence: number;
|
|
25
|
-
dependencies?: string[];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface MCPExecutionResult {
|
|
29
|
-
success: boolean;
|
|
30
|
-
recommendation: AgentRecommendation;
|
|
31
|
-
toolResult?: any;
|
|
32
|
-
error?: string;
|
|
33
|
-
executionTime: number;
|
|
34
|
-
retries?: number;
|
|
35
|
-
fallbackUsed?: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface MCPServerConfig {
|
|
39
|
-
name: string;
|
|
40
|
-
command: string;
|
|
41
|
-
args?: string[];
|
|
42
|
-
env?: Record<string, string>;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Maps agent recommendations to MCP tool calls and executes them
|
|
47
|
-
*/
|
|
48
|
-
export class MCPExecutionBridge extends EventEmitter {
|
|
49
|
-
private mcpClients: Map<string, Client> = new Map();
|
|
50
|
-
private mcpProcesses: Map<string, ChildProcess> = new Map();
|
|
51
|
-
private sessionAuth: ServiceNowOAuth;
|
|
52
|
-
private memory: QueenMemorySystem;
|
|
53
|
-
private logger: Logger;
|
|
54
|
-
|
|
55
|
-
// MCP server configurations
|
|
56
|
-
private serverConfigs: Map<string, MCPServerConfig> = new Map([
|
|
57
|
-
['deployment', {
|
|
58
|
-
name: 'servicenow-deployment',
|
|
59
|
-
command: 'node',
|
|
60
|
-
args: ['dist/mcp/servicenow-deployment-mcp.js']
|
|
61
|
-
}],
|
|
62
|
-
['intelligent', {
|
|
63
|
-
name: 'servicenow-intelligent',
|
|
64
|
-
command: 'node',
|
|
65
|
-
args: ['dist/mcp/servicenow-intelligent-mcp.js']
|
|
66
|
-
}],
|
|
67
|
-
['operations', {
|
|
68
|
-
name: 'servicenow-operations',
|
|
69
|
-
command: 'node',
|
|
70
|
-
args: ['dist/mcp/servicenow-operations-mcp.js']
|
|
71
|
-
}],
|
|
72
|
-
['update-set', {
|
|
73
|
-
name: 'servicenow-update-set',
|
|
74
|
-
command: 'node',
|
|
75
|
-
args: ['dist/mcp/servicenow-update-set-mcp.js']
|
|
76
|
-
}],
|
|
77
|
-
]);
|
|
78
|
-
|
|
79
|
-
// Tool to server mapping
|
|
80
|
-
private toolServerMap: Map<string, string> = new Map([
|
|
81
|
-
// Deployment tools
|
|
82
|
-
['snow_deploy', 'deployment'],
|
|
83
|
-
['snow_deploy_widget', 'deployment'],
|
|
84
|
-
['snow_deploy_flow', 'deployment'],
|
|
85
|
-
['snow_preview_widget', 'deployment'],
|
|
86
|
-
['snow_widget_test', 'deployment'],
|
|
87
|
-
|
|
88
|
-
// Intelligent tools
|
|
89
|
-
['snow_find_artifact', 'intelligent'],
|
|
90
|
-
['snow_edit_artifact', 'intelligent'],
|
|
91
|
-
['snow_comprehensive_search', 'intelligent'],
|
|
92
|
-
['snow_analyze_requirements', 'intelligent'],
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// Operations tools
|
|
96
|
-
['snow_catalog_item_search', 'operations'],
|
|
97
|
-
['snow_test_flow_with_mock', 'operations'],
|
|
98
|
-
['snow_link_catalog_to_flow', 'operations'],
|
|
99
|
-
|
|
100
|
-
// Update set tools
|
|
101
|
-
['snow_update_set_create', 'update-set'],
|
|
102
|
-
['snow_update_set_switch', 'update-set'],
|
|
103
|
-
['snow_update_set_add_artifact', 'update-set'],
|
|
104
|
-
]);
|
|
105
|
-
|
|
106
|
-
constructor(memory?: QueenMemorySystem) {
|
|
107
|
-
super();
|
|
108
|
-
this.sessionAuth = new ServiceNowOAuth();
|
|
109
|
-
this.memory = memory || new QueenMemorySystem();
|
|
110
|
-
this.logger = new Logger('MCPExecutionBridge');
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Execute an agent recommendation by calling the appropriate MCP tool
|
|
115
|
-
*/
|
|
116
|
-
async executeAgentRecommendation(
|
|
117
|
-
agent: { id: string; type: string },
|
|
118
|
-
recommendation: AgentRecommendation
|
|
119
|
-
): Promise<MCPExecutionResult> {
|
|
120
|
-
const startTime = Date.now();
|
|
121
|
-
|
|
122
|
-
try {
|
|
123
|
-
this.logger.info(`Executing recommendation from ${agent.type} agent:`, {
|
|
124
|
-
tool: recommendation.tool,
|
|
125
|
-
action: recommendation.action,
|
|
126
|
-
confidence: recommendation.confidence
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
// 1. Map recommendation to MCP tool
|
|
130
|
-
const toolMapping = await this.mapRecommendationToTool(recommendation);
|
|
131
|
-
|
|
132
|
-
// 2. Ensure MCP client is connected
|
|
133
|
-
const client = await this.ensureMCPClient(toolMapping.server);
|
|
134
|
-
|
|
135
|
-
// 3. Execute tool with session authentication
|
|
136
|
-
const toolResult = await this.executeToolWithAuth(
|
|
137
|
-
client,
|
|
138
|
-
toolMapping.tool,
|
|
139
|
-
toolMapping.params
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
// 4. Store result in shared memory for other agents
|
|
143
|
-
await this.storeExecutionResult(agent.id, recommendation, toolResult);
|
|
144
|
-
|
|
145
|
-
// 5. Emit execution event for monitoring
|
|
146
|
-
this.emit('execution:complete', {
|
|
147
|
-
agent,
|
|
148
|
-
recommendation,
|
|
149
|
-
result: toolResult,
|
|
150
|
-
executionTime: Date.now() - startTime
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
return {
|
|
154
|
-
success: true,
|
|
155
|
-
recommendation,
|
|
156
|
-
toolResult: toolResult.result,
|
|
157
|
-
executionTime: Date.now() - startTime
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
} catch (error) {
|
|
161
|
-
this.logger.error('Execution failed:', error);
|
|
162
|
-
|
|
163
|
-
// Try fallback strategies
|
|
164
|
-
const fallbackResult = await this.tryFallbackStrategies(
|
|
165
|
-
agent,
|
|
166
|
-
recommendation,
|
|
167
|
-
error
|
|
168
|
-
);
|
|
169
|
-
|
|
170
|
-
if (fallbackResult.success) {
|
|
171
|
-
return fallbackResult;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Store failure for learning
|
|
175
|
-
await this.storeExecutionFailure(agent.id, recommendation, error);
|
|
176
|
-
|
|
177
|
-
return {
|
|
178
|
-
success: false,
|
|
179
|
-
recommendation,
|
|
180
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
181
|
-
executionTime: Date.now() - startTime,
|
|
182
|
-
fallbackUsed: true
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Map agent recommendation to specific MCP tool
|
|
189
|
-
*/
|
|
190
|
-
private async mapRecommendationToTool(
|
|
191
|
-
recommendation: AgentRecommendation
|
|
192
|
-
): Promise<{ server: string; tool: string; params: any }> {
|
|
193
|
-
// Direct tool mapping if specified
|
|
194
|
-
if (recommendation.tool && recommendation.server) {
|
|
195
|
-
return {
|
|
196
|
-
server: recommendation.server,
|
|
197
|
-
tool: recommendation.tool,
|
|
198
|
-
params: recommendation.params
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// Intelligent mapping based on action
|
|
203
|
-
const mapping = await this.intelligentToolMapping(recommendation);
|
|
204
|
-
|
|
205
|
-
// Store mapping decision for learning
|
|
206
|
-
await this.memory.store(`tool_mapping_${Date.now()}`, {
|
|
207
|
-
recommendation,
|
|
208
|
-
mapping,
|
|
209
|
-
timestamp: new Date().toISOString()
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
return mapping;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Intelligent tool mapping based on action description
|
|
217
|
-
*/
|
|
218
|
-
private async intelligentToolMapping(
|
|
219
|
-
recommendation: AgentRecommendation
|
|
220
|
-
): Promise<{ server: string; tool: string; params: any }> {
|
|
221
|
-
const action = recommendation.action.toLowerCase();
|
|
222
|
-
|
|
223
|
-
// Widget-related actions
|
|
224
|
-
if (action.includes('widget') || action.includes('portal')) {
|
|
225
|
-
if (action.includes('deploy') || action.includes('create')) {
|
|
226
|
-
return {
|
|
227
|
-
server: 'deployment',
|
|
228
|
-
tool: 'snow_deploy',
|
|
229
|
-
params: { type: 'widget', config: recommendation.params }
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
if (action.includes('test')) {
|
|
233
|
-
return {
|
|
234
|
-
server: 'deployment',
|
|
235
|
-
tool: 'snow_widget_test',
|
|
236
|
-
params: recommendation.params
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// Flow-related actions (testing only - creation not supported)
|
|
242
|
-
if (action.includes('flow') || action.includes('workflow')) {
|
|
243
|
-
if (action.includes('test')) {
|
|
244
|
-
return {
|
|
245
|
-
server: 'operations',
|
|
246
|
-
tool: 'snow_test_flow_with_mock',
|
|
247
|
-
params: recommendation.params
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// Search/discovery actions
|
|
253
|
-
if (action.includes('find') || action.includes('search') || action.includes('discover')) {
|
|
254
|
-
return {
|
|
255
|
-
server: 'intelligent',
|
|
256
|
-
tool: 'snow_find_artifact',
|
|
257
|
-
params: {
|
|
258
|
-
query: recommendation.params.query || recommendation.action,
|
|
259
|
-
type: recommendation.params.type || 'any'
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Update set actions
|
|
265
|
-
if (action.includes('update set') || action.includes('track')) {
|
|
266
|
-
if (action.includes('create')) {
|
|
267
|
-
return {
|
|
268
|
-
server: 'update-set',
|
|
269
|
-
tool: 'snow_update_set_create',
|
|
270
|
-
params: recommendation.params
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
if (action.includes('add') || action.includes('track')) {
|
|
274
|
-
return {
|
|
275
|
-
server: 'update-set',
|
|
276
|
-
tool: 'snow_update_set_add_artifact',
|
|
277
|
-
params: recommendation.params
|
|
278
|
-
};
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// Default fallback - try intelligent search
|
|
283
|
-
return {
|
|
284
|
-
server: 'intelligent',
|
|
285
|
-
tool: 'snow_find_artifact',
|
|
286
|
-
params: {
|
|
287
|
-
query: recommendation.action,
|
|
288
|
-
type: 'any'
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Ensure MCP client is connected and ready
|
|
295
|
-
*/
|
|
296
|
-
private async ensureMCPClient(serverName: string): Promise<Client> {
|
|
297
|
-
// Check if client already exists and is connected
|
|
298
|
-
const existingClient = this.mcpClients.get(serverName);
|
|
299
|
-
if (existingClient) {
|
|
300
|
-
return existingClient;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// Get server configuration
|
|
304
|
-
const config = this.serverConfigs.get(serverName);
|
|
305
|
-
if (!config) {
|
|
306
|
-
throw new Error(`Unknown MCP server: ${serverName}`);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// Spawn MCP server process
|
|
310
|
-
const serverProcess = spawn(config.command, config.args || [], {
|
|
311
|
-
env: { ...process.env, ...config.env },
|
|
312
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
this.mcpProcesses.set(serverName, serverProcess);
|
|
316
|
-
|
|
317
|
-
// Create MCP client
|
|
318
|
-
const transport = new StdioClientTransport({
|
|
319
|
-
command: config.command,
|
|
320
|
-
args: config.args,
|
|
321
|
-
env: { ...process.env, ...config.env },
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
const client = new Client({
|
|
325
|
-
name: `queen-agent-${serverName}`,
|
|
326
|
-
version: '1.0.0',
|
|
327
|
-
}, {
|
|
328
|
-
capabilities: {}
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
// Connect to server
|
|
332
|
-
await client.connect(transport);
|
|
333
|
-
|
|
334
|
-
// Store client
|
|
335
|
-
this.mcpClients.set(serverName, client);
|
|
336
|
-
|
|
337
|
-
this.logger.info(`Connected to MCP server: ${serverName}`);
|
|
338
|
-
|
|
339
|
-
return client;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Execute tool with authentication
|
|
344
|
-
*/
|
|
345
|
-
private async executeToolWithAuth(
|
|
346
|
-
client: Client,
|
|
347
|
-
tool: string,
|
|
348
|
-
params: any
|
|
349
|
-
): Promise<any> {
|
|
350
|
-
// Ensure we have valid authentication
|
|
351
|
-
const isAuthenticated = await this.sessionAuth.isAuthenticated();
|
|
352
|
-
if (!isAuthenticated) {
|
|
353
|
-
throw new Error('Not authenticated with ServiceNow. Run "snow-flow auth login" first.');
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// Call the tool
|
|
357
|
-
const result = await client.callTool({
|
|
358
|
-
name: tool,
|
|
359
|
-
arguments: params
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
return result;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Store execution result in shared memory
|
|
367
|
-
*/
|
|
368
|
-
private async storeExecutionResult(
|
|
369
|
-
agentId: string,
|
|
370
|
-
recommendation: AgentRecommendation,
|
|
371
|
-
result: any
|
|
372
|
-
): Promise<void> {
|
|
373
|
-
const executionRecord = {
|
|
374
|
-
agentId,
|
|
375
|
-
recommendation,
|
|
376
|
-
result,
|
|
377
|
-
timestamp: new Date().toISOString(),
|
|
378
|
-
success: true
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
// Store in memory for coordination
|
|
382
|
-
await this.memory.store(`execution_${agentId}_${Date.now()}`, executionRecord);
|
|
383
|
-
|
|
384
|
-
// Update agent progress
|
|
385
|
-
await this.memory.storeProgress(agentId, {
|
|
386
|
-
lastExecution: executionRecord,
|
|
387
|
-
totalExecutions: await this.getAgentExecutionCount(agentId) + 1
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* Store execution failure for learning
|
|
393
|
-
*/
|
|
394
|
-
private async storeExecutionFailure(
|
|
395
|
-
agentId: string,
|
|
396
|
-
recommendation: AgentRecommendation,
|
|
397
|
-
error: any
|
|
398
|
-
): Promise<void> {
|
|
399
|
-
const failureRecord = {
|
|
400
|
-
agentId,
|
|
401
|
-
recommendation,
|
|
402
|
-
error: error instanceof Error ? error.message : String(error),
|
|
403
|
-
errorStack: error instanceof Error ? error.stack : undefined,
|
|
404
|
-
timestamp: new Date().toISOString(),
|
|
405
|
-
success: false
|
|
406
|
-
};
|
|
407
|
-
|
|
408
|
-
// Store failure for pattern analysis
|
|
409
|
-
await this.memory.store(`failure_${agentId}_${Date.now()}`, failureRecord);
|
|
410
|
-
|
|
411
|
-
// Update failure patterns
|
|
412
|
-
await this.memory.storeFailurePattern({
|
|
413
|
-
tool: recommendation.tool,
|
|
414
|
-
action: recommendation.action,
|
|
415
|
-
errorType: this.classifyError(error),
|
|
416
|
-
frequency: 1
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
/**
|
|
421
|
-
* Try fallback strategies when primary execution fails
|
|
422
|
-
*/
|
|
423
|
-
private async tryFallbackStrategies(
|
|
424
|
-
agent: { id: string; type: string },
|
|
425
|
-
recommendation: AgentRecommendation,
|
|
426
|
-
error: any
|
|
427
|
-
): Promise<MCPExecutionResult> {
|
|
428
|
-
const strategies = [
|
|
429
|
-
this.tryAlternativeTool.bind(this),
|
|
430
|
-
this.trySimplifiedParams.bind(this),
|
|
431
|
-
this.tryManualSteps.bind(this)
|
|
432
|
-
];
|
|
433
|
-
|
|
434
|
-
for (const strategy of strategies) {
|
|
435
|
-
try {
|
|
436
|
-
const result = await strategy(agent, recommendation, error);
|
|
437
|
-
if (result.success) {
|
|
438
|
-
this.logger.info('Fallback strategy succeeded:', strategy.name);
|
|
439
|
-
return result;
|
|
440
|
-
}
|
|
441
|
-
} catch (strategyError) {
|
|
442
|
-
this.logger.warn(`Fallback strategy ${strategy.name} failed:`, strategyError);
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
return {
|
|
447
|
-
success: false,
|
|
448
|
-
recommendation,
|
|
449
|
-
error: 'All fallback strategies failed',
|
|
450
|
-
executionTime: 0,
|
|
451
|
-
fallbackUsed: true
|
|
452
|
-
};
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* Try using an alternative tool
|
|
457
|
-
*/
|
|
458
|
-
private async tryAlternativeTool(
|
|
459
|
-
agent: { id: string; type: string },
|
|
460
|
-
recommendation: AgentRecommendation,
|
|
461
|
-
error: any
|
|
462
|
-
): Promise<MCPExecutionResult> {
|
|
463
|
-
// Map to alternative tools based on failure type
|
|
464
|
-
const alternatives: Record<string, string[]> = {
|
|
465
|
-
'snow_deploy': ['snow_deploy_widget', 'snow_deploy_flow'],
|
|
466
|
-
'snow_create_flow': ['snow_flow_wizard', 'snow_deploy'],
|
|
467
|
-
'snow_find_artifact': ['snow_comprehensive_search', 'snow_discover_existing_flows']
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
const altTools = alternatives[recommendation.tool];
|
|
471
|
-
if (!altTools || altTools.length === 0) {
|
|
472
|
-
throw new Error('No alternative tools available');
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
// Try first alternative
|
|
476
|
-
const altTool = altTools[0];
|
|
477
|
-
const altRecommendation = {
|
|
478
|
-
...recommendation,
|
|
479
|
-
tool: altTool,
|
|
480
|
-
params: this.adaptParamsForTool(recommendation.params, altTool)
|
|
481
|
-
};
|
|
482
|
-
|
|
483
|
-
return this.executeAgentRecommendation(agent, altRecommendation);
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
/**
|
|
487
|
-
* Try with simplified parameters
|
|
488
|
-
*/
|
|
489
|
-
private async trySimplifiedParams(
|
|
490
|
-
agent: { id: string; type: string },
|
|
491
|
-
recommendation: AgentRecommendation,
|
|
492
|
-
error: any
|
|
493
|
-
): Promise<MCPExecutionResult> {
|
|
494
|
-
// Simplify complex params
|
|
495
|
-
const simplifiedParams = this.simplifyParams(recommendation.params);
|
|
496
|
-
|
|
497
|
-
const simplifiedRecommendation = {
|
|
498
|
-
...recommendation,
|
|
499
|
-
params: simplifiedParams
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
return this.executeAgentRecommendation(agent, simplifiedRecommendation);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
/**
|
|
506
|
-
* Generate manual steps as fallback
|
|
507
|
-
*/
|
|
508
|
-
private async tryManualSteps(
|
|
509
|
-
agent: { id: string; type: string },
|
|
510
|
-
recommendation: AgentRecommendation,
|
|
511
|
-
error: any
|
|
512
|
-
): Promise<MCPExecutionResult> {
|
|
513
|
-
// Generate manual steps based on recommendation
|
|
514
|
-
const manualSteps = this.generateManualSteps(recommendation);
|
|
515
|
-
|
|
516
|
-
return {
|
|
517
|
-
success: true,
|
|
518
|
-
recommendation,
|
|
519
|
-
toolResult: {
|
|
520
|
-
type: 'manual_steps',
|
|
521
|
-
steps: manualSteps,
|
|
522
|
-
reason: 'Automated execution failed, manual steps provided'
|
|
523
|
-
},
|
|
524
|
-
executionTime: 0,
|
|
525
|
-
fallbackUsed: true
|
|
526
|
-
};
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
/**
|
|
530
|
-
* Helper methods
|
|
531
|
-
*/
|
|
532
|
-
|
|
533
|
-
private async getAgentExecutionCount(agentId: string): Promise<number> {
|
|
534
|
-
const progress = await this.memory.getProgress(agentId);
|
|
535
|
-
return progress?.totalExecutions || 0;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
private classifyError(error: any): string {
|
|
539
|
-
if (!error) return 'unknown';
|
|
540
|
-
|
|
541
|
-
const message = error instanceof Error ? error.message.toLowerCase() : String(error).toLowerCase();
|
|
542
|
-
|
|
543
|
-
if (message.includes('auth') || message.includes('401') || message.includes('403')) {
|
|
544
|
-
return 'authentication';
|
|
545
|
-
}
|
|
546
|
-
if (message.includes('timeout')) {
|
|
547
|
-
return 'timeout';
|
|
548
|
-
}
|
|
549
|
-
if (message.includes('not found') || message.includes('404')) {
|
|
550
|
-
return 'not_found';
|
|
551
|
-
}
|
|
552
|
-
if (message.includes('permission') || message.includes('access')) {
|
|
553
|
-
return 'permission';
|
|
554
|
-
}
|
|
555
|
-
if (message.includes('rate limit')) {
|
|
556
|
-
return 'rate_limit';
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
return 'general';
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
private adaptParamsForTool(params: any, newTool: string): any {
|
|
563
|
-
// Adapt parameters for different tool format
|
|
564
|
-
const adaptations: Record<string, (p: any) => any> = {
|
|
565
|
-
'snow_deploy_widget': (p) => ({
|
|
566
|
-
name: p.name || p.config?.name,
|
|
567
|
-
title: p.title || p.config?.title,
|
|
568
|
-
template: p.template || p.config?.template || '<div>Widget</div>',
|
|
569
|
-
css: p.css || p.config?.css || '',
|
|
570
|
-
client_script: p.client_script || p.config?.client_script || '',
|
|
571
|
-
server_script: p.server_script || p.config?.server_script || ''
|
|
572
|
-
}),
|
|
573
|
-
'snow_comprehensive_search': (p) => ({
|
|
574
|
-
query: p.query || p.name || p.description,
|
|
575
|
-
include_inactive: false
|
|
576
|
-
})
|
|
577
|
-
};
|
|
578
|
-
|
|
579
|
-
const adapter = adaptations[newTool];
|
|
580
|
-
return adapter ? adapter(params) : params;
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
private simplifyParams(params: any): any {
|
|
584
|
-
// Remove optional fields and complex nested structures
|
|
585
|
-
const simplified: any = {};
|
|
586
|
-
|
|
587
|
-
for (const [key, value] of Object.entries(params)) {
|
|
588
|
-
if (value !== undefined && value !== null) {
|
|
589
|
-
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
590
|
-
// Flatten nested objects
|
|
591
|
-
simplified[key] = JSON.stringify(value);
|
|
592
|
-
} else if (Array.isArray(value) && value.length > 5) {
|
|
593
|
-
// Limit array size
|
|
594
|
-
simplified[key] = value.slice(0, 5);
|
|
595
|
-
} else {
|
|
596
|
-
simplified[key] = value;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
return simplified;
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
private generateManualSteps(recommendation: AgentRecommendation): string[] {
|
|
605
|
-
const action = recommendation.action.toLowerCase();
|
|
606
|
-
|
|
607
|
-
if (action.includes('widget')) {
|
|
608
|
-
return [
|
|
609
|
-
'1. Navigate to Service Portal > Widgets',
|
|
610
|
-
'2. Click "New" to create a widget',
|
|
611
|
-
`3. Set name to: ${recommendation.params.name || 'custom_widget'}`,
|
|
612
|
-
'4. Add HTML template, CSS, and scripts as needed',
|
|
613
|
-
'5. Save and test the widget',
|
|
614
|
-
'6. Add widget to a portal page'
|
|
615
|
-
];
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
if (action.includes('flow')) {
|
|
619
|
-
return [
|
|
620
|
-
'1. Navigate to Flow Designer',
|
|
621
|
-
'2. Click "New" > "Flow"',
|
|
622
|
-
`3. Set name to: ${recommendation.params.name || 'custom_flow'}`,
|
|
623
|
-
'4. Configure trigger conditions',
|
|
624
|
-
'5. Add flow steps and actions',
|
|
625
|
-
'6. Test and activate the flow'
|
|
626
|
-
];
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
return [
|
|
630
|
-
'1. Log in to ServiceNow instance',
|
|
631
|
-
`2. Execute action: ${recommendation.action}`,
|
|
632
|
-
'3. Use parameters provided in the recommendation',
|
|
633
|
-
'4. Test the implementation',
|
|
634
|
-
'5. Document the changes'
|
|
635
|
-
];
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
/**
|
|
639
|
-
* Cleanup resources
|
|
640
|
-
*/
|
|
641
|
-
async cleanup(): Promise<void> {
|
|
642
|
-
// Disconnect all clients
|
|
643
|
-
for (const [name, client] of this.mcpClients) {
|
|
644
|
-
try {
|
|
645
|
-
await client.close();
|
|
646
|
-
this.logger.info(`Disconnected from MCP server: ${name}`);
|
|
647
|
-
} catch (error) {
|
|
648
|
-
this.logger.error(`Error disconnecting from ${name}:`, error);
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
// Terminate all processes
|
|
653
|
-
for (const [name, process] of this.mcpProcesses) {
|
|
654
|
-
try {
|
|
655
|
-
process.kill('SIGTERM');
|
|
656
|
-
this.logger.info(`Terminated MCP server process: ${name}`);
|
|
657
|
-
} catch (error) {
|
|
658
|
-
this.logger.error(`Error terminating ${name}:`, error);
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
this.mcpClients.clear();
|
|
663
|
-
this.mcpProcesses.clear();
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
/**
|
|
667
|
-
* Disconnect from MCP resources
|
|
668
|
-
*/
|
|
669
|
-
async disconnect(): Promise<void> {
|
|
670
|
-
this.logger.info('Disconnecting MCP resources');
|
|
671
|
-
// Clean up any active connections or resources
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
/**
|
|
675
|
-
* Public shutdown method for cleanup
|
|
676
|
-
*/
|
|
677
|
-
async shutdown(): Promise<void> {
|
|
678
|
-
this.logger.info('Shutting down MCP Execution Bridge');
|
|
679
|
-
await this.disconnect();
|
|
680
|
-
}
|
|
681
|
-
}
|