Skip to content

Commit 7393a82

Browse files
authored
mcp: add --json flag to all tool calls to provide args as a json object (#1245)
* add -json flag for all tools * initial version of accepting args as json * update usage for --json flag - include note section where we mention the precedence * add comments
1 parent 5d1e211 commit 7393a82

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

cmd/src/mcp.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
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("\nCOMMON FLAGS:")
34+
fmt.Println(" --json '{...}' Provide all tool arguments as a JSON object (recommended for agents)")
35+
fmt.Println("\nNOTE:")
36+
fmt.Println(" When both --json and explicit flags are provided, values from --json take precedence.")
37+
fmt.Println("\nEXAMPLE:")
38+
fmt.Println(" src mcp <tool-name> --json '{\"arg1\": \"value1\", \"arg2\": \"value2\"}'")
3239
}
3340

3441
func 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
}

internal/mcp/mcp_args.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,7 @@ func BuildArgFlagSet(tool *ToolDef) (*flag.FlagSet, map[string]any, error) {
9393
}
9494
}
9595

96+
flagVars["json"] = fs.String("json", "", "provide all arguments as a JSON object")
97+
9698
return fs, flagVars, nil
9799
}

0 commit comments

Comments
 (0)