-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathutils.test.ts
More file actions
1079 lines (890 loc) · 36.6 KB
/
Copy pathutils.test.ts
File metadata and controls
1079 lines (890 loc) · 36.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { parseCommandLineArgs, shouldIncludeTool, mcpProxy, setupOAuthCallbackServerWithLongPoll, getServerUrlHash } from './utils'
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
import { EventEmitter } from 'events'
import express from 'express'
// All sanitizeUrl tests have been moved to the strict-url-sanitise package
describe('Feature: Command Line Arguments Parsing', () => {
it('Scenario: Parse basic server URL', async () => {
// Given command line arguments with only a server URL
const args = ['https://example.com/sse']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the server URL should be correctly extracted
expect(result.serverUrl).toBe('https://example.com/sse')
expect(typeof result.serverUrl).toBe('string')
})
it('Scenario: Parse server URL with callback port', async () => {
// Given command line arguments with server URL and port
const args = ['https://example.com/sse', '3000']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then both server URL and callback port should be correctly extracted
expect(result.serverUrl).toBe('https://example.com/sse')
expect(result.callbackPort).toBe(3000)
})
it('Scenario: Parse localhost URL with HTTP protocol', async () => {
// Given command line arguments with localhost HTTP URL
const args = ['http://localhost:8080/sse']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the localhost HTTP URL should be accepted
expect(result.serverUrl).toBe('http://localhost:8080/sse')
})
it('Scenario: Parse 127.0.0.1 URL with HTTP protocol', async () => {
// Given command line arguments with 127.0.0.1 HTTP URL
const args = ['http://127.0.0.1:8080/sse']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the 127.0.0.1 HTTP URL should be accepted
expect(result.serverUrl).toBe('http://127.0.0.1:8080/sse')
})
it('Scenario: Parse single custom header', async () => {
// Given command line arguments with a custom header
const args = ['https://example.com/sse', '--header', 'foo: taz']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the custom header should be correctly parsed
expect(result.serverUrl).toBe('https://example.com/sse')
expect(result.headers).toEqual({ foo: 'taz' })
})
it('Scenario: Do not log custom header values', async () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const args = ['https://example.com/sse', '--header', 'Authorization: Bearer private-token']
const result = await parseCommandLineArgs(args, 'test usage')
expect(result.headers).toEqual({ Authorization: 'Bearer private-token' })
expect(JSON.stringify(consoleSpy.mock.calls)).not.toContain('private-token')
expect(JSON.stringify(consoleSpy.mock.calls)).toContain('Authorization')
consoleSpy.mockRestore()
})
it('Scenario: Parse multiple custom headers', async () => {
// Given command line arguments with multiple custom headers
const args = ['https://example.com/sse', '--header', 'Authorization: Bearer token123', '--header', 'Content-Type: application/json']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then all custom headers should be correctly parsed
expect(result.serverUrl).toBe('https://example.com/sse')
expect(result.headers).toEqual({
Authorization: 'Bearer token123',
'Content-Type': 'application/json',
})
})
it('Scenario: Ignore invalid header format', async () => {
// Given command line arguments with an invalid header format
const args = ['https://example.com/sse', '--header', 'invalid-header-format']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the invalid header should be ignored and headers should be empty
expect(result.serverUrl).toBe('https://example.com/sse')
expect(result.headers).toEqual({})
})
it('Scenario: Handle --allow-http flag for non-localhost URLs', async () => {
// Given command line arguments with HTTP URL and --allow-http flag
const args = ['http://example.com/sse', '--allow-http']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the HTTP URL should be accepted due to --allow-http flag
expect(result.serverUrl).toBe('http://example.com/sse')
})
it('Scenario: Accept HTTPS URLs without --allow-http flag', async () => {
// Given command line arguments with HTTPS URL only
const args = ['https://example.com/sse']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the HTTPS URL should be accepted without any additional flags
expect(result.serverUrl).toBe('https://example.com/sse')
})
it('Scenario: Handle --allow-http with other arguments', async () => {
// Given command line arguments with HTTP URL, port, --allow-http flag, and custom header
const args = ['http://example.com/sse', '4000', '--allow-http', '--header', 'Authorization: Bearer abc123']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then all arguments should be correctly parsed including HTTP URL acceptance
expect(result.serverUrl).toBe('http://example.com/sse')
expect(result.callbackPort).toBe(4000)
expect(result.headers).toEqual({ Authorization: 'Bearer abc123' })
})
it('Scenario: Use default transport strategy when not specified', async () => {
// Given command line arguments with only server URL
const args = ['https://example.com/sse']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the default transport strategy should be http-first
expect(result.transportStrategy).toBe('http-first')
})
it('Scenario: Parse transport strategy sse-only', async () => {
// Given command line arguments with --transport sse-only
const args = ['https://example.com/sse', '--transport', 'sse-only']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the transport strategy should be set to sse-only
expect(result.transportStrategy).toBe('sse-only')
})
it('Scenario: Parse transport strategy http-only', async () => {
// Given command line arguments with --transport http-only
const args = ['https://example.com/sse', '--transport', 'http-only']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the transport strategy should be set to http-only
expect(result.transportStrategy).toBe('http-only')
})
it('Scenario: Parse transport strategy sse-first', async () => {
// Given command line arguments with --transport sse-first
const args = ['https://example.com/sse', '--transport', 'sse-first']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the transport strategy should be set to sse-first
expect(result.transportStrategy).toBe('sse-first')
})
it('Scenario: Parse transport strategy http-first', async () => {
// Given command line arguments with --transport http-first
const args = ['https://example.com/sse', '--transport', 'http-first']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the transport strategy should be set to http-first
expect(result.transportStrategy).toBe('http-first')
})
it('Scenario: Ignore invalid transport strategy and use default', async () => {
// Given command line arguments with invalid transport strategy
const args = ['https://example.com/sse', '--transport', 'invalid-strategy']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the invalid strategy should be ignored and default should be used
expect(result.transportStrategy).toBe('http-first') // Should fallback to default
})
it('Scenario: Use default host when not specified', async () => {
// Given command line arguments with only server URL
const args = ['https://example.com/sse']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the default host should be localhost
expect(result.host).toBe('localhost')
})
it('Scenario: Parse custom IP host', async () => {
// Given command line arguments with custom IP host
const args = ['https://example.com/sse', '--host', '127.0.0.1']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the custom IP host should be correctly set
expect(result.host).toBe('127.0.0.1')
})
it('Scenario: Parse custom domain host', async () => {
// Given command line arguments with custom domain host
const args = ['https://example.com/sse', '--host', 'myserver.local']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the custom domain host should be correctly set
expect(result.host).toBe('myserver.local')
})
it('Scenario: Handle host with multiple other arguments', async () => {
// Given command line arguments with host, port, and transport strategy
const args = ['https://example.com/sse', '3000', '--host', 'custom.host.com', '--transport', 'sse-only']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then all arguments should be correctly parsed including the host
expect(result.serverUrl).toBe('https://example.com/sse')
expect(result.callbackPort).toBe(3000)
expect(result.host).toBe('custom.host.com')
expect(result.transportStrategy).toBe('sse-only')
})
it('Scenario: Return empty ignored tools array when none specified', async () => {
// Given command line arguments without --ignore-tool flags
const args = ['https://example.com/sse']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the ignored tools array should be empty
expect(result.ignoredTools).toEqual([])
})
it('Scenario: Parse single ignored tool', async () => {
// Given command line arguments with one --ignore-tool flag
const args = ['https://example.com/sse', '--ignore-tool', 'foo']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the ignored tools array should contain the specified tool
expect(result.serverUrl).toBe('https://example.com/sse')
expect(result.ignoredTools).toEqual(['foo'])
})
it('Scenario: Parse multiple ignored tools', async () => {
// Given command line arguments with multiple --ignore-tool flags
const args = ['https://example.com/sse', '--ignore-tool', 'foo', '--ignore-tool', 'bar', '--ignore-tool', 'baz']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the ignored tools array should contain all specified tools
expect(result.serverUrl).toBe('https://example.com/sse')
expect(result.ignoredTools).toEqual(['foo', 'bar', 'baz'])
})
it('Scenario: Handle ignored tools with other arguments', async () => {
// Given command line arguments with ignored tools mixed with other arguments
const args = [
'https://example.com/sse',
'4000',
'--ignore-tool',
'tool1',
'--host',
'localhost',
'--ignore-tool',
'tool2',
'--transport',
'sse-only',
]
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then all arguments should be correctly parsed including ignored tools
expect(result.serverUrl).toBe('https://example.com/sse')
expect(result.callbackPort).toBe(4000)
expect(result.host).toBe('localhost')
expect(result.transportStrategy).toBe('sse-only')
expect(result.ignoredTools).toEqual(['tool1', 'tool2'])
})
it('Scenario: Use default auth timeout when not specified', async () => {
// Given command line arguments without --auth-timeout flag
const args = ['https://example.com/sse']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the default auth timeout should be 30000ms
expect(result.authTimeoutMs).toBe(30000)
})
it('Scenario: Parse valid auth timeout in seconds and convert to milliseconds', async () => {
// Given command line arguments with valid --auth-timeout
const args = ['https://example.com/sse', '--auth-timeout', '60']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the timeout should be converted to milliseconds
expect(result.authTimeoutMs).toBe(60000)
})
it('Scenario: Use default timeout when invalid auth timeout value is provided', async () => {
// Given command line arguments with invalid --auth-timeout value
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const args = ['https://example.com/sse', '--auth-timeout', 'invalid']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the default timeout should be used and warning logged
expect(result.authTimeoutMs).toBe(30000)
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Warning: Ignoring invalid auth timeout value: invalid. Must be a positive number.'),
)
consoleSpy.mockRestore()
})
it('Scenario: Use default timeout when negative auth timeout value is provided', async () => {
// Given command line arguments with negative --auth-timeout value
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const args = ['https://example.com/sse', '--auth-timeout', '-30']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the default timeout should be used and warning logged
expect(result.authTimeoutMs).toBe(30000)
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Warning: Ignoring invalid auth timeout value: -30. Must be a positive number.'),
)
consoleSpy.mockRestore()
})
it('Scenario: Use default timeout when zero auth timeout value is provided', async () => {
// Given command line arguments with zero --auth-timeout value
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const args = ['https://example.com/sse', '--auth-timeout', '0']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the default timeout should be used and warning logged
expect(result.authTimeoutMs).toBe(30000)
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Warning: Ignoring invalid auth timeout value: 0. Must be a positive number.'),
)
consoleSpy.mockRestore()
})
it('Scenario: Log when using custom auth timeout', async () => {
// Given command line arguments with custom --auth-timeout value
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const args = ['https://example.com/sse', '--auth-timeout', '45']
const usage = 'test usage'
// When parsing the command line arguments
const result = await parseCommandLineArgs(args, usage)
// Then the custom timeout should be used and logged
expect(result.authTimeoutMs).toBe(45000)
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Using auth callback timeout: 45 seconds'))
consoleSpy.mockRestore()
})
it('Scenario: Suppresses LOG when using --silent', async () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const args = ['https://example.com/sse', '--auth-timeout', '45', '--silent']
const usage = 'test usage'
const result = await parseCommandLineArgs(args, usage)
expect(result.authTimeoutMs).toBe(45000)
expect(consoleSpy).not.toHaveBeenCalled()
consoleSpy.mockRestore()
})
})
describe('Feature: Tool Filtering with Ignore Patterns', () => {
it('Scenario: Single wildcard pattern ignores matching tools', () => {
// Given ignore patterns with create* wildcard
const ignorePatterns = ['create*']
// When checking if createTask should be included
const result1 = shouldIncludeTool(ignorePatterns, 'createTask')
// Then it should be excluded (return false)
expect(result1).toBe(false)
// When checking if getTask should be included
const result2 = shouldIncludeTool(ignorePatterns, 'getTask')
// Then it should be included (return true)
expect(result2).toBe(true)
})
it('Scenario: Multiple wildcard patterns ignore matching tools', () => {
// Given ignore patterns with create* and put* wildcards
const ignorePatterns = ['create*', 'put*']
// When checking if createTask should be included
const result1 = shouldIncludeTool(ignorePatterns, 'createTask')
// Then it should be excluded (return false)
expect(result1).toBe(false)
// When checking if infoTask should be included
const result2 = shouldIncludeTool(ignorePatterns, 'infoTask')
// Then it should be included (return true)
expect(result2).toBe(true)
})
it('Scenario: Suffix wildcard pattern ignores matching tools', () => {
// Given ignore patterns with *account suffix wildcard
const ignorePatterns = ['*account']
// When checking various account-related tools
const result1 = shouldIncludeTool(ignorePatterns, 'getAccount')
const result2 = shouldIncludeTool(ignorePatterns, 'putAccount')
const result3 = shouldIncludeTool(ignorePatterns, 'account')
// Then all should be excluded (return false)
expect(result1).toBe(false)
expect(result2).toBe(false)
expect(result3).toBe(false)
})
it('Scenario: Empty ignore patterns include all tools', () => {
// Given empty ignore patterns
const ignorePatterns: string[] = []
// When checking any tool
const result = shouldIncludeTool(ignorePatterns, 'anyTool')
// Then it should be included (return true)
expect(result).toBe(true)
})
it('Scenario: Non-matching patterns include tools', () => {
// Given ignore patterns that don't match the tool
const ignorePatterns = ['delete*', 'remove*']
// When checking a tool that doesn't match any pattern
const result = shouldIncludeTool(ignorePatterns, 'createTask')
// Then it should be included (return true)
expect(result).toBe(true)
})
it('Scenario: Exact match without wildcards', () => {
// Given ignore patterns with exact tool names
const ignorePatterns = ['exactTool', 'anotherTool']
// When checking the exact tool name
const result1 = shouldIncludeTool(ignorePatterns, 'exactTool')
// Then it should be excluded (return false)
expect(result1).toBe(false)
// When checking a different tool name
const result2 = shouldIncludeTool(ignorePatterns, 'differentTool')
// Then it should be included (return true)
expect(result2).toBe(true)
})
})
describe('Feature: MCP Proxy', () => {
it('Scenario: Proxy initialize message from client to server', async () => {
// Given mock transports for client and server
const mockTransportToClient = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
const mockTransportToServer = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
// When setting up the proxy
mcpProxy({
transportToClient: mockTransportToClient,
transportToServer: mockTransportToServer,
ignoredTools: [],
})
// And when client sends an initialize message
const initializeMessage = {
jsonrpc: '2.0' as const,
method: 'initialize',
id: '1',
params: {
clientInfo: {
name: 'Test Client',
version: '1.0.0',
},
},
}
// Simulate client sending a message by calling the message handler directly
if (mockTransportToClient.onmessage) {
mockTransportToClient.onmessage(initializeMessage)
}
// Then the message should be forwarded to the server
expect(mockTransportToServer.send).toHaveBeenCalledWith(
expect.objectContaining({
jsonrpc: '2.0',
method: 'initialize',
id: '1',
params: expect.objectContaining({
clientInfo: expect.objectContaining({
name: expect.stringContaining('Test Client'),
version: '1.0.0',
}),
}),
}),
)
})
it('Scenario: Proxy server response back to client', async () => {
// Given mock transports for client and server
const mockTransportToClient = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
const mockTransportToServer = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
// When setting up the proxy
mcpProxy({
transportToClient: mockTransportToClient,
transportToServer: mockTransportToServer,
ignoredTools: [],
})
// First simulate client sending a request (so there's a pending request)
const clientRequest = {
jsonrpc: '2.0' as const,
method: 'initialize',
id: '1',
params: {
clientInfo: {
name: 'Test Client',
version: '1.0.0',
},
},
}
if (mockTransportToClient.onmessage) {
mockTransportToClient.onmessage(clientRequest)
}
// Clear the previous call
vi.clearAllMocks()
// Now simulate server sending a response message
const serverResponse = {
jsonrpc: '2.0' as const,
id: '1',
result: {
capabilities: {
tools: {
listChanged: true,
},
},
serverInfo: {
name: 'Atlassian MCP',
version: '1.0.0',
},
},
}
// Simulate server sending a response by calling the message handler directly
if (mockTransportToServer.onmessage) {
mockTransportToServer.onmessage(serverResponse)
}
// Then the response should be forwarded to the client
expect(mockTransportToClient.send).toHaveBeenCalledWith(
expect.objectContaining({
jsonrpc: '2.0',
id: '1',
result: {
capabilities: {
tools: {
listChanged: true,
},
},
serverInfo: {
name: 'Atlassian MCP',
version: '1.0.0',
},
},
}),
)
})
it('Scenario: Close server transport when client transport closes', async () => {
// Given mock transports for client and server
const mockTransportToClient = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
const mockTransportToServer = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
// When setting up the proxy
mcpProxy({
transportToClient: mockTransportToClient,
transportToServer: mockTransportToServer,
ignoredTools: [],
})
// And when client transport closes
if (mockTransportToClient.onclose) {
mockTransportToClient.onclose()
}
// Then server transport should also be closed
expect(mockTransportToServer.close).toHaveBeenCalled()
})
it('Scenario: Close client transport when server transport closes', async () => {
// Given mock transports for client and server
const mockTransportToClient = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
const mockTransportToServer = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
// When setting up the proxy
mcpProxy({
transportToClient: mockTransportToClient,
transportToServer: mockTransportToServer,
ignoredTools: [],
})
// And when server transport closes
if (mockTransportToServer.onclose) {
mockTransportToServer.onclose()
}
// Then client transport should also be closed
expect(mockTransportToClient.close).toHaveBeenCalled()
})
it('Scenario: Filter tools in tools/list response when ignoredTools is configured', async () => {
// Given mock transports for client and server
const mockTransportToClient = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
const mockTransportToServer = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
// When setting up the proxy with ignored tools
mcpProxy({
transportToClient: mockTransportToClient,
transportToServer: mockTransportToServer,
ignoredTools: ['delete*', 'remove*'],
})
// First simulate client sending a tools/list request
const toolsListRequest = {
jsonrpc: '2.0' as const,
method: 'tools/list',
id: '2',
params: {},
}
if (mockTransportToClient.onmessage) {
mockTransportToClient.onmessage(toolsListRequest)
}
// Clear the previous call
vi.clearAllMocks()
// Now simulate server sending a tools/list response with various tools
const serverToolsResponse = {
jsonrpc: '2.0' as const,
id: '2',
result: {
tools: [
{ name: 'createTask', description: 'Create a new task' },
{ name: 'deleteTask', description: 'Delete a task' },
{ name: 'updateTask', description: 'Update a task' },
{ name: 'removeUser', description: 'Remove a user' },
{ name: 'listTasks', description: 'List all tasks' },
],
},
}
// Simulate server sending a response
if (mockTransportToServer.onmessage) {
mockTransportToServer.onmessage(serverToolsResponse)
}
// Then the response should be forwarded to the client with filtered tools
expect(mockTransportToClient.send).toHaveBeenCalledWith(
expect.objectContaining({
jsonrpc: '2.0',
id: '2',
result: {
tools: [
{ name: 'createTask', description: 'Create a new task' },
{ name: 'updateTask', description: 'Update a task' },
{ name: 'listTasks', description: 'List all tasks' },
],
},
}),
)
})
it('Scenario: Block tools/call for ignored tools with delete* filter', async () => {
// Given mock transports for client and server
const mockTransportToClient = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
const mockTransportToServer = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
// When setting up the proxy with delete* filter
mcpProxy({
transportToClient: mockTransportToClient,
transportToServer: mockTransportToServer,
ignoredTools: ['delete*'],
})
// And when client tries to call a deleteTask tool
const toolsCallMessage = {
jsonrpc: '2.0' as const,
method: 'tools/call',
id: '3',
params: {
name: 'deleteTask',
arguments: {
taskId: '1',
},
_meta: {
progressToken: 1,
},
},
}
// Simulate client sending the tools/call message
if (mockTransportToClient.onmessage) {
mockTransportToClient.onmessage(toolsCallMessage)
}
// Then the call should NOT be forwarded to the server
expect(mockTransportToServer.send).not.toHaveBeenCalled()
// And an error response should be sent back to the client
expect(mockTransportToClient.send).toHaveBeenCalledWith(
expect.objectContaining({
jsonrpc: '2.0',
id: '3',
error: expect.objectContaining({
code: expect.any(Number),
message: expect.stringContaining('Tool "deleteTask" is not available'),
}),
}),
)
})
it('Scenario: Handle server-initiated requests (without corresponding client request)', async () => {
// Given mock transports for client and server
const mockTransportToClient = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
const mockTransportToServer = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
// When setting up the proxy
mcpProxy({
transportToClient: mockTransportToClient,
transportToServer: mockTransportToServer,
ignoredTools: [],
})
// And when server sends a ping message (server-initiated, no corresponding client request)
const serverPingMessage = {
jsonrpc: '2.0' as const,
method: 'ping',
id: 'server-ping-1',
}
// Simulate server sending the message
if (mockTransportToServer.onmessage) {
mockTransportToServer.onmessage(serverPingMessage)
}
// Then the message should be forwarded to the client without errors
expect(mockTransportToClient.send).toHaveBeenCalledWith(
expect.objectContaining({
jsonrpc: '2.0',
method: 'ping',
id: 'server-ping-1',
}),
)
})
it('Scenario: Handle server-initiated response messages without corresponding request', async () => {
// Given mock transports for client and server
const mockTransportToClient = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
const mockTransportToServer = {
send: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
start: vi.fn().mockResolvedValue(undefined),
onmessage: vi.fn(),
onclose: vi.fn(),
onerror: vi.fn(),
} as unknown as Transport
// When setting up the proxy
mcpProxy({
transportToClient: mockTransportToClient,
transportToServer: mockTransportToServer,
ignoredTools: [],
})
// And when server sends a response with an ID that has no corresponding request
const orphanedResponse = {
jsonrpc: '2.0' as const,
id: 'unknown-request-id',
result: {},
}
// Simulate server sending a response without a matching request
if (mockTransportToServer.onmessage) {
mockTransportToServer.onmessage(orphanedResponse)
}
// Then the response should still be forwarded to the client
expect(mockTransportToClient.send).toHaveBeenCalledWith(
expect.objectContaining({
jsonrpc: '2.0',
id: 'unknown-request-id',
result: {},
}),
)
})
})
describe('setupOAuthCallbackServerWithLongPoll', () => {
let server: any
let events: EventEmitter