@@ -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
0 commit comments