-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathmcp.ts
More file actions
134 lines (111 loc) · 3.41 KB
/
mcp.ts
File metadata and controls
134 lines (111 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Workflow MCP Integration
*
* Configures MCP servers for engines before workflow execution.
* This enables agents to use structured tool calls instead of text-based signals.
*/
import { registry } from '../infra/engines/index.js';
import { debug } from '../shared/logging/logger.js';
import type { WorkflowTemplate } from './templates/types.js';
/**
* Get unique engine IDs used in a workflow template
*/
export function getWorkflowEngines(template: WorkflowTemplate): string[] {
const engines = new Set<string>();
const defaultEngine = registry.getDefault();
for (const step of template.steps) {
if (step.type === 'module') {
const engineId = step.engine ?? defaultEngine?.metadata.id;
if (engineId) {
engines.add(engineId);
}
}
}
return Array.from(engines);
}
/**
* Configure MCP servers for all engines used in a workflow
*
* Call this before running the workflow to ensure agents have access
* to the workflow-signals MCP tools.
*/
export async function setupWorkflowMCP(
template: WorkflowTemplate,
workflowDir: string
): Promise<{ configured: string[]; failed: string[] }> {
const engineIds = getWorkflowEngines(template);
const configured: string[] = [];
const failed: string[] = [];
debug('[MCP] Setting up MCP for engines: %s', engineIds.join(', '));
for (const engineId of engineIds) {
const engine = registry.get(engineId);
if (!engine) {
debug('[MCP] Engine not found: %s', engineId);
failed.push(engineId);
continue;
}
if (!engine.mcp?.supported) {
debug('[MCP] Engine does not support MCP: %s', engineId);
continue; // Not a failure, just unsupported
}
if (!engine.mcp.configure) {
debug('[MCP] Engine has no configure method: %s', engineId);
continue;
}
try {
await engine.mcp.configure(workflowDir);
configured.push(engineId);
debug('[MCP] Configured MCP for engine: %s', engineId);
} catch (error) {
debug('[MCP] Failed to configure MCP for engine %s: %s', engineId, (error as Error).message);
failed.push(engineId);
}
}
return { configured, failed };
}
/**
* Clean up MCP servers after workflow completes
*/
export async function cleanupWorkflowMCP(
template: WorkflowTemplate,
workflowDir: string
): Promise<void> {
const engineIds = getWorkflowEngines(template);
debug('[MCP] Cleaning up MCP for engines: %s', engineIds.join(', '));
for (const engineId of engineIds) {
const engine = registry.get(engineId);
if (!engine?.mcp?.cleanup) {
continue;
}
try {
await engine.mcp.cleanup(workflowDir);
debug('[MCP] Cleaned up MCP for engine: %s', engineId);
} catch (error) {
debug('[MCP] Failed to cleanup MCP for engine %s: %s', engineId, (error as Error).message);
// Ignore cleanup errors
}
}
}
/**
* Check if MCP is configured for all engines in a workflow
*/
export async function isWorkflowMCPConfigured(
template: WorkflowTemplate,
workflowDir: string
): Promise<boolean> {
const engineIds = getWorkflowEngines(template);
for (const engineId of engineIds) {
const engine = registry.get(engineId);
if (!engine?.mcp?.supported) {
continue; // Skip unsupported engines
}
if (!engine.mcp.isConfigured) {
continue;
}
const configured = await engine.mcp.isConfigured(workflowDir);
if (!configured) {
return false;
}
}
return true;
}