-
-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathtool-service.ts
More file actions
165 lines (143 loc) · 4.6 KB
/
tool-service.ts
File metadata and controls
165 lines (143 loc) · 4.6 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
import type { CallToolResult, Tool } from '@modelcontextprotocol/sdk/types.js';
import { log } from '../../utils/logger.ts';
import {
XcodeToolsBridgeClient,
type XcodeToolsBridgeClientOptions,
type XcodeToolsBridgeClientStatus,
} from './client.ts';
import { getMcpBridgeAvailability } from './core.ts';
export interface BridgeCapabilities {
available: boolean;
path: string | null;
connected: boolean;
bridgePid: number | null;
lastError: string | null;
toolCount: number;
}
export interface XcodeIdeToolServiceOptions {
onToolCatalogInvalidated?: () => void;
clientOptions?: XcodeToolsBridgeClientOptions;
}
export interface ListBridgeToolsOptions {
refresh?: boolean;
}
export class XcodeIdeToolService {
private readonly client: XcodeToolsBridgeClient;
private readonly options: XcodeIdeToolServiceOptions;
private workflowEnabled = false;
private toolCatalog = new Map<string, Tool>();
private lastError: string | null = null;
private listInFlight: Promise<Tool[]> | null = null;
constructor(options: XcodeIdeToolServiceOptions = {}) {
this.options = options;
this.client = new XcodeToolsBridgeClient({
...this.options.clientOptions,
onToolsListChanged: (): void => {
this.toolCatalog.clear();
this.options.onToolCatalogInvalidated?.();
},
onBridgeClosed: (): void => {
this.toolCatalog.clear();
this.lastError = this.client.getStatus().lastError ?? this.lastError;
this.options.onToolCatalogInvalidated?.();
},
});
}
setWorkflowEnabled(enabled: boolean): void {
this.workflowEnabled = enabled;
}
isWorkflowEnabled(): boolean {
return this.workflowEnabled;
}
getClientStatus(): XcodeToolsBridgeClientStatus {
return this.client.getStatus();
}
getLastError(): string | null {
return this.lastError ?? this.client.getStatus().lastError;
}
getCachedTools(): Tool[] {
return [...this.toolCatalog.values()];
}
async getCapabilities(): Promise<BridgeCapabilities> {
const bridge = await getMcpBridgeAvailability();
const clientStatus = this.client.getStatus();
return {
available: bridge.available,
path: bridge.path,
connected: clientStatus.connected,
bridgePid: clientStatus.bridgePid,
lastError: this.getLastError(),
toolCount: this.toolCatalog.size,
};
}
async listTools(opts: ListBridgeToolsOptions = {}): Promise<Tool[]> {
if (opts.refresh === false) {
return this.getCachedTools();
}
return this.refreshTools();
}
async invokeTool(
name: string,
args: Record<string, unknown>,
opts: { timeoutMs?: number } = {},
): Promise<CallToolResult> {
await this.ensureConnected();
log('debug', `[xcode-tools-bridge] invokeTool: ${name} args=${JSON.stringify(args)}`);
try {
const response = await this.client.callTool(name, args, opts);
this.lastError = null;
log(
'debug',
`[xcode-tools-bridge] invokeTool result: ${name} contentItems=${response.content.length} isError=${response.isError ?? false}`,
);
return response;
} catch (error) {
this.lastError = toErrorMessage(error);
log('debug', `[xcode-tools-bridge] invokeTool error: ${name} error=${this.lastError}`);
throw error;
}
}
async disconnect(): Promise<void> {
this.toolCatalog.clear();
this.listInFlight = null;
await this.client.disconnect();
}
private async refreshTools(): Promise<Tool[]> {
if (this.listInFlight) {
return this.listInFlight;
}
this.listInFlight = (async (): Promise<Tool[]> => {
await this.ensureConnected();
const tools = await this.client.listTools();
this.toolCatalog = new Map(tools.map((tool) => [tool.name, tool]));
this.lastError = null;
return tools;
})();
try {
return await this.listInFlight;
} catch (error) {
this.toolCatalog.clear();
this.lastError = toErrorMessage(error);
throw error;
} finally {
this.listInFlight = null;
}
}
private async ensureConnected(): Promise<void> {
if (!this.workflowEnabled) {
const message = 'xcode-ide workflow is not enabled';
this.lastError = message;
throw new Error(message);
}
const bridge = await getMcpBridgeAvailability();
if (!bridge.available) {
const message = 'mcpbridge not available (xcrun --find mcpbridge failed)';
this.lastError = message;
throw new Error(message);
}
await this.client.connectOnce();
}
}
function toErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}