-
-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathregistry.integration.test.ts
More file actions
181 lines (159 loc) · 5.96 KB
/
registry.integration.test.ts
File metadata and controls
181 lines (159 loc) · 5.96 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
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { XcodeToolsBridgeClient } from '../client.ts';
import { XcodeToolsProxyRegistry } from '../registry.ts';
function fixturePath(rel: string): string {
const here = path.dirname(fileURLToPath(import.meta.url));
return path.join(here, 'fixtures', rel);
}
async function waitFor(fn: () => boolean, timeoutMs = 2000): Promise<void> {
const start = Date.now();
while (true) {
if (fn()) return;
if (Date.now() - start > timeoutMs) {
throw new Error('Timed out waiting for condition');
}
await new Promise((r) => setTimeout(r, 25));
}
}
describe('XcodeToolsProxyRegistry (stdio integration)', () => {
let localServer: McpServer;
let localClient: Client;
let bridgeClient: XcodeToolsBridgeClient;
let registry: XcodeToolsProxyRegistry;
const doSync = async (): Promise<void> => {
const tools = await bridgeClient.listTools();
registry.sync(tools, async (remoteName, args) => bridgeClient.callTool(remoteName, args));
if (localServer.isConnected()) {
localServer.sendToolListChanged();
}
};
beforeAll(async () => {
localServer = new McpServer(
{ name: 'local-test-server', version: '0.0.0' },
{ capabilities: { tools: { listChanged: true } } },
);
registry = new XcodeToolsProxyRegistry(localServer);
const fakeServerScript = fixturePath('fake-xcode-tools-server.mjs');
const env: Record<string, string> = {};
for (const [key, value] of Object.entries(process.env)) {
if (typeof value === 'string') {
env[key] = value;
}
}
bridgeClient = new XcodeToolsBridgeClient({
serverParams: {
command: process.execPath,
args: [fakeServerScript],
stderr: 'pipe',
env,
},
onToolsListChanged: () => {
void doSync();
},
});
await bridgeClient.connectOnce();
await doSync();
// Connect after initial tool registration so MCP server can register capabilities before connect.
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await localServer.connect(serverTransport);
localClient = new Client({ name: 'local-test-client', version: '0.0.0' });
await localClient.connect(clientTransport);
});
afterAll(async () => {
await bridgeClient.disconnect();
await localClient.close();
await localServer.close();
});
it('registers proxied tools and forwards calls', async () => {
const tools = await localClient.listTools();
const names = tools.tools.map((t) => t.name);
expect(names).toContain('xcode_tools_Alpha');
expect(names).toContain('xcode_tools_Beta');
expect(names).toContain('xcode_tools_TriggerChange');
const res = (await localClient.callTool({
name: 'xcode_tools_Alpha',
arguments: { value: 'hi' },
})) as CallToolResult;
expect(res.isError).not.toBe(true);
expect(res.content[0]).toMatchObject({ type: 'text', text: 'Alpha:hi' });
});
it('fills approval annotations for proxied tools when the remote tool omits them', async () => {
const tools = await localClient.listTools();
const alpha = tools.tools.find((tool) => tool.name === 'xcode_tools_Alpha');
const beta = tools.tools.find((tool) => tool.name === 'xcode_tools_Beta');
expect(alpha?.annotations).toMatchObject({
title: 'Alpha',
readOnlyHint: true,
destructiveHint: false,
openWorldHint: false,
});
expect(beta?.annotations).toMatchObject({
readOnlyHint: false,
destructiveHint: false,
openWorldHint: false,
});
});
it('updates registered tools on remote list change', async () => {
await localClient.callTool({ name: 'xcode_tools_TriggerChange', arguments: {} });
await waitFor(() => registry.getRegisteredToolNames().includes('xcode_tools_Gamma'));
const tools = await localClient.listTools();
const names = tools.tools.map((t) => t.name);
expect(names).toContain('xcode_tools_Alpha');
expect(names).not.toContain('xcode_tools_Beta');
expect(names).toContain('xcode_tools_Gamma');
const res = (await localClient.callTool({
name: 'xcode_tools_Alpha',
arguments: { value: 'hi', extra: 'e' },
})) as CallToolResult;
expect(res.content[0]).toMatchObject({ type: 'text', text: 'Alpha2:hi:e' });
});
it('passes arguments through the proxy without modification', async () => {
const tools = await localClient.listTools();
expect(tools.tools.map((t) => t.name)).toContain('xcode_tools_Echo');
const testCases = [
{
name: 'file paths with slashes and spaces',
args: {
filePath: '/Users/test/My Project/Sources/Views/MainTabView.swift',
tabIdentifier: 'windowtab1',
},
},
{
name: 'unicode characters in paths',
args: {
filePath: '/Users/test/Projekt/Ansichten/\u00dcbersicht.swift',
tabIdentifier: 'tab-2',
},
},
{
name: 'extra properties not in schema (passthrough)',
args: {
filePath: 'Sources/App.swift',
tabIdentifier: 'wt1',
extraFlag: true,
nested: { deep: 'value' },
},
},
];
for (const tc of testCases) {
const res = (await localClient.callTool({
name: 'xcode_tools_Echo',
arguments: tc.args,
})) as CallToolResult;
expect(res.isError).not.toBe(true);
const echoed = JSON.parse((res.content[0] as { text: string }).text) as Record<
string,
unknown
>;
for (const [key, value] of Object.entries(tc.args)) {
expect(echoed[key]).toEqual(value);
}
}
});
});