-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathmcp_test.go
More file actions
1227 lines (1199 loc) · 47.5 KB
/
Copy pathmcp_test.go
File metadata and controls
1227 lines (1199 loc) · 47.5 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
package mcp
import (
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/perplexityai/bumblebee/internal/model"
)
func TestInferPackageFromArgs(t *testing.T) {
cases := []struct {
cmd string
args []string
wantSpec string
wantLauncher string
}{
{"/usr/bin/npx", []string{"-y", "@modelcontextprotocol/server-github"}, "@modelcontextprotocol/server-github", ""},
{"uvx", []string{"mcp-server-time"}, "mcp-server-time", "uv"},
{"python3", []string{"-m", "mypkg.server"}, "python:mypkg.server", ""},
{"node", []string{"/x/y.js"}, "", ""},
{"npx", []string{"-y", "@playwright/mcp@latest"}, "@playwright/mcp@latest", ""},
{"npx", []string{"left-pad@1.2.3"}, "left-pad@1.2.3", ""},
// pnpm/yarn/bun/npm wrappers take a subcommand before the package;
// the subcommand must not be returned as the package name.
{"pnpm", []string{"dlx", "@modelcontextprotocol/server-github"}, "@modelcontextprotocol/server-github", ""},
{"pnpm", []string{"dlx", "-y", "@playwright/mcp@latest"}, "@playwright/mcp@latest", ""},
{"yarn", []string{"dlx", "@modelcontextprotocol/server-time"}, "@modelcontextprotocol/server-time", ""},
{"bun", []string{"x", "left-pad@1.2.3"}, "left-pad@1.2.3", ""},
{"npm", []string{"exec", "--", "@modelcontextprotocol/server-github"}, "@modelcontextprotocol/server-github", ""},
{"/usr/local/bin/pnpm", []string{"dlx", "mcp-server-time"}, "mcp-server-time", ""},
// npm exec --package= / --package <pkg>: package is named via flag, not positional.
{"npm", []string{"exec", "--package=@modelcontextprotocol/server-time", "--", "server-time"}, "@modelcontextprotocol/server-time", ""},
{"npm", []string{"exec", "--package", "@modelcontextprotocol/server-time", "--", "server-time"}, "@modelcontextprotocol/server-time", ""},
// pipx: launcher must be "pipx", not empty or indistinguishable.
{"pipx", []string{"run", "mcp-server-time"}, "mcp-server-time", "pipx"},
{"pipx", []string{"run", "--spec", "bugcrowd-mcp", "bugcrowd"}, "bugcrowd-mcp", "pipx"},
{"/usr/local/bin/pipx", []string{"run", "some-mcp"}, "some-mcp", "pipx"},
// uv + uv tool run, including --from override.
{"uv", []string{"tool", "run", "mcp-server-time"}, "mcp-server-time", "uv"},
// "uv run <path>" invokes a local script or directory project, not
// a published package — the first positional is not a package
// identity, so the spec must be empty. The caller falls back to
// the server id with low confidence.
{"uv", []string{"run", "--directory", "./backend", "mcps/foo.py"}, "", "uv"},
{"uv", []string{"run", "mcps/foo.py"}, "", "uv"},
{"uv", []string{"tool", "run", "--from", "bugcrowd-mcp", "bugcrowd"}, "bugcrowd-mcp", "uv"},
// "uv run --from <pkg>" is a published-package invocation even
// without "tool"; honor the --from name.
{"uv", []string{"run", "--from", "bugcrowd-mcp", "bugcrowd"}, "bugcrowd-mcp", "uv"},
// docker run with various flags before the image.
{"docker", []string{"run", "-i", "--rm", "ghcr.io/example-org/example-mcp:latest"}, "ghcr.io/example-org/example-mcp:latest", "docker"},
{"docker", []string{"run", "-e", "FOO=bar", "--name", "x", "mcp/slack"}, "mcp/slack", "docker"},
{"docker", []string{"run", "--env-file=.env", "ghcr.io/github/github-mcp-server"}, "ghcr.io/github/github-mcp-server", "docker"},
{"/usr/local/bin/docker", []string{"run", "mcp/slack"}, "mcp/slack", "docker"},
// Non-executor first tokens name no published package: the spec is
// empty and ScanConfig falls back to the server id, like `uv run
// <script>` below. The case body is identical for npm/pnpm/yarn/bun,
// so one row per distinct shape suffices.
//
// `run <script>` — incl. the reported `bun run … start` from the
// official Claude messaging plugins, which had leaked package "start".
{"bun", []string{"run", "--cwd", "/x", "--shell=bun", "--silent", "start"}, "", ""},
{"npm", []string{"run", "start"}, "", ""},
// Bare `<script>`: an npm lifecycle alias, a plain script, and one with
// a trailing flag (which must not be mistaken for the package).
{"npm", []string{"start"}, "", ""},
{"yarn", []string{"dev"}, "", ""},
{"yarn", []string{"build", "--port", "3000"}, "", ""},
// create/init DO name a published create-<name> package, but resolving
// that is intentionally out of scope (initializers don't launch MCP
// servers); pinned so the fallback is deliberate, not an accidental drop.
{"npm", []string{"create", "vite"}, "", ""},
{"npm", []string{"init", "foo"}, "", ""},
// Known limitation: a value-taking global flag NOT in npmValueTakingFlags
// before the subcommand shifts detection, missing a genuine `exec`
// package. Does not occur in real MCP configs; documented, not desired.
{"npm", []string{"--unknownflag", "val", "exec", "real-pkg"}, "", ""},
}
for _, c := range cases {
gotSpec, gotLauncher := inferPackageFromArgs(c.cmd, c.args)
if gotSpec != c.wantSpec || gotLauncher != c.wantLauncher {
t.Errorf("inferPackageFromArgs(%q,%v) = (%q,%q), want (%q,%q)",
c.cmd, c.args, gotSpec, gotLauncher, c.wantSpec, c.wantLauncher)
}
}
}
func TestSplitSpec(t *testing.T) {
cases := []struct {
in string
wantName string
wantSel string
}{
{"@playwright/mcp@latest", "@playwright/mcp", "@latest"},
{"@playwright/mcp", "@playwright/mcp", ""},
{"@scope/name@1.2.3", "@scope/name", "@1.2.3"},
{"left-pad@1.2.3", "left-pad", "@1.2.3"},
{"mcp-server-time", "mcp-server-time", ""},
{"python:mypkg.server", "python:mypkg.server", ""},
{"", "", ""},
// npm alias selectors must not be misparsed: the selector starts at
// the '@' before "npm:", not at the trailing version. Otherwise the
// alias target's own version is attributed to the host package and
// name extraction is wrong.
{"pkg@npm:alias@1.0.0", "pkg", "@npm:alias@1.0.0"},
{"@scope/pkg@npm:other@2.0", "@scope/pkg", "@npm:other@2.0"},
{"pkg@npm:alias", "pkg", "@npm:alias"},
// Alias target may itself be scoped: "host@npm:@scope/other@1.0.0".
{"host@npm:@scope/other@1.0.0", "host", "@npm:@scope/other@1.0.0"},
}
for _, c := range cases {
gotName, gotSel := splitSpec(c.in)
if gotName != c.wantName || gotSel != c.wantSel {
t.Errorf("splitSpec(%q) = (%q,%q), want (%q,%q)", c.in, gotName, gotSel, c.wantName, c.wantSel)
}
}
}
func TestScanConfig(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
body := `{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {"GITHUB_TOKEN":"shouldnotbecaptured"}
},
"time": {
"command": "uvx",
"args": ["mcp-server-time"]
}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
if len(out) != 2 {
t.Fatalf("want 2 records, got %d", len(out))
}
sort.Slice(out, func(i, j int) bool { return out[i].PackageName < out[j].PackageName })
if out[0].PackageName != "@modelcontextprotocol/server-github" {
t.Errorf("inferred spec should set PackageName: %+v", out[0])
}
if out[0].ServerName != "github" {
t.Errorf("ServerName not preserved: %+v", out[0])
}
if out[0].RootKind != model.RootKindMCPConfig {
t.Errorf("RootKind = %q, want %q", out[0].RootKind, model.RootKindMCPConfig)
}
if out[0].Version != "" {
t.Errorf("Version should be empty for MCP records: %q", out[0].Version)
}
if out[0].RequestedSpec != "" {
t.Errorf("RequestedSpec should be empty when no selector: %q", out[0].RequestedSpec)
}
if out[1].PackageName != "mcp-server-time" {
t.Errorf("inferred spec should set PackageName: %+v", out[1])
}
if out[1].ServerName != "time" {
t.Errorf("ServerName not preserved: %+v", out[1])
}
if out[0].Confidence != "low" {
t.Errorf("confidence: %q", out[0].Confidence)
}
}
func TestScanConfig_FlatShape(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, ".mcp.json")
body := `{
"linear": {"command":"npx","args":["-y","@modelcontextprotocol/server-linear"]}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
if len(out) != 1 || out[0].PackageName != "@modelcontextprotocol/server-linear" {
t.Fatalf("flat shape: %+v", out)
}
if out[0].ServerName != "linear" {
t.Errorf("ServerName not preserved: %+v", out[0])
}
if out[0].RootKind != model.RootKindMCPConfig {
t.Errorf("RootKind = %q, want %q", out[0].RootKind, model.RootKindMCPConfig)
}
}
// TestScanConfig_DockerAndUV verifies that container/python-tool launchers
// produce records identified by image ref or tool name and tag the
// package_manager so exposure matching does not treat them as npm.
func TestScanConfig_DockerAndUV(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
body := `{
"mcpServers": {
"example": {"command":"docker","args":["run","-i","--rm","ghcr.io/example-org/example-mcp:latest"]},
"github": {"command":"docker","args":["run","-e","TOKEN","ghcr.io/github/github-mcp-server"]},
"slack": {"command":"docker","args":["run","mcp/slack"]},
"bugcrowd": {"command":"uv","args":["tool","run","--from","bugcrowd-mcp","bugcrowd"]},
"time": {"command":"uvx","args":["mcp-server-time"]},
"plugin": {"command":"node","args":["${CLAUDE_PLUGIN_ROOT}/dist/index.js"]}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
if len(out) != 6 {
t.Fatalf("want 6 records, got %d: %+v", len(out), out)
}
byServer := map[string]model.Record{}
for _, r := range out {
byServer[r.ServerName] = r
}
// Pinned image ref: tag must be split off into Version, leaving
// PackageName as the bare image name.
if r := byServer["example"]; r.PackageName != "ghcr.io/example-org/example-mcp" || r.Version != "latest" || r.PackageManager != "docker" {
t.Errorf("example: %+v", r)
}
// No tag in the ref: Version stays empty.
if r := byServer["github"]; r.PackageName != "ghcr.io/github/github-mcp-server" || r.Version != "" || r.PackageManager != "docker" {
t.Errorf("github: %+v", r)
}
if r := byServer["slack"]; r.PackageName != "mcp/slack" || r.Version != "" || r.PackageManager != "docker" {
t.Errorf("slack: %+v", r)
}
if r := byServer["bugcrowd"]; r.PackageName != "bugcrowd-mcp" || r.PackageManager != "uv" {
t.Errorf("bugcrowd: %+v", r)
}
if r := byServer["time"]; r.PackageName != "mcp-server-time" || r.PackageManager != "uv" {
t.Errorf("time: %+v", r)
}
// Unresolved ${CLAUDE_PLUGIN_ROOT} should fall back to the server id,
// not be emitted as a literal package name.
if r := byServer["plugin"]; r.PackageName != "plugin" {
t.Errorf("plugin (unresolved shell var): %+v", r)
}
}
// TestSplitDockerImageRef covers the colon-tag splitter for OCI image refs.
// The interesting cases: an unambiguous tag, no tag at all, a
// registry-port reference whose first colon must NOT be treated as the
// tag separator, and a digest reference which should be left intact.
func TestSplitDockerImageRef(t *testing.T) {
cases := []struct {
in string
wantName string
wantTag string
}{
{"hashicorp/terraform-mcp-server:0.4.0", "hashicorp/terraform-mcp-server", "0.4.0"},
{"ghcr.io/example-org/example-mcp:latest", "ghcr.io/example-org/example-mcp", "latest"},
{"ghcr.io/github/github-mcp-server", "ghcr.io/github/github-mcp-server", ""},
{"mcp/slack", "mcp/slack", ""},
// Registry port: the colon before the port lives in the registry
// segment (before the last slash) and must not be split.
{"localhost:5000/foo/bar:1.2.3", "localhost:5000/foo/bar", "1.2.3"},
{"localhost:5000/foo/bar", "localhost:5000/foo/bar", ""},
{"registry.example.com:443/team/img:v1", "registry.example.com:443/team/img", "v1"},
// Digest refs are returned with the digest preserved on the name
// side and an empty tag.
{"alpine@sha256:abc123", "alpine@sha256:abc123", ""},
{"ghcr.io/foo/bar@sha256:deadbeef", "ghcr.io/foo/bar@sha256:deadbeef", ""},
// tag@digest form: preserve the digest on the name and split the
// tag off into version. The digest is the immutable identity; the
// tag is the human-readable version pointer.
{"alpine:3.19@sha256:abc", "alpine@sha256:abc", "3.19"},
{"ghcr.io/foo/bar:1.2.3@sha256:deadbeef", "ghcr.io/foo/bar@sha256:deadbeef", "1.2.3"},
// Single-segment image with a tag.
{"alpine:3.19", "alpine", "3.19"},
{"", "", ""},
}
for _, c := range cases {
gotName, gotTag := splitDockerImageRef(c.in)
if gotName != c.wantName || gotTag != c.wantTag {
t.Errorf("splitDockerImageRef(%q) = (%q,%q), want (%q,%q)", c.in, gotName, gotTag, c.wantName, c.wantTag)
}
}
}
// TestScanConfig_DockerPinnedTag exercises the end-to-end record shape
// for a pinned image ref: PackageName must lose the tag, Version must
// carry the tag, PackageManager stays "docker".
func TestScanConfig_DockerPinnedTag(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
body := `{
"mcpServers": {
"terraform": {"command":"docker","args":["run","-i","--rm","hashicorp/terraform-mcp-server:0.4.0"]},
"localreg": {"command":"docker","args":["run","localhost:5000/team/img:1.2.3"]},
"untagged": {"command":"docker","args":["run","ghcr.io/example-org/example-mcp"]}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
byServer := map[string]model.Record{}
for _, r := range out {
byServer[r.ServerName] = r
}
if r := byServer["terraform"]; r.PackageName != "hashicorp/terraform-mcp-server" || r.NormalizedName != "hashicorp/terraform-mcp-server" || r.Version != "0.4.0" || r.PackageManager != "docker" {
t.Errorf("terraform: %+v", r)
}
if r := byServer["localreg"]; r.PackageName != "localhost:5000/team/img" || r.Version != "1.2.3" || r.PackageManager != "docker" {
t.Errorf("localreg: %+v", r)
}
if r := byServer["untagged"]; r.PackageName != "ghcr.io/example-org/example-mcp" || r.Version != "" || r.PackageManager != "docker" {
t.Errorf("untagged: %+v", r)
}
}
// TestScanConfig_UVRunDirectory verifies that "uv run --directory <dir>
// <script>" does not leak the directory path as a package name. The
// record falls back to the server id with low confidence so a literal
// "./backend" cannot drive a catalog hit.
func TestScanConfig_UVRunDirectory(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
body := `{
"mcpServers": {
"bugcrowd": {"command":"uv","args":["run","--directory","./backend","mcps/bugcrowd.py"]},
"github_extended": {"command":"uv","args":["run","--directory","./backend","mcps/github_extended.py"]},
"from-flag": {"command":"uv","args":["run","--from","bugcrowd-mcp","bugcrowd"]}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
byServer := map[string]model.Record{}
for _, r := range out {
byServer[r.ServerName] = r
}
if r := byServer["bugcrowd"]; r.PackageName != "bugcrowd" || r.PackageManager != "uv" || r.Confidence != "low" {
t.Errorf("bugcrowd: %+v", r)
}
if r := byServer["github_extended"]; r.PackageName != "github_extended" || r.PackageManager != "uv" {
t.Errorf("github_extended: %+v", r)
}
// --from still wins even when "tool" is absent.
if r := byServer["from-flag"]; r.PackageName != "bugcrowd-mcp" || r.PackageManager != "uv" {
t.Errorf("from-flag: %+v", r)
}
}
// TestScanConfig_RealWorldCorpus runs the parser over command/args shapes
// taken from real MCP configurations (the modelcontextprotocol servers, the
// Claude/Cursor setup guides, and the official Claude bundled-server
// plugins). It is the regression guard for the script-runner change: genuine
// package launchers (npx/uvx/docker) must keep their package identity, while
// local-script launchers (`bun run … start`, bare `yarn`/`pnpm`, `node` file,
// `npm create`) must fall back to the server id rather than leak a script or
// initializer-verb token as a package.
func TestScanConfig_RealWorldCorpus(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
body := `{
"mcpServers": {
"seq-think": {"command":"npx","args":["-y","@modelcontextprotocol/server-sequential-thinking"]},
"omnisearch": {"command":"npx","args":["-y","mcp-omnisearch"]},
"sqlite": {"command":"uvx","args":["mcp-server-sqlite","--db-path","test.db"]},
"chronulus": {"command":"uvx","args":["chronulus-mcp"]},
"github": {"command":"docker","args":["run","-i","--rm","ghcr.io/github/github-mcp-server"]},
"local-node": {"command":"node","args":["/home/u/mcp-tools/build/index.js"]},
"discord": {"command":"bun","args":["run","--cwd","/plugins/discord","--shell=bun","--silent","start"]},
"yarn-dev": {"command":"yarn","args":["dev"]},
"scaffold": {"command":"npm","args":["create","vite"]}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
byServer := map[string]model.Record{}
for _, r := range out {
byServer[r.ServerName] = r
}
want := map[string]struct{ pkg, pm string }{
// Genuine package launchers — identity preserved.
"seq-think": {"@modelcontextprotocol/server-sequential-thinking", "mcp"},
"omnisearch": {"mcp-omnisearch", "mcp"},
"sqlite": {"mcp-server-sqlite", "uv"},
"chronulus": {"chronulus-mcp", "uv"},
"github": {"ghcr.io/github/github-mcp-server", "docker"},
// Local-script / non-package launchers — fall back to the server id.
"local-node": {"local-node", "mcp"}, // node <file>
"discord": {"discord", "mcp"}, // claude-plugins-official template: bun run … start
"yarn-dev": {"yarn-dev", "mcp"}, // bare yarn <script>
"scaffold": {"scaffold", "mcp"}, // npm create (initializer, out of scope)
}
for id, w := range want {
r, ok := byServer[id]
if !ok {
t.Fatalf("%s: no record emitted", id)
}
if r.PackageName != w.pkg || r.PackageManager != w.pm {
t.Errorf("%s: got package_name=%q package_manager=%q, want %q/%q",
id, r.PackageName, r.PackageManager, w.pkg, w.pm)
}
}
}
// TestScanConfig_MalformedJSONEmitsWarn verifies that a malformed MCP
// config file is surfaced as a warn diagnostic rather than silently
// swallowed. Operators rely on diagnostics to find configs the scanner
// could not parse.
func TestScanConfig_MalformedJSONEmitsWarn(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
if err := os.WriteFile(path, []byte(`{this is not json`), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
type diag struct {
level, path, msg string
}
var diags []diag
s := &Scanner{
MaxFileSize: 1 << 20,
Emit: func(r model.Record) { out = append(out, r) },
Diag: func(level, p, msg string) { diags = append(diags, diag{level, p, msg}) },
}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatalf("ScanConfig returned error: %v", err)
}
if len(out) != 0 {
t.Fatalf("expected no records, got %+v", out)
}
foundWarn := false
for _, d := range diags {
if d.level == "warn" && d.path == path {
foundWarn = true
}
}
if !foundWarn {
t.Fatalf("expected a warn diagnostic for malformed JSON, got %+v", diags)
}
}
// TestScanConfig_SpecSelector verifies that npm-style version selectors
// ("@latest", "@1.2.3") in MCP args are split off into RequestedSpec
// while PackageName is normalized to the bare package name. The
// installed Version stays empty because MCP configs do not pin to an
// installed version.
func TestScanConfig_SpecSelector(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
body := `{
"mcpServers": {
"playwright": {"command":"npx","args":["-y","@playwright/mcp@latest"]},
"pinned": {"command":"npx","args":["left-pad@1.2.3"]}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
sort.Slice(out, func(i, j int) bool { return out[i].ServerName < out[j].ServerName })
if len(out) != 2 {
t.Fatalf("want 2 records, got %+v", out)
}
// pinned: left-pad@1.2.3
if out[0].PackageName != "left-pad" || out[0].RequestedSpec != "left-pad@1.2.3" || out[0].Version != "" {
t.Errorf("pinned record: %+v", out[0])
}
// playwright: @playwright/mcp@latest
if out[1].PackageName != "@playwright/mcp" || out[1].RequestedSpec != "@playwright/mcp@latest" || out[1].Version != "" {
t.Errorf("playwright record: %+v", out[1])
}
}
// TestScanConfig_DockerConfidence verifies that docker MCP records with a
// pinned tag or digest get bumped to "medium" confidence (the only MCP
// shape that ties identity to an immutable reference), while untagged
// images stay at "low".
func TestScanConfig_DockerConfidence(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
body := `{
"mcpServers": {
"tagged": {"command":"docker","args":["run","ghcr.io/example/example-mcp:1.2.3"]},
"digest": {"command":"docker","args":["run","ghcr.io/example/example-mcp@sha256:abc"]},
"untagged": {"command":"docker","args":["run","ghcr.io/example/example-mcp"]},
"npm": {"command":"npx","args":["-y","@playwright/mcp@latest"]}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
by := map[string]model.Record{}
for _, r := range out {
by[r.ServerName] = r
}
if r := by["tagged"]; r.Confidence != "medium" {
t.Errorf("tagged docker confidence = %q, want medium: %+v", r.Confidence, r)
}
if r := by["digest"]; r.Confidence != "medium" {
t.Errorf("digest docker confidence = %q, want medium: %+v", r.Confidence, r)
}
if r := by["untagged"]; r.Confidence != "low" {
t.Errorf("untagged docker confidence = %q, want low: %+v", r.Confidence, r)
}
// Non-docker launchers stay at the conservative "low" — a selector
// like @latest is not an immutable identity.
if r := by["npm"]; r.Confidence != "low" {
t.Errorf("npm confidence = %q, want low: %+v", r.Confidence, r)
}
}
// TestScanConfig_RemoteURL verifies that remote MCP entries (sse / http
// transports identified by url/serverUrl/httpUrl) are not silently
// dropped: the sanitized endpoint is preserved in requested_spec and
// the record is tagged package_manager=mcp-remote. Headers, env, and
// any userinfo or query-string secrets must never appear.
func TestScanConfig_RemoteURL(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mcp.json")
body := `{
"mcpServers": {
"sse": {"type":"sse","url":"https://mcp.example.com/sse"},
"alt": {"serverUrl":"https://alt.example.com/api"},
"http": {"httpUrl":"https://http.example.com/v1"},
"auth": {"url":"https://user:secret@auth.example.com/mcp?token=shh#frag"}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
by := map[string]model.Record{}
for _, r := range out {
by[r.ServerName] = r
}
if len(out) != 4 {
t.Fatalf("want 4 remote records, got %d: %+v", len(out), out)
}
for _, name := range []string{"sse", "alt", "http", "auth"} {
r, ok := by[name]
if !ok {
t.Fatalf("missing remote record %q: %+v", name, out)
}
if r.PackageManager != "mcp-remote" {
t.Errorf("%s: package_manager = %q, want mcp-remote", name, r.PackageManager)
}
if r.PackageName != name {
t.Errorf("%s: package_name = %q, want %q (server id fallback)", name, r.PackageName, name)
}
if r.RequestedSpec == "" {
t.Errorf("%s: RequestedSpec empty, want sanitized URL", name)
}
}
// Userinfo, query string, fragment, and path must all be dropped from
// the recorded URL — credentials commonly slip in through any of them.
got := by["auth"].RequestedSpec
if strings.Contains(got, "secret") || strings.Contains(got, "token") || strings.Contains(got, "user:") || strings.Contains(got, "#") || strings.Contains(got, "?") || strings.Contains(got, "/mcp") {
t.Errorf("sanitized URL still contains secrets or path: %q", got)
}
if got != "https://auth.example.com" {
t.Errorf("sanitized URL = %q, want https://auth.example.com", got)
}
}
// TestScanConfig_FlatRemoteURL verifies that flat-shape MCP configs
// (no mcpServers/servers envelope) are admitted when the entry carries
// only a remote URL field — serverUrl or httpUrl — with no command,
// args, or type. Previously the flat admission check only recognized
// the "url" field, so entries using the alternate names were dropped
// silently.
func TestScanConfig_FlatRemoteURL(t *testing.T) {
dir := t.TempDir()
cases := []struct {
name, body, wantHost string
}{
{
name: "serverUrl",
body: `{"alt": {"serverUrl":"https://alt.example.com/api"}}`,
wantHost: "https://alt.example.com",
},
{
name: "httpUrl",
body: `{"http": {"httpUrl":"https://http.example.com/v1"}}`,
wantHost: "https://http.example.com",
},
}
for _, c := range cases {
path := filepath.Join(dir, c.name+".mcp.json")
if err := os.WriteFile(path, []byte(c.body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
if len(out) != 1 {
t.Fatalf("%s: want 1 record, got %d: %+v", c.name, len(out), out)
}
r := out[0]
if r.PackageManager != "mcp-remote" {
t.Errorf("%s: package_manager = %q, want mcp-remote", c.name, r.PackageManager)
}
if r.RequestedSpec != c.wantHost {
t.Errorf("%s: RequestedSpec = %q, want %q", c.name, r.RequestedSpec, c.wantHost)
}
}
}
// TestSanitizeRemoteURL covers the URL sanitizer in isolation. Only
// scheme + host are preserved; userinfo, query, fragment, and path
// are all dropped because credentials commonly hide in path segments
// ("/mcp/<token>") as well as the more obvious userinfo/query slots.
// Scheme-less network-path references collapse to "//host". Anything
// the parser cannot recover a host from returns "" rather than
// echoing a raw, potentially secret-bearing string.
func TestSanitizeRemoteURL(t *testing.T) {
cases := []struct {
name, in, want string
}{
{"empty", "", ""},
{"normal", "https://example.com/mcp", "https://example.com"},
{"query token", "https://example.com/mcp?token=abc", "https://example.com"},
{"fragment", "https://example.com/mcp#frag", "https://example.com"},
{"userinfo", "https://user:pass@example.com/mcp", "https://example.com"},
{"userinfo + query + fragment", "https://user:pass@example.com/mcp?token=abc#frag", "https://example.com"},
// Path-embedded token: the whole path is dropped, not just query
// and fragment, since secrets routinely show up as path segments.
{"path token", "https://example.com/mcp/sk-live-abcdef", "https://example.com"},
// Scheme-less network-path: //host[/path] keeps the //host form
// with userinfo stripped.
{"scheme-less userinfo", "//user:pass@example.com/mcp", "//example.com"},
{"scheme-less plain", "//example.com/api", "//example.com"},
// '@' in the path is irrelevant once the entire path is dropped.
{"path with @", "https://example.com/path/with@symbol", "https://example.com"},
// A bare token with no host shape must not be echoed back.
{"bare token", "sk-live-abcdef", ""},
}
for _, c := range cases {
if got := sanitizeRemoteURL(c.in); got != c.want {
t.Errorf("%s: sanitizeRemoteURL(%q) = %q, want %q", c.name, c.in, got, c.want)
}
}
}
// TestIsKnownMCPConfig verifies the basename allowlist covers both the
// historical files and the recently-added cline/mcp_settings forms.
func TestIsKnownMCPConfig(t *testing.T) {
want := []string{
"mcp.json",
".mcp.json",
"claude_desktop_config.json",
"mcp_config.json",
"mcp_settings.json",
"cline_mcp_settings.json",
}
for _, name := range want {
if !IsKnownMCPConfig(name) {
t.Errorf("IsKnownMCPConfig(%q) = false, want true", name)
}
}
for _, name := range []string{"package.json", "config.json", "mcp.yaml", ""} {
if IsKnownMCPConfig(name) {
t.Errorf("IsKnownMCPConfig(%q) = true, want false", name)
}
}
// settings.json is intentionally NOT in the basename allowlist —
// it's an ambiguous filename (VS Code user settings, etc.). Gemini
// CLI's ~/.gemini/settings.json is matched via the path-aware
// IsGeminiSettingsJSON helper instead.
if IsKnownMCPConfig("settings.json") {
t.Errorf("IsKnownMCPConfig(\"settings.json\") = true, want false (use IsGeminiSettingsJSON for path-aware dispatch)")
}
}
// TestIsGeminiSettingsJSON verifies that path-aware dispatch only matches
// `<...>/.gemini/settings.json` and does not pick up other settings.json
// files (notably VS Code's user settings) under unrelated directories.
func TestIsGeminiSettingsJSON(t *testing.T) {
match := []string{
"/home/alice/.gemini/settings.json",
"/Users/alice/.gemini/settings.json",
filepath.Join(".gemini", "settings.json"),
}
for _, p := range match {
if !IsGeminiSettingsJSON(p) {
t.Errorf("IsGeminiSettingsJSON(%q) = false, want true", p)
}
}
noMatch := []string{
"/home/alice/.vscode/settings.json",
"/home/alice/.config/Code/User/settings.json",
"/home/alice/.gemini/other.json",
"/home/alice/gemini/settings.json", // missing the dot
"/home/alice/.gemini/sub/settings.json",
"",
}
for _, p := range noMatch {
if IsGeminiSettingsJSON(p) {
t.Errorf("IsGeminiSettingsJSON(%q) = true, want false", p)
}
}
}
// TestIsClaudeConfigJSON verifies path-aware dispatch for Claude Code's
// `~/.claude.json` user config, which the basename allowlist does not
// route to the generic MCP parser.
func TestIsClaudeConfigJSON(t *testing.T) {
match := []string{
"/home/alice/.claude.json",
"/Users/alice/.claude.json",
".claude.json",
}
for _, p := range match {
if !IsClaudeConfigJSON(p) {
t.Errorf("IsClaudeConfigJSON(%q) = false, want true", p)
}
}
noMatch := []string{
"/home/alice/.claude/mcp.json",
"/home/alice/claude.json", // missing the dot
"/home/alice/.claude.jsonc",
"",
}
for _, p := range noMatch {
if IsClaudeConfigJSON(p) {
t.Errorf("IsClaudeConfigJSON(%q) = true, want false", p)
}
}
}
// TestScanClaudeConfig verifies that both MCP scopes in ~/.claude.json are
// inventoried — the top-level `mcpServers` (user scope) and the
// per-project `projects.<dir>.mcpServers` (local scope) — while the file's
// many unrelated settings are ignored and never misread as servers.
func TestScanClaudeConfig(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, ".claude.json")
body := `{
"numStartups": 42,
"oauthAccount": {"emailAddress": "shouldnotbecaptured"},
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {"GITHUB_TOKEN": "shouldnotbecaptured"}
}
},
"projects": {
"/home/alice/proj-a": {
"allowedTools": [],
"mcpServers": {
"local-time": {"command": "uvx", "args": ["mcp-server-time"]}
}
},
"/home/alice/proj-b": {
"mcpServers": {
"stripe": {"type": "http", "url": "https://user:secret@mcp.stripe.com/mcp?token=abc"}
}
},
"/home/alice/proj-c": {
"lastCost": 0.12
}
}
}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanClaudeConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
if len(out) != 3 {
t.Fatalf("want 3 records (1 user-scope + 2 local-scope), got %d: %+v", len(out), out)
}
byServer := map[string]model.Record{}
for _, r := range out {
byServer[r.ServerName] = r
if r.RootKind != model.RootKindMCPConfig {
t.Errorf("%s: RootKind = %q, want %q", r.ServerName, r.RootKind, model.RootKindMCPConfig)
}
if r.SourceFile != path {
t.Errorf("%s: SourceFile = %q, want %q", r.ServerName, r.SourceFile, path)
}
}
// User-scope server: top-level mcpServers, ProjectPath is the file's dir.
if got := byServer["github"]; got.PackageName != "@modelcontextprotocol/server-github" || got.ProjectPath != dir {
t.Errorf("github user-scope record wrong: %+v", got)
}
// Local-scope servers: ProjectPath is the per-project key.
if got := byServer["local-time"]; got.PackageName != "mcp-server-time" || got.ProjectPath != "/home/alice/proj-a" {
t.Errorf("local-time local-scope record wrong: %+v", got)
}
// Remote entry: sanitized to scheme://host, no userinfo/path/query.
if got := byServer["stripe"]; got.ProjectPath != "/home/alice/proj-b" || got.PackageManager != "mcp-remote" || got.RequestedSpec != "https://mcp.stripe.com" {
t.Errorf("stripe remote record wrong: %+v", got)
}
}
// TestScanClaudeConfig_NoServers verifies that a ~/.claude.json with no
// MCP servers configured (only unrelated settings) emits nothing — the
// generic flat-object fallback must not run over this file and treat
// arbitrary top-level keys as server entries.
func TestScanClaudeConfig_NoServers(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, ".claude.json")
body := `{"numStartups": 3, "oauthAccount": {"emailAddress": "x"}, "projects": {"/p": {"lastCost": 1}}}`
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
var out []model.Record
s := &Scanner{MaxFileSize: 1 << 20, Emit: func(r model.Record) { out = append(out, r) }}
if err := s.ScanClaudeConfig(path, model.Record{}); err != nil {
t.Fatal(err)
}
if len(out) != 0 {
t.Fatalf("want 0 records, got %d: %+v", len(out), out)
}
}
// TestLooksLikePackageSpec verifies the package-spec gate that keeps
// non-package launcher arguments — URLs, VCS refs, file/path refs, and
// tarball archives — out of PackageName and RequestedSpec.
func TestLooksLikePackageSpec(t *testing.T) {
pass := []string{
"@modelcontextprotocol/server-github",
"@playwright/mcp@latest",
"left-pad",
"left-pad@1.2.3",
"@scope/pkg",
"@scope/pkg@1.2.3",
"pkg@npm:other@1.0",
"host@npm:@scope/other@1.0.0",
"@scope/pkg@npm:@other/target@2.0.0",
"python:mypkg.server",
}
reject := []string{
"",
"http://reg.example.com/pkg.tgz",
"https://reg.example.com/pkg.tgz",
"https://user:token@reg.example.com/",
"https://reg.example.com/",
"ftp://example.com/x",
"git://github.com/owner/repo.git",
"git+https://github.com/owner/repo.git",
"git+ssh://git@github.com/owner/repo.git",
"ssh://git@github.com/owner/repo.git",
"github:owner/repo",
"gitlab:owner/repo",
"bitbucket:owner/repo",
"file:./local/path",
"file:///abs/path",
"/abs/path/to/pkg.tgz",
"./local/pkg.tgz",
"../local/pkg",
"./relative/dir",
"pkg.tgz",
"archive.tar.gz",
"archive.zip",
"C:\\Users\\me\\pkg",
"C:/Users/me/pkg",
`server\path\here`,
"user:pass@host/path",
// npm alias targets must be re-validated against the same
// rules — URL, file:, and path forms after "@npm:" must not
// slip through the alias carve-out.
"host@npm:https://user:token@reg.example.com/pkg.tgz",
"host@npm:http://reg.example.com/pkg.tgz",
"host@npm:file:./local",
"host@npm:file:///abs/path",
"host@npm:../local",
"host@npm:./local",
"host@npm:/abs/path",
"host@npm:pkg.tgz",
"host@npm:user:pass@host/path",
"host@npm:", // empty alias target
}
for _, s := range pass {
if !looksLikePackageSpec(s) {
t.Errorf("looksLikePackageSpec(%q) = false, want true", s)
}
}
for _, s := range reject {
if looksLikePackageSpec(s) {
t.Errorf("looksLikePackageSpec(%q) = true, want false", s)
}
}
}
// TestInferPackageFromArgs_ValueTakingFlags verifies that npm-family
// launchers consume the value of value-taking flags so a flag's value
// (often a registry URL with embedded credentials) is not returned as
// the package spec.
func TestInferPackageFromArgs_ValueTakingFlags(t *testing.T) {
cases := []struct {
name string
cmd string
args []string
wantSpec string
wantLauncher string
}{
{
name: "npx --registry URL pkg",
cmd: "npx",
args: []string{"--registry", "https://token@reg.example.com/", "@scope/pkg"},
// Registry URL must not be returned; the package after it is.
wantSpec: "@scope/pkg",
wantLauncher: "",
},
{
name: "npx --registry=URL pkg",
cmd: "npx",
args: []string{"--registry=https://token@reg.example.com/", "@scope/pkg"},
wantSpec: "@scope/pkg",
wantLauncher: "",
},
{
name: "pnpm dlx --registry URL pkg",
cmd: "pnpm",
args: []string{"dlx", "--registry", "https://t@reg.example.com/", "@scope/pkg"},
wantSpec: "@scope/pkg",
wantLauncher: "",
},
{
name: "yarn dlx --cache PATH pkg",
cmd: "yarn",
args: []string{"dlx", "--cache", "/some/local/cache", "left-pad@1.2.3"},
wantSpec: "left-pad@1.2.3",
wantLauncher: "",
},
{
name: "bun x --cwd PATH pkg",
cmd: "bun",
args: []string{"x", "--cwd", "/tmp/work", "left-pad"},
wantSpec: "left-pad",
wantLauncher: "",
},
{
name: "bunx --registry URL pkg",
cmd: "bunx",
args: []string{"--registry", "https://t@reg.example.com/", "left-pad"},
wantSpec: "left-pad",
wantLauncher: "",
},
{
name: "npm exec --registry URL -- pkg",
cmd: "npm",
args: []string{"exec", "--registry", "https://t@reg.example.com/", "--", "@scope/pkg"},
wantSpec: "@scope/pkg",
wantLauncher: "",
},
{
// npx --package <pkg> -- <cmd>: the explicit --package value is
// the package identity, not the trailing entry-point command.
name: "npx --package <scoped> -- cmd",
cmd: "npx",
args: []string{"--package", "@scope/pkg", "--", "cmd"},
wantSpec: "@scope/pkg",
wantLauncher: "",