File size: 10,422 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { Client } from '@modelcontextprotocol/sdk/client';
import { CORS_PROXY } from '$lib/constants';
import { MCPConnectionPhase, MCPTransportType } from '$lib/enums';
import { MCPService } from '$lib/services/mcp.service';
import type { MCPConnection, MCPConnectionLog, MCPServerConfig } from '$lib/types';
import { afterEach, describe, expect, it, vi } from 'vitest';

type DiagnosticFetchFactory = (
	serverName: string,
	config: MCPServerConfig,
	baseInit: RequestInit,
	targetUrl: URL,
	useProxy: boolean,
	onLog?: (log: MCPConnectionLog) => void
) => { fetch: typeof fetch; disable: () => void };

const createDiagnosticFetch = (
	config: MCPServerConfig,
	onLog?: (log: MCPConnectionLog) => void,
	baseInit: RequestInit = {},
	useProxy = false
) =>
	(
		MCPService as unknown as { createDiagnosticFetch: DiagnosticFetchFactory }
	).createDiagnosticFetch('test-server', config, baseInit, new URL(config.url), useProxy, onLog);

describe('MCPService', () => {
	afterEach(() => {
		vi.restoreAllMocks();
		vi.unstubAllGlobals();
	});

	it('stops transport phase logging after handshake diagnostics are disabled', async () => {
		const logs: MCPConnectionLog[] = [];
		const response = new Response('{}', {
			headers: { 'content-type': 'application/json' },
			status: 200
		});

		vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response));

		const config: MCPServerConfig = {
			transport: MCPTransportType.STREAMABLE_HTTP,
			url: 'https://example.com/mcp'
		};
		const controller = createDiagnosticFetch(config, (log) => logs.push(log));

		await controller.fetch(config.url, { body: '{}', method: 'POST' });
		expect(logs).toHaveLength(2);
		expect(logs.every((log) => log.message.includes('https://example.com/mcp'))).toBe(true);

		controller.disable();
		await controller.fetch(config.url, { body: '{}', method: 'POST' });

		expect(logs).toHaveLength(2);
	});

	it('redacts all configured custom headers in diagnostic request logs', async () => {
		const logs: MCPConnectionLog[] = [];
		const response = new Response('{}', {
			headers: { 'content-type': 'application/json' },
			status: 200
		});

		vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response));

		const config: MCPServerConfig = {
			headers: {
				'x-auth-token': 'secret-token',
				'x-vendor-api-key': 'secret-key'
			},
			transport: MCPTransportType.STREAMABLE_HTTP,
			url: 'https://example.com/mcp'
		};
		const controller = createDiagnosticFetch(config, (log) => logs.push(log), {
			headers: config.headers
		});

		await controller.fetch(config.url, {
			body: '{}',
			headers: { 'content-type': 'application/json' },
			method: 'POST'
		});

		expect(logs).toHaveLength(2);
		expect(logs[0].details).toMatchObject({
			request: {
				headers: {
					'content-type': 'application/json',
					'x-auth-token': '[redacted]',
					'x-vendor-api-key': '[redacted]'
				}
			}
		});
	});

	it('wraps dynamic request headers when using the CORS proxy', async () => {
		const logs: MCPConnectionLog[] = [];
		const proxiedAuthToken = `${CORS_PROXY.HEADER_PREFIX}x-auth-token`;
		const proxiedContentType = `${CORS_PROXY.HEADER_PREFIX}content-type`;
		const proxiedSessionId = `${CORS_PROXY.HEADER_PREFIX}mcp-session-id`;
		const response = new Response('{}', {
			headers: { 'content-type': 'application/json' },
			status: 200
		});
		const fetchMock = vi.fn().mockResolvedValue(response);

		vi.stubGlobal('fetch', fetchMock);

		const config: MCPServerConfig = {
			transport: MCPTransportType.STREAMABLE_HTTP,
			url: 'https://example.com/mcp',
			useProxy: true
		};
		const controller = createDiagnosticFetch(
			config,
			(log) => logs.push(log),
			{
				headers: {
					authorization: 'Bearer llama-server-key',
					[proxiedAuthToken]: 'target-token'
				}
			},
			true
		);

		await controller.fetch('http://localhost:8080/cors-proxy?url=https%3A%2F%2Fexample.com%2Fmcp', {
			body: '{}',
			headers: {
				'content-type': 'application/json',
				'mcp-session-id': 'session-request-12345'
			},
			method: 'POST'
		});

		const sentHeaders = fetchMock.mock.calls[0]?.[1]?.headers as Headers;

		expect(sentHeaders.get('authorization')).toBe('Bearer llama-server-key');
		expect(sentHeaders.get(proxiedAuthToken)).toBe('target-token');
		expect(sentHeaders.get(proxiedContentType)).toBe('application/json');
		expect(sentHeaders.get(proxiedSessionId)).toBe('session-request-12345');
		expect(sentHeaders.has('content-type')).toBe(false);
		expect(sentHeaders.has('mcp-session-id')).toBe(false);
		expect(logs[0].details).toMatchObject({
			request: {
				headers: {
					authorization: '[redacted]',
					[proxiedAuthToken]: '[redacted]',
					[proxiedSessionId]: '....12345'
				}
			}
		});
	});

	it('DELETE request with CORS proxy should return a fake 200 response', async () => {
		const logs: MCPConnectionLog[] = [];
		const fetchMock = vi.fn();

		vi.stubGlobal('fetch', fetchMock);

		const config: MCPServerConfig = {
			transport: MCPTransportType.STREAMABLE_HTTP,
			url: 'https://example.com/mcp',
			useProxy: true
		};
		const controller = createDiagnosticFetch(config, (log) => logs.push(log), {}, true);
		const response = await controller.fetch(
			'http://localhost:8080/cors-proxy?url=https%3A%2F%2Fexample.com%2Fmcp',
			{ method: 'DELETE' }
		);

		expect(fetchMock).not.toHaveBeenCalled();
		expect(response.status).toBe(200);
		expect(logs.at(-1)?.details).toMatchObject({
			response: { isFake: true, status: 200 }
		});
	});

	it('partially redacts mcp-session-id in diagnostic request and response logs', async () => {
		const logs: MCPConnectionLog[] = [];
		const response = new Response('{}', {
			headers: {
				'content-type': 'application/json',
				'mcp-session-id': 'session-response-67890'
			},
			status: 200
		});

		vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response));

		const config: MCPServerConfig = {
			transport: MCPTransportType.STREAMABLE_HTTP,
			url: 'https://example.com/mcp'
		};
		const controller = createDiagnosticFetch(config, (log) => logs.push(log));

		await controller.fetch(config.url, {
			body: '{}',
			headers: {
				'content-type': 'application/json',
				'mcp-session-id': 'session-request-12345'
			},
			method: 'POST'
		});

		expect(logs).toHaveLength(2);
		expect(logs[0].details).toMatchObject({
			request: {
				headers: {
					'content-type': 'application/json',
					'mcp-session-id': '....12345'
				}
			}
		});
		expect(logs[1].details).toMatchObject({
			response: {
				headers: {
					'content-type': 'application/json',
					'mcp-session-id': '....67890'
				}
			}
		});
	});

	it('extracts JSON-RPC methods without logging the raw request body', async () => {
		const logs: MCPConnectionLog[] = [];
		const response = new Response('{}', {
			headers: { 'content-type': 'application/json' },
			status: 200
		});

		vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response));

		const config: MCPServerConfig = {
			transport: MCPTransportType.STREAMABLE_HTTP,
			url: 'https://example.com/mcp'
		};
		const controller = createDiagnosticFetch(config, (log) => logs.push(log));

		await controller.fetch(config.url, {
			body: JSON.stringify([
				{ id: 1, jsonrpc: '2.0', method: 'initialize' },
				{ jsonrpc: '2.0', method: 'notifications/initialized' }
			]),
			method: 'POST'
		});

		expect(logs[0].details).toMatchObject({
			request: {
				body: {
					kind: 'string',
					size: expect.any(Number)
				},
				jsonRpcMethods: ['initialize', 'notifications/initialized'],
				method: 'POST'
			}
		});
	});

	it('adds a CORS hint to Failed to fetch diagnostic log messages', async () => {
		const logs: MCPConnectionLog[] = [];
		const fetchError = new TypeError('Failed to fetch');

		vi.stubGlobal('fetch', vi.fn().mockRejectedValue(fetchError));

		const config: MCPServerConfig = {
			transport: MCPTransportType.STREAMABLE_HTTP,
			url: 'http://localhost:8000/mcp'
		};
		const controller = createDiagnosticFetch(config, (log) => logs.push(log));

		await expect(controller.fetch(config.url, { body: '{}', method: 'POST' })).rejects.toThrow(
			'Failed to fetch'
		);

		expect(logs).toHaveLength(2);
		expect(logs[1].message).toBe(
			'HTTP POST http://localhost:8000/mcp failed: Failed to fetch (check CORS?)'
		);
	});

	it('detaches phase error logging after the initialize handshake completes', async () => {
		const phaseLogs: Array<{ phase: MCPConnectionPhase; log: MCPConnectionLog }> = [];
		const stopPhaseLogging = vi.fn();

		let emitClientError: ((error: Error) => void) | undefined;

		vi.spyOn(MCPService, 'createTransport').mockReturnValue({
			stopPhaseLogging,
			transport: {} as never,
			type: MCPTransportType.WEBSOCKET
		});
		vi.spyOn(MCPService, 'listTools').mockResolvedValue([]);
		vi.spyOn(Client.prototype, 'getServerVersion').mockReturnValue(undefined);
		vi.spyOn(Client.prototype, 'getServerCapabilities').mockReturnValue(undefined);
		vi.spyOn(Client.prototype, 'getInstructions').mockReturnValue(undefined);
		vi.spyOn(Client.prototype, 'connect').mockImplementation(async function (this: Client) {
			emitClientError = (error: Error) => this.onerror?.(error);
			this.onerror?.(new Error('handshake protocol error'));
		});

		await MCPService.connect(
			'test-server',
			{
				transport: MCPTransportType.WEBSOCKET,
				url: 'ws://example.com/mcp'
			},
			undefined,
			undefined,
			(phase, log) => phaseLogs.push({ log, phase })
		);

		expect(stopPhaseLogging).toHaveBeenCalledTimes(1);
		expect(
			phaseLogs.filter(
				({ log, phase }) =>
					phase === MCPConnectionPhase.ERROR &&
					log.message === 'Protocol error: handshake protocol error'
			)
		).toHaveLength(1);

		emitClientError?.(new Error('runtime protocol error'));

		expect(
			phaseLogs.filter(
				({ log, phase }) =>
					phase === MCPConnectionPhase.ERROR &&
					log.message === 'Protocol error: runtime protocol error'
			)
		).toHaveLength(0);
	});

	it('falls back to structuredContent when content array is empty', async () => {
		const connection = {
			client: {
				callTool: vi.fn().mockResolvedValue({
					content: [],
					structuredContent: { accounts: [{ id: 1 }], total: 1 }
				})
			},
			requestTimeoutMs: 9000,
			serverName: 'test-server'
		} as unknown as MCPConnection;
		const result = await MCPService.callTool(connection, { arguments: {}, name: 'tool' });

		expect(result.isError).toBe(false);
		expect(result.content).toBe('{"accounts":[{"id":1}],"total":1}');
	});
});