@@ -16,9 +16,9 @@ import (
1616 "github.com/urfave/cli/v3"
1717)
1818
19- const toolDelimiter = "_"
19+ const ToolDelimiter = "_"
2020
21- func MCPCommand (root * cli.Command ) * cli.Command {
21+ func MCPCommand (root * cli.Command , prefix ... string ) * cli.Command {
2222 // Calling root.Run will modify root.Action, so store if it is non-nil first
2323 // We'll use this to decide to hide the root app if it's just a help command.
2424 hasRootAction := root .Action != nil
@@ -30,7 +30,7 @@ func MCPCommand(root *cli.Command) *cli.Command {
3030 slog .Debug ("building MCP server" , slog .Any ("app" , root .Name ))
3131
3232 slog .Debug ("serving MCP server" )
33- srv , err := MPCServer (root , hasRootAction )
33+ srv , err := MPCServer (root , hasRootAction , prefix ... )
3434 if err != nil {
3535 return err
3636 }
@@ -40,13 +40,18 @@ func MCPCommand(root *cli.Command) *cli.Command {
4040 }
4141}
4242
43- func MPCServer (root * cli.Command , hasRootAction bool ) (* server.MCPServer , error ) {
43+ func MPCServer (root * cli.Command , hasRootAction bool , prefix ... string ) (* server.MCPServer , error ) {
4444 srv := server .NewMCPServer (root .Name , root .Version , server .WithToolCapabilities (true ))
4545
4646 toolHandler := func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
47- args := strings .Split (request .Params .Name , toolDelimiter )
47+ args := strings .Split (request .Params .Name , ToolDelimiter )
48+
49+ // Assume we were called with a root command - it should not be forwarded when we fork.
4850 args = args [1 :]
4951
52+ // If we were not called from the root command, add any prefix that was specified:
53+ args = append (prefix , args ... )
54+
5055 // We're about to execute some user input, how bad of an idea is that?
5156 // Because we hardcode `os.Args[0]` and don't use a shell, we're safe from command injection.
5257 // I _assume_ mcp-go has verified the arguments are actually what our tool said it can use.
@@ -68,8 +73,8 @@ func MPCServer(root *cli.Command, hasRootAction bool) (*server.MCPServer, error)
6873 }
6974 }
7075 var logFields []any
71- for _ , arg := range args {
72- logFields = append (logFields , slog .Any ("arg" , arg ))
76+ for i , arg := range args {
77+ logFields = append (logFields , slog .Any (fmt . Sprintf ( "%d" , i ) , arg ))
7378 }
7479 slog .Info ("forking" , slog .Any ("cmd" , os .Args [0 ]), slog .Group ("args" , logFields ... ))
7580
0 commit comments