Skip to content

Commit 2b21f03

Browse files
committed
add mcp tests
1 parent 32da77d commit 2b21f03

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

mage/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/magefile/mage/internal"
27+
magemcp "github.com/magefile/mage/mcp"
2728
"github.com/magefile/mage/mg"
2829
"github.com/magefile/mage/parse"
2930
"github.com/magefile/mage/sh"
@@ -106,6 +107,7 @@ type Invocation struct {
106107
GOOS string // sets the GOOS when producing a binary with -compileout
107108
GOARCH string // sets the GOARCH when producing a binary with -compileout
108109
Ldflags string // sets the ldflags when producing a binary with -compileout
110+
MCP bool // generate an MCP server instead of a CLI
109111
Stdout io.Writer // writer to write stdout messages to
110112
Stderr io.Writer // writer to write stderr messages to
111113
Stdin io.Reader // reader to read stdin from
@@ -219,6 +221,7 @@ func Parse(stderr, stdout io.Writer, args []string) (inv Invocation, cmd Command
219221
fs.StringVar(&inv.GOOS, "goos", "", "set GOOS for binary produced with -compile")
220222
fs.StringVar(&inv.GOARCH, "goarch", "", "set GOARCH for binary produced with -compile")
221223
fs.StringVar(&inv.Ldflags, "ldflags", "", "set ldflags for binary produced with -compile")
224+
fs.BoolVar(&inv.MCP, "mcp", false, "generate an MCP server instead of a CLI")
222225

223226
// commands below
224227

@@ -257,6 +260,7 @@ Options:
257260
use the given go binary to compile the output (default: "go")
258261
-goos sets the GOOS for the binary created by -compile (default: current OS)
259262
-ldflags sets the ldflags for the binary created by -compile (default: "")
263+
-mcp generate an MCP server instead of a CLI
260264
-multiline retain line returns in help docs (default: convert to spaces)
261265
-h show description of a target
262266
-keep keep intermediate mage files around after running
@@ -320,6 +324,10 @@ Options:
320324
return inv, cmd, errors.New("-goos and -goarch only apply when running with -compile")
321325
}
322326

327+
if inv.MCP && cmd != None && cmd != CompileStatic {
328+
return inv, cmd, errors.New("-mcp cannot be combined with -init, -clean, or -version")
329+
}
330+
323331
inv.Args = fs.Args()
324332
if inv.Help && len(inv.Args) > 1 {
325333
return inv, cmd, errors.New("-h can only show help for a single target")
@@ -450,7 +458,11 @@ func Invoke(inv Invocation) int {
450458
binaryName = filepath.Base(inv.CompileOut)
451459
}
452460

453-
err = GenerateMainfile(binaryName, main, info)
461+
if inv.MCP {
462+
err = magemcp.GenerateMainfile(binaryName, main, info)
463+
} else {
464+
err = GenerateMainfile(binaryName, main, info)
465+
}
454466
if err != nil {
455467
errlog.Println("Error:", err)
456468
return 1

mage/main_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,3 +1917,61 @@ func fileData(file string) (exeType, archSize, error) {
19171917
}
19181918
return -1, -1, errors.New("unrecognized executable format")
19191919
}
1920+
1921+
func TestParseMCPFlag(t *testing.T) {
1922+
buf := &bytes.Buffer{}
1923+
inv, _, err := Parse(io.Discard, buf, []string{"-mcp"})
1924+
if err != nil {
1925+
t.Fatal("unexpected error", err)
1926+
}
1927+
if !inv.MCP {
1928+
t.Error("expected MCP to be true")
1929+
}
1930+
}
1931+
1932+
func TestParseMCPWithCompile(t *testing.T) {
1933+
buf := &bytes.Buffer{}
1934+
inv, cmd, err := Parse(io.Discard, buf, []string{"-mcp", "-compile", "output"})
1935+
if err != nil {
1936+
t.Fatal("unexpected error", err)
1937+
}
1938+
if !inv.MCP {
1939+
t.Error("expected MCP to be true")
1940+
}
1941+
if cmd != CompileStatic {
1942+
t.Errorf("expected CompileStatic command, got %v", cmd)
1943+
}
1944+
}
1945+
1946+
func TestParseMCPWithInit(t *testing.T) {
1947+
buf := &bytes.Buffer{}
1948+
_, _, err := Parse(io.Discard, buf, []string{"-mcp", "-init"})
1949+
if err == nil {
1950+
t.Fatal("expected error combining -mcp with -init")
1951+
}
1952+
if !strings.Contains(err.Error(), "-mcp cannot be combined") {
1953+
t.Fatalf("unexpected error message: %v", err)
1954+
}
1955+
}
1956+
1957+
func TestParseMCPWithClean(t *testing.T) {
1958+
buf := &bytes.Buffer{}
1959+
_, _, err := Parse(io.Discard, buf, []string{"-mcp", "-clean"})
1960+
if err == nil {
1961+
t.Fatal("expected error combining -mcp with -clean")
1962+
}
1963+
if !strings.Contains(err.Error(), "-mcp cannot be combined") {
1964+
t.Fatalf("unexpected error message: %v", err)
1965+
}
1966+
}
1967+
1968+
func TestParseMCPWithVersion(t *testing.T) {
1969+
buf := &bytes.Buffer{}
1970+
_, _, err := Parse(io.Discard, buf, []string{"-mcp", "-version"})
1971+
if err == nil {
1972+
t.Fatal("expected error combining -mcp with -version")
1973+
}
1974+
if !strings.Contains(err.Error(), "-mcp cannot be combined") {
1975+
t.Fatalf("unexpected error message: %v", err)
1976+
}
1977+
}

0 commit comments

Comments
 (0)