55 "encoding/json"
66 "flag"
77 "fmt"
8+ "maps"
89 "strings"
910
1011 "github.com/sourcegraph/src-cli/internal/mcp"
@@ -29,6 +30,12 @@ func mcpUsage() {
2930 fmt .Println (" src mcp <tool-name> schema View the input/output schema of a tool" )
3031 fmt .Println (" src mcp <tool-name> <flags> Invoke a tool with the given flags" )
3132 fmt .Println (" src mcp <tool-name> -h List the available flags of a tool" )
33+ fmt .Println ("\n COMMON FLAGS:" )
34+ fmt .Println (" --json '{...}' Provide all tool arguments as a JSON object (recommended for agents)" )
35+ fmt .Println ("\n NOTE:" )
36+ fmt .Println (" When both --json and explicit flags are provided, values from --json take precedence." )
37+ fmt .Println ("\n EXAMPLE:" )
38+ fmt .Println (" src mcp <tool-name> --json '{\" arg1\" : \" value1\" , \" arg2\" : \" value2\" }'" )
3239}
3340
3441func mcpMain (args []string ) error {
@@ -76,6 +83,20 @@ func mcpMain(args []string) error {
7683 }
7784 mcp .DerefFlagValues (flags , vars )
7885
86+ // arguments provided via a JSON object take precedence over explicit values provided from flags
87+ if val , ok := vars ["json" ]; ok {
88+ if jsonVal , ok := val .(string ); ok && len (jsonVal ) > 0 {
89+ m := make (map [string ]any )
90+ if err := json .Unmarshal ([]byte (jsonVal ), & m ); err != nil {
91+ return err
92+ }
93+ // copy overrides existing keys
94+ maps .Copy (vars , m )
95+ }
96+ // we delete "json" from vars, otherwise it will be sent as a argument to the tool call
97+ delete (vars , "json" )
98+ }
99+
79100 if err := validateToolArgs (tool .InputSchema , args , vars ); err != nil {
80101 return err
81102 }
@@ -94,16 +115,20 @@ func mcpMain(args []string) error {
94115}
95116
96117func printSchemas (tool * mcp.ToolDef ) error {
97- input , err := json .Marshal (tool .InputSchema )
98- if err != nil {
99- return err
118+ var schema = struct {
119+ Name string `json:"tool"`
120+ Input any `json:"inputSchema"`
121+ Output any `json:"outputSchema"`
122+ }{
123+ Name : tool .Name ,
124+ Input : tool .InputSchema ,
125+ Output : tool .OutputSchema ,
100126 }
101- output , err := json .Marshal (tool . OutputSchema )
127+ jsonVal , err := json .Marshal (schema )
102128 if err != nil {
103129 return err
104130 }
105-
106- fmt .Printf ("Input:\n %v\n Output:\n %v\n " , string (input ), string (output ))
131+ fmt .Println (string (jsonVal ))
107132 return nil
108133}
109134
0 commit comments