forked from getsentry/XcodeBuildMCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-registry.ts
More file actions
217 lines (187 loc) · 5.82 KB
/
tool-registry.ts
File metadata and controls
217 lines (187 loc) · 5.82 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { McpServer, RegisteredTool } from '@camsoft/mcp-sdk/server/mcp.js';
import { loadPlugins } from '../core/plugin-registry.ts';
import { ToolResponse } from '../types/common.ts';
import { log } from './logger.ts';
// Global registry to track registered tools for cleanup
const toolRegistry = new Map<string, RegisteredTool>();
/**
* Register a tool and track it for potential removal
*/
export function registerAndTrackTool(
server: McpServer,
name: string,
config: Parameters<McpServer['registerTool']>[1],
callback: Parameters<McpServer['registerTool']>[2],
): RegisteredTool {
const registeredTool = server.registerTool(name, config, callback);
toolRegistry.set(name, registeredTool);
return registeredTool;
}
/**
* Register multiple tools and track them for potential removal
*/
export function registerAndTrackTools(
server: McpServer,
tools: Parameters<McpServer['registerTools']>[0],
): RegisteredTool[] {
const registeredTools = server.registerTools(tools);
// Track each registered tool
tools.forEach((tool, index) => {
if (registeredTools[index]) {
toolRegistry.set(tool.name, registeredTools[index]);
}
});
return registeredTools;
}
/**
* Check if a tool is already registered
*/
export function isToolRegistered(name: string): boolean {
return toolRegistry.has(name);
}
/**
* Remove a specific tracked tool by name
*/
export function removeTrackedTool(name: string): boolean {
const tool = toolRegistry.get(name);
if (!tool) {
return false;
}
try {
tool.remove();
toolRegistry.delete(name);
log('debug', `✅ Removed tool: ${name}`);
return true;
} catch (error) {
log('error', `❌ Failed to remove tool ${name}: ${error}`);
return false;
}
}
/**
* Remove multiple tracked tools by names
*/
export function removeTrackedTools(names: string[]): string[] {
const removedTools: string[] = [];
for (const name of names) {
if (removeTrackedTool(name)) {
removedTools.push(name);
}
}
return removedTools;
}
/**
* Remove all currently tracked tools
*/
export function removeAllTrackedTools(): void {
const toolNames = Array.from(toolRegistry.keys());
if (toolNames.length === 0) {
return;
}
log('info', `Removing ${toolNames.length} tracked tools...`);
const removedTools = removeTrackedTools(toolNames);
log('info', `✅ Removed ${removedTools.length} tracked tools`);
}
/**
* Get the number of currently tracked tools
*/
export function getTrackedToolCount(): number {
return toolRegistry.size;
}
/**
* Get the names of currently tracked tools
*/
export function getTrackedToolNames(): string[] {
return Array.from(toolRegistry.keys());
}
/**
* Register only discovery tools (discover_tools, discover_projs) with tracking
*/
export async function registerDiscoveryTools(server: McpServer): Promise<void> {
const plugins = await loadPlugins();
let registeredCount = 0;
// Only register discovery tools initially
const discoveryTools = [];
for (const plugin of plugins.values()) {
// Only load discover_tools and discover_projs initially - other tools will be loaded via workflows
if (plugin.name === 'discover_tools' || plugin.name === 'discover_projs') {
discoveryTools.push({
name: plugin.name,
config: {
description: plugin.description ?? '',
inputSchema: plugin.schema,
},
// Adapt callback to match SDK's expected signature
callback: (args: unknown): Promise<ToolResponse> =>
plugin.handler(args as Record<string, unknown>),
});
registeredCount++;
}
}
// Register discovery tools using bulk registration with tracking
if (discoveryTools.length > 0) {
registerAndTrackTools(server, discoveryTools);
}
log('info', `✅ Registered ${registeredCount} discovery tools in dynamic mode.`);
}
/**
* Register selected workflows based on environment variable
*/
export async function registerSelectedWorkflows(
server: McpServer,
workflowNames: string[],
): Promise<void> {
const { loadWorkflowGroups } = await import('../core/plugin-registry.js');
const workflowGroups = await loadWorkflowGroups();
const selectedTools = [];
for (const workflowName of workflowNames) {
const workflow = workflowGroups.get(workflowName.trim());
if (workflow) {
for (const tool of workflow.tools) {
selectedTools.push({
name: tool.name,
config: {
description: tool.description ?? '',
inputSchema: tool.schema,
},
callback: (args: unknown): Promise<ToolResponse> =>
tool.handler(args as Record<string, unknown>),
});
}
}
}
if (selectedTools.length > 0) {
server.registerTools(selectedTools);
}
log(
'info',
`✅ Registered ${selectedTools.length} tools from workflows: ${workflowNames.join(', ')}`,
);
}
/**
* Register all tools (static mode) - no tracking needed since these won't be removed
*/
export async function registerAllToolsStatic(server: McpServer): Promise<void> {
const plugins = await loadPlugins();
const allTools = [];
for (const plugin of plugins.values()) {
// Exclude discovery tools in static mode - they should only be available in dynamic mode
if (plugin.name === 'discover_tools') {
continue;
}
allTools.push({
name: plugin.name,
config: {
description: plugin.description ?? '',
inputSchema: plugin.schema,
},
// Adapt callback to match SDK's expected signature
callback: (args: unknown): Promise<ToolResponse> =>
plugin.handler(args as Record<string, unknown>),
});
}
// Register all tools using bulk registration (no tracking since static tools aren't removed)
if (allTools.length > 0) {
server.registerTools(allTools);
}
log('info', `✅ Registered ${allTools.length} tools in static mode.`);
}