{{ message }}
Repository navigation
Expand file tree
/
Copy pathmcp_test.go
More file actions
904 lines (848 loc) · 25.1 KB
/
Copy pathmcp_test.go
File metadata and controls
904 lines (848 loc) · 25.1 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
//go:build !integration
package parser
import (
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/github/gh-aw/pkg/types"
"github.com/github/gh-aw/pkg/constants"
)
// TestEnsureLocalhostDomains tests the helper function that ensures localhost domains are always included
func TestExtractMCPConfigurations(t *testing.T) {
tests := []struct {
name string
frontmatter map[string]any
serverFilter string
expected []RegistryMCPServerConfig
expectError bool
expectedErrContains string
}{
{
name: "GitHub tool with read-only true",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{
"read-only": true,
},
},
},
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:" + string(constants.DefaultGitHubMCPServerVersion),
},
Env: map[string]string{
"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}",
}}, Name: "github",
Allowed: []string{},
},
},
},
{
name: "GitHub tool with read-only false (always enforced as read-only)",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{
"read-only": false,
},
},
},
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:" + string(constants.DefaultGitHubMCPServerVersion),
},
Env: map[string]string{
"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}",
}}, Name: "github",
Allowed: []string{},
},
},
},
{
name: "GitHub tool with boolean true (shorthand)",
frontmatter: map[string]any{
"tools": map[string]any{
"github": true,
},
},
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:" + string(constants.DefaultGitHubMCPServerVersion),
},
Env: map[string]string{
"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}",
}}, Name: "github",
},
},
},
{
name: "GitHub tool without read-only (default behavior)",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{},
},
},
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:" + string(constants.DefaultGitHubMCPServerVersion),
},
Env: map[string]string{
"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}",
}}, Name: "github",
Allowed: []string{},
},
},
},
{
name: "Empty frontmatter",
frontmatter: map[string]any{},
expected: []RegistryMCPServerConfig{},
},
{
name: "No tools section",
frontmatter: map[string]any{
"name": "test-workflow",
"on": "push",
},
expected: []RegistryMCPServerConfig{},
},
{
name: "Serena tool is removed",
frontmatter: map[string]any{
"tools": map[string]any{
"serena": true,
},
},
expectError: true,
expectedErrContains: "tools.serena is removed",
},
{
name: "mcp-servers type error takes precedence over removed serena",
frontmatter: map[string]any{
"mcp-servers": "invalid",
"tools": map[string]any{
"serena": true,
},
},
expectError: true,
expectedErrContains: "mcp-servers section must be a map",
},
{
name: "GitHub tool default configuration",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{},
},
},
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:" + string(constants.DefaultGitHubMCPServerVersion),
},
Env: map[string]string{"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}"}}, Name: "github",
Allowed: []string{},
},
},
},
{
name: "GitHub tool with custom configuration",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{
"allowed": []any{"issue_create", "pull_request_list"},
"version": "latest",
},
},
},
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:latest",
},
Env: map[string]string{"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}"}}, Name: "github",
Allowed: []string{"issue_create", "pull_request_list"},
},
},
},
{
name: "GitHub tool with integer version",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{
"version": 20,
},
},
},
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:20",
},
Env: map[string]string{"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}"}}, Name: "github",
Allowed: []string{},
},
},
},
{
name: "GitHub tool with float version",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{
"version": 3.11,
},
},
},
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:3.11",
},
Env: map[string]string{"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}"}}, Name: "github",
Allowed: []string{},
},
},
},
{
name: "Server filter - matching",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{},
"custom": map[string]any{
"mcp": map[string]any{
"type": "stdio",
"command": "custom-server",
},
},
},
},
serverFilter: "github",
expected: []RegistryMCPServerConfig{
{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "docker",
Command: "docker",
Args: []string{
"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"-e", "GITHUB_READ_ONLY=1",
"ghcr.io/github/github-mcp-server:" + string(constants.DefaultGitHubMCPServerVersion),
},
Env: map[string]string{"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN_REQUIRED}"}}, Name: "github",
Allowed: []string{},
},
},
},
{
name: "Server filter - no match",
frontmatter: map[string]any{
"tools": map[string]any{
"github": map[string]any{},
"custom": map[string]any{
"mcp": map[string]any{
"type": "stdio",
"command": "custom-server",
},
},
},
},
serverFilter: "nomatch",
expected: []RegistryMCPServerConfig{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := ExtractMCPConfigurations(tt.frontmatter, tt.serverFilter)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
return
}
if tt.expectedErrContains != "" && !strings.Contains(err.Error(), tt.expectedErrContains) {
t.Errorf("Expected error containing %q, got %q", tt.expectedErrContains, err.Error())
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if len(result) != len(tt.expected) {
t.Errorf("Expected %d configs, got %d", len(tt.expected), len(result))
return
}
for i, expected := range tt.expected {
if i >= len(result) {
t.Errorf("Missing config at index %d", i)
continue
}
actual := result[i]
if actual.Name != expected.Name {
t.Errorf("Config %d: expected name %q, got %q", i, expected.Name, actual.Name)
}
if actual.Type != expected.Type {
t.Errorf("Config %d: expected type %q, got %q", i, expected.Type, actual.Type)
}
if actual.Command != expected.Command {
t.Errorf("Config %d: expected command %q, got %q", i, expected.Command, actual.Command)
}
if !reflect.DeepEqual(actual.Args, expected.Args) {
t.Errorf("Config %d: expected args %v, got %v", i, expected.Args, actual.Args)
}
// For GitHub configurations, just check that GITHUB_PERSONAL_ACCESS_TOKEN exists
// The actual value depends on environment and may be a real token or placeholder
if actual.Name == "github" {
if _, hasToken := actual.Env["GITHUB_PERSONAL_ACCESS_TOKEN"]; !hasToken {
t.Errorf("Config %d: GitHub config missing GITHUB_PERSONAL_ACCESS_TOKEN", i)
}
} else {
if !reflect.DeepEqual(actual.Env, expected.Env) {
t.Errorf("Config %d: expected env %v, got %v", i, expected.Env, actual.Env)
}
}
// Compare allowed tools, handling nil vs empty slice equivalence
actualAllowed := actual.Allowed
if actualAllowed == nil {
actualAllowed = []string{}
}
expectedAllowed := expected.Allowed
if expectedAllowed == nil {
expectedAllowed = []string{}
}
if !reflect.DeepEqual(actualAllowed, expectedAllowed) {
t.Errorf("Config %d: expected allowed %v, got %v", i, expectedAllowed, actualAllowed)
}
}
})
}
}
func TestParseMCPConfig(t *testing.T) {
tests := []struct {
name string
toolName string
mcpSection any
toolConfig map[string]any
expected RegistryMCPServerConfig
expectError bool
}{
{
name: "Stdio with command and args",
toolName: "test-server",
mcpSection: map[string]any{
"type": "stdio",
"command": "/usr/bin/server",
"args": []any{"--verbose", "--config=/etc/config.yml"},
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Command: "/usr/bin/server",
Args: []string{"--verbose", "--config=/etc/config.yml"},
Env: map[string]string{},
Headers: map[string]string{}}, Name: "test-server",
Allowed: []string{},
},
},
{
name: "Stdio with container",
toolName: "docker-server",
mcpSection: map[string]any{
"type": "stdio",
"container": "myregistry/server:latest",
"env": map[string]any{
"DEBUG": "1",
"API_URL": "https://api.example.com",
},
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Container: "myregistry/server:latest",
Command: "docker",
Args: []string{"run", "--rm", "-i", "-e", "DEBUG", "-e", "API_URL", "myregistry/server:latest"},
Env: map[string]string{
"DEBUG": "1",
"API_URL": "https://api.example.com",
},
Headers: map[string]string{}}, Name: "docker-server",
Allowed: []string{},
},
},
{
name: "HTTP server",
toolName: "http-server",
mcpSection: map[string]any{
"type": "http",
"url": "https://mcp.example.com/api",
"headers": map[string]any{
"Authorization": "Bearer token123",
"User-Agent": "gh-aw/1.0",
},
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "http",
URL: "https://mcp.example.com/api",
Headers: map[string]string{
"Authorization": "Bearer token123",
"User-Agent": "gh-aw/1.0",
},
Env: map[string]string{}}, Name: "http-server",
Allowed: []string{},
},
},
{
name: "HTTP server with underscored headers",
toolName: "datadog-server",
mcpSection: map[string]any{
"type": "http",
"url": "https://mcp.datadoghq.com/api/unstable/mcp-server/mcp",
"headers": map[string]any{
"DD_API_KEY": "test-api-key",
"DD_APPLICATION_KEY": "test-app-key",
"DD_SITE": "datadoghq.com",
},
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "http",
URL: "https://mcp.datadoghq.com/api/unstable/mcp-server/mcp",
Headers: map[string]string{
"DD_API_KEY": "test-api-key",
"DD_APPLICATION_KEY": "test-app-key",
"DD_SITE": "datadoghq.com",
},
Env: map[string]string{}}, Name: "datadog-server",
Allowed: []string{},
},
},
{
name: "With allowed tools",
toolName: "server-with-allowed",
mcpSection: map[string]any{
"type": "stdio",
"command": "server",
},
toolConfig: map[string]any{
"allowed": []any{"tool1", "tool2", "tool3"},
},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Command: "server",
Env: map[string]string{},
Headers: map[string]string{}}, Name: "server-with-allowed",
Allowed: []string{"tool1", "tool2", "tool3"},
},
},
{
name: "JSON string config",
toolName: "json-server",
mcpSection: `{
"type": "stdio",
"command": "python",
"args": ["-m", "mcp_server"],
"env": {
"PYTHON_PATH": "/opt/python"
}
}`,
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Command: "python",
Args: []string{"-m", "mcp_server"},
Env: map[string]string{
"PYTHON_PATH": "/opt/python",
},
Headers: map[string]string{}}, Name: "json-server",
Allowed: []string{},
},
},
{
name: "Stdio with environment variables",
toolName: "env-server",
mcpSection: map[string]any{
"type": "stdio",
"command": "server",
"env": map[string]any{
"LOG_LEVEL": "debug",
"PORT": "8080",
},
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Command: "server",
Env: map[string]string{
"LOG_LEVEL": "debug",
"PORT": "8080",
},
Headers: map[string]string{}}, Name: "env-server",
Allowed: []string{},
},
},
// Error cases
{
name: "Type inferred from command field",
toolName: "inferred-stdio",
mcpSection: map[string]any{"command": "server"},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Command: "server",
Args: nil,
Env: map[string]string{},
Headers: map[string]string{}}, Name: "inferred-stdio",
Allowed: nil,
},
},
{
name: "Stdio with network proxy-args (new format)",
toolName: "network-proxy-server",
mcpSection: map[string]any{
"type": "stdio",
"command": "docker",
"args": []any{"run", "myserver"},
"network": map[string]any{
"allowed": []any{"example.com", "api.example.com"},
"proxy-args": []any{"--network-proxy-arg1", "--network-proxy-arg2"},
},
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Command: "docker",
Args: []string{"run", "myserver"},
Env: map[string]string{},
Headers: map[string]string{}}, Name: "network-proxy-server",
ProxyArgs: []string{"--network-proxy-arg1", "--network-proxy-arg2"},
Allowed: []string{},
},
},
{
name: "Local type (alias for stdio)",
toolName: "local-server",
mcpSection: map[string]any{
"type": "local",
"command": "local-mcp-server",
"args": []any{"--local-mode"},
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio", // normalized to stdio
Command: "local-mcp-server",
Args: []string{"--local-mode"},
Env: map[string]string{},
Headers: map[string]string{}}, Name: "local-server",
Allowed: []string{},
},
},
{
name: "Stdio with registry",
toolName: "registry-stdio",
mcpSection: map[string]any{
"type": "stdio",
"command": "registry-server",
"registry": "https://registry.example.com/servers/mcp-server",
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Command: "registry-server",
Env: map[string]string{},
Headers: map[string]string{}}, Name: "registry-stdio",
Registry: "https://registry.example.com/servers/mcp-server",
Allowed: []string{},
},
},
{
name: "HTTP with registry",
toolName: "registry-http",
mcpSection: map[string]any{
"type": "http",
"url": "https://api.example.com/mcp",
"registry": "https://registry.example.com/servers/http-mcp",
},
toolConfig: map[string]any{},
expected: RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "http",
URL: "https://api.example.com/mcp",
Headers: map[string]string{},
Env: map[string]string{}}, Name: "registry-http",
Registry: "https://registry.example.com/servers/http-mcp",
Allowed: []string{},
},
},
{
name: "Missing type and no inferrable fields",
toolName: "no-type-no-fields",
mcpSection: map[string]any{"env": map[string]any{"KEY": "value"}},
toolConfig: map[string]any{},
expectError: true,
},
{
name: "Invalid type",
toolName: "invalid-type",
mcpSection: map[string]any{"type": 123},
toolConfig: map[string]any{},
expectError: true,
},
{
name: "Unsupported type",
toolName: "unsupported",
mcpSection: map[string]any{"type": "websocket"},
toolConfig: map[string]any{},
expectError: true,
},
{
name: "Stdio missing command and container",
toolName: "no-command",
mcpSection: map[string]any{"type": "stdio"},
toolConfig: map[string]any{},
expectError: true,
},
{
name: "HTTP missing URL",
toolName: "no-url",
mcpSection: map[string]any{"type": "http"},
toolConfig: map[string]any{},
expectError: true,
},
{
name: "Invalid JSON string",
toolName: "invalid-json",
mcpSection: `{"invalid": json}`,
toolConfig: map[string]any{},
expectError: true,
},
{
name: "Invalid config format",
toolName: "invalid-format",
mcpSection: 123,
toolConfig: map[string]any{},
expectError: true,
},
{
name: "Invalid command type",
toolName: "invalid-command",
mcpSection: map[string]any{
"type": "stdio",
"command": 123, // Should be string
},
toolConfig: map[string]any{},
expectError: true,
},
{
name: "Invalid URL type",
toolName: "invalid-url",
mcpSection: map[string]any{
"type": "http",
"url": 123, // Should be string
},
toolConfig: map[string]any{},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := ParseMCPConfig(tt.toolName, tt.mcpSection, tt.toolConfig)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if result.Name != tt.expected.Name {
t.Errorf("Expected name %q, got %q", tt.expected.Name, result.Name)
}
if result.Type != tt.expected.Type {
t.Errorf("Expected type %q, got %q", tt.expected.Type, result.Type)
}
if result.Command != tt.expected.Command {
t.Errorf("Expected command %q, got %q", tt.expected.Command, result.Command)
}
if result.Container != tt.expected.Container {
t.Errorf("Expected container %q, got %q", tt.expected.Container, result.Container)
}
if result.URL != tt.expected.URL {
t.Errorf("Expected URL %q, got %q", tt.expected.URL, result.URL)
}
// For Docker containers, the environment variable order in args may vary
// due to map iteration order, so check for presence rather than exact order
if result.Container != "" {
// Check that all expected elements are present in args
expectedElements := make(map[string]struct{})
for _, arg := range tt.expected.Args {
expectedElements[arg] = struct{}{}
}
actualElements := make(map[string]struct{})
for _, arg := range result.Args {
actualElements[arg] = struct{}{}
}
if !reflect.DeepEqual(expectedElements, actualElements) {
t.Errorf("Expected args elements %v, got %v", tt.expected.Args, result.Args)
}
} else {
if !reflect.DeepEqual(result.Args, tt.expected.Args) {
t.Errorf("Expected args %v, got %v", tt.expected.Args, result.Args)
}
}
if !reflect.DeepEqual(result.Headers, tt.expected.Headers) {
t.Errorf("Expected headers %v, got %v", tt.expected.Headers, result.Headers)
}
if !reflect.DeepEqual(result.Env, tt.expected.Env) {
t.Errorf("Expected env %v, got %v", tt.expected.Env, result.Env)
}
// Compare allowed tools, handling nil vs empty slice equivalence
actualAllowed := result.Allowed
if actualAllowed == nil {
actualAllowed = []string{}
}
expectedAllowed := tt.expected.Allowed
if expectedAllowed == nil {
expectedAllowed = []string{}
}
if !reflect.DeepEqual(actualAllowed, expectedAllowed) {
t.Errorf("Expected allowed %v, got %v", expectedAllowed, actualAllowed)
}
// Compare proxy args, handling nil vs empty slice equivalence
actualProxyArgs := result.ProxyArgs
if actualProxyArgs == nil {
actualProxyArgs = []string{}
}
expectedProxyArgs := tt.expected.ProxyArgs
if expectedProxyArgs == nil {
expectedProxyArgs = []string{}
}
if !reflect.DeepEqual(actualProxyArgs, expectedProxyArgs) {
t.Errorf("Expected proxy-args %v, got %v", expectedProxyArgs, actualProxyArgs)
}
})
}
}
// TestMCPConfigTypes tests the struct types for proper JSON serialization
func TestMCPConfigTypes(t *testing.T) {
// Test that our structs can be properly marshaled/unmarshaled
config := RegistryMCPServerConfig{BaseMCPServerConfig: types.BaseMCPServerConfig{Type: "stdio",
Command: "test-command",
Args: []string{"arg1", "arg2"},
Env: map[string]string{"KEY": "value"},
Headers: map[string]string{"Content-Type": "application/json"}}, Name: "test-server",
ProxyArgs: []string{"--proxy-test"},
Allowed: []string{"tool1", "tool2"},
}
// Marshal to JSON
jsonData, err := json.Marshal(config)
if err != nil {
t.Errorf("Failed to marshal config: %v", err)
}
// Unmarshal from JSON
var decoded RegistryMCPServerConfig
if err := json.Unmarshal(jsonData, &decoded); err != nil {
t.Errorf("Failed to unmarshal config: %v", err)
}
// Compare
if !reflect.DeepEqual(config, decoded) {
t.Errorf("Config changed after marshal/unmarshal cycle")
}
}
// TestIsMCPType tests the unified IsMCPType function
func TestIsMCPType(t *testing.T) {
tests := []struct {
name string
typeStr string
expected bool
}{
{
name: "stdio type",
typeStr: "stdio",
expected: true,
},
{
name: "http type",
typeStr: "http",
expected: true,
},
{
name: "local type (alias for stdio)",
typeStr: "local",
expected: true,
},
{
name: "empty string",
typeStr: "",
expected: false,
},
{
name: "unknown type",
typeStr: "unknown",
expected: false,
},
{
name: "docker type (not valid MCP type)",
typeStr: "docker",
expected: false,
},
{
name: "websocket type (not valid)",
typeStr: "websocket",
expected: false,
},
{
name: "grpc type (not valid)",
typeStr: "grpc",
expected: false,
},
{
name: "mixed case STDIO",
typeStr: "STDIO",
expected: false,
},
{
name: "mixed case Http",
typeStr: "Http",
expected: false,
},
{
name: "whitespace padded",
typeStr: " stdio ",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsMCPType(tt.typeStr)
if got != tt.expected {
t.Errorf("IsMCPType(%q) = %v, want %v", tt.typeStr, got, tt.expected)
}
})
}
}
// TestValidMCPTypes tests that ValidMCPTypes constant is properly defined
func TestValidMCPTypes(t *testing.T) {
expected := []string{"stdio", "http", "local"}
if !reflect.DeepEqual(ValidMCPTypes, expected) {
t.Errorf("ValidMCPTypes = %v, want %v", ValidMCPTypes, expected)
}
// Verify that all types in ValidMCPTypes pass IsMCPType
for _, mcpType := range ValidMCPTypes {
if !IsMCPType(mcpType) {
t.Errorf("IsMCPType(%q) should return true for type in ValidMCPTypes", mcpType)
}
}
}
You can’t perform that action at this time.
