Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions pkgs/mcpx/internal/cli/adapter_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,21 @@ func buildCompletionFileImport(o importOptions) (adapter.Spec, []string, error)
return adapter.Spec{}, nil, fmt.Errorf("%s does not look like a bash, zsh or fish completion script; say which with --shell", o.completion)
}
base := filepath.Base(o.completion)
name := o.name
if name == "" {
name = adapterName(completionName(base, shell))
}
// The program name is kept exactly as written for parsing and
// for the spec's command: parsers match completion lines against
// it (a fish file's -c value is the real program name), so
// normalising it first would filter out every line of a dotted
// program's file (toolbox.v2.fish) and store a command that does
// not exist. Only the adapter namespace name is sanitised (#9796).
program := o.name
if program == "" {
program = completionName(base, shell)
}
name := adapterName(program)
if name == "" || name == "-" {
return adapter.Spec{}, nil, fmt.Errorf("cannot tell which program %s completes; name it with --name", o.completion)
}
shape := parseCompletion(string(b), shell, name)
shape := parseCompletion(string(b), shell, program)
if shape.empty() {
return adapter.Spec{}, nil, fmt.Errorf("%s yielded no subcommands or flags as a %s completion; nothing to declare", o.completion, shell)
}
Expand All @@ -550,7 +557,7 @@ func buildCompletionFileImport(o importOptions) (adapter.Spec, []string, error)
GeneratedAt: time.Now().UTC().Format(time.RFC3339),
Generator: "mcpx adapter import",
}
spec, notes, err := shapeToSpec(name, name, shape, prov)
spec, notes, err := shapeToSpec(name, program, shape, prov)
if err != nil {
return adapter.Spec{}, nil, err
}
Expand Down
26 changes: 26 additions & 0 deletions pkgs/mcpx/internal/cli/adapter_import_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,32 @@ func TestBuildCompletionFileImport(t *testing.T) {
}
}

func TestBuildCompletionFileImportDottedProgramName(t *testing.T) {
// Issue #9796: a dotted program name must survive for parsing
// and for the spec's command; only the adapter namespace name
// is sanitised (dots to dashes). Before the fix the import of
// this fixture failed as empty, because every `complete` line
// is for -c toolbox.v2 while the parser was handed toolbox-v2.
dotted := strings.ReplaceAll(fishFixture, "-c toolbox", "-c toolbox.v2")
path := filepath.Join(t.TempDir(), "toolbox.v2.fish")
if err := os.WriteFile(path, []byte(dotted), 0o644); err != nil {
t.Fatal(err)
}
spec, _, err := buildCompletionFileImport(importOptions{completion: path})
if err != nil {
t.Fatal(err)
}
if spec.Name != "toolbox-v2" {
t.Errorf("adapter namespace name should be sanitised: %q", spec.Name)
}
if spec.Command != "toolbox.v2" {
t.Errorf("spec command should keep the original program name: %q", spec.Command)
}
if len(spec.Tools) != 2 {
t.Errorf("declaration should be non-empty with both subcommand tools: %+v", spec.Tools)
}
}

func TestParseImportArgsGenerationFlags(t *testing.T) {
o, err := parseImportArgs([]string{"--source", "help", "git", "--out=x.json", "--name", "g"})
if err != nil {
Expand Down