-
-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathregistry.ts
More file actions
207 lines (178 loc) · 5.74 KB
/
registry.ts
File metadata and controls
207 lines (178 loc) · 5.74 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
import type { McpServer, RegisteredTool } from '@modelcontextprotocol/sdk/server/mcp.js';
import type { CallToolResult, Tool, ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
import * as z from 'zod';
import { log } from '../../utils/logger.ts';
import { jsonSchemaToZod } from './jsonschema-to-zod.ts';
export type CallRemoteTool = (
remoteToolName: string,
args: Record<string, unknown>,
) => Promise<CallToolResult>;
type Entry = {
remoteName: string;
localName: string;
fingerprint: string;
registered: RegisteredTool;
};
export type ProxySyncResult = {
added: number;
updated: number;
removed: number;
total: number;
};
export class XcodeToolsProxyRegistry {
private readonly server: McpServer;
private readonly tools: Map<string, Entry> = new Map();
constructor(server: McpServer) {
this.server = server;
}
getRegisteredToolNames(): string[] {
return [...this.tools.values()].map((t) => t.localName).sort();
}
getRegisteredCount(): number {
return this.tools.size;
}
clear(): void {
for (const entry of this.tools.values()) {
entry.registered.remove();
}
this.tools.clear();
}
sync(remoteTools: Tool[], callRemoteTool: CallRemoteTool): ProxySyncResult {
const desiredRemoteNames = new Set(remoteTools.map((t) => t.name));
let added = 0;
let updated = 0;
let removed = 0;
for (const remoteTool of remoteTools) {
const remoteName = remoteTool.name;
const localName = toLocalToolName(remoteName);
const fingerprint = stableFingerprint(remoteTool);
const existing = this.tools.get(remoteName);
if (!existing) {
this.tools.set(remoteName, {
remoteName,
localName,
fingerprint,
registered: this.registerProxyTool(remoteTool, localName, callRemoteTool),
});
added += 1;
continue;
}
if (existing.fingerprint !== fingerprint) {
existing.registered.remove();
this.tools.set(remoteName, {
remoteName,
localName,
fingerprint,
registered: this.registerProxyTool(remoteTool, localName, callRemoteTool),
});
updated += 1;
}
}
for (const [remoteName, entry] of this.tools.entries()) {
if (!desiredRemoteNames.has(remoteName)) {
entry.registered.remove();
this.tools.delete(remoteName);
removed += 1;
}
}
return { added, updated, removed, total: this.tools.size };
}
private registerProxyTool(
tool: Tool,
localName: string,
callRemoteTool: CallRemoteTool,
): RegisteredTool {
const inputSchema = buildBestEffortInputSchema(tool);
const annotations = buildBestEffortAnnotations(tool, localName);
return this.server.registerTool(
localName,
{
description: tool.description ?? '',
inputSchema,
annotations,
_meta: {
xcodeToolsBridge: {
remoteTool: tool.name,
source: 'xcrun mcpbridge',
},
},
},
async (args: unknown) => {
const params = (args ?? {}) as Record<string, unknown>;
log(
'debug',
`[xcode-tools-bridge] Proxy call: ${tool.name} args=${JSON.stringify(params)}`,
);
const result = await callRemoteTool(tool.name, params);
log(
'debug',
`[xcode-tools-bridge] Proxy result: ${tool.name} contentItems=${result.content.length} isError=${result.isError ?? false}`,
);
return result;
},
);
}
}
export function toLocalToolName(remoteToolName: string): string {
return `xcode_tools_${remoteToolName}`;
}
function stableFingerprint(tool: Tool): string {
return JSON.stringify({
name: tool.name,
description: tool.description ?? null,
inputSchema: tool.inputSchema ?? null,
outputSchema: tool.outputSchema ?? null,
annotations: tool.annotations ?? null,
execution: tool.execution ?? null,
});
}
function buildBestEffortInputSchema(tool: Tool): z.ZodTypeAny {
if (!tool.inputSchema) {
return z.object({}).passthrough();
}
const zod = jsonSchemaToZod(tool.inputSchema);
return zod;
}
function buildBestEffortAnnotations(tool: Tool, localName: string): ToolAnnotations {
const existing = (tool.annotations ?? {}) as ToolAnnotations;
const readOnlyHint = existing.readOnlyHint ?? inferReadOnlyHint(localName);
const destructiveHint = existing.destructiveHint ?? inferDestructiveHint(localName, readOnlyHint);
const openWorldHint = existing.openWorldHint ?? inferOpenWorldHint(localName);
return {
...existing,
readOnlyHint,
destructiveHint,
openWorldHint,
};
}
function inferReadOnlyHint(localToolName: string): boolean {
// Default to conservative: most IDE tools can mutate project state.
const name = localToolName.toLowerCase();
const definitelyReadOnlyPrefixes = [
'xcode_tools_xcodelist',
'xcode_tools_xcodeglob',
'xcode_tools_xcodegrep',
'xcode_tools_xcoderead',
'xcode_tools_xcoderefreshcodeissuesinfile',
'xcode_tools_documentationsearch',
'xcode_tools_getbuildlog',
'xcode_tools_gettestlist',
];
if (definitelyReadOnlyPrefixes.some((p) => name.startsWith(p))) return true;
return false;
}
function inferDestructiveHint(localToolName: string, readOnlyHint: boolean): boolean {
if (readOnlyHint) return false;
const name = localToolName.toLowerCase();
const destructivePrefixes = [
'xcode_tools_xcodedelete',
'xcode_tools_xcodeclean',
'xcode_tools_xcodeerase',
'xcode_tools_xcoderemove',
];
return destructivePrefixes.some((p) => name.startsWith(p));
}
function inferOpenWorldHint(_localToolName: string): boolean {
// Xcode bridge tools are local IDE capabilities, not internet-facing or open-world tools.
return false;
}