Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cmd/src/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"flag"
"fmt"
"maps"
"strings"

"github.com/sourcegraph/src-cli/internal/mcp"
Expand All @@ -29,6 +30,12 @@ func mcpUsage() {
fmt.Println(" src mcp <tool-name> schema View the input/output schema of a tool")
fmt.Println(" src mcp <tool-name> <flags> Invoke a tool with the given flags")
fmt.Println(" src mcp <tool-name> -h List the available flags of a tool")
fmt.Println("\nCOMMON FLAGS:")
fmt.Println(" --json '{...}' Provide all tool arguments as a JSON object (recommended for agents)")
fmt.Println("\nNOTE:")
fmt.Println(" When both --json and explicit flags are provided, values from --json take precedence.")
fmt.Println("\nEXAMPLE:")
fmt.Println(" src mcp <tool-name> --json '{\"arg1\": \"value1\", \"arg2\": \"value2\"}'")
}

func mcpMain(args []string) error {
Expand Down Expand Up @@ -76,6 +83,20 @@ func mcpMain(args []string) error {
}
mcp.DerefFlagValues(flags, vars)

// arguments provided via a JSON object take precedence over explicit values provided from flags
if val, ok := vars["json"]; ok {
if jsonVal, ok := val.(string); ok && len(jsonVal) > 0 {
m := make(map[string]any)
if err := json.Unmarshal([]byte(jsonVal), &m); err != nil {
return err
}
// copy overrides existing keys
maps.Copy(vars, m)
}
// we delete "json" from vars, otherwise it will be sent as a argument to the tool call
delete(vars, "json")
}

if err := validateToolArgs(tool.InputSchema, args, vars); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/mcp/mcp_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ func BuildArgFlagSet(tool *ToolDef) (*flag.FlagSet, map[string]any, error) {
}
}

flagVars["json"] = fs.String("json", "", "provide all arguments as a JSON object")

return fs, flagVars, nil
}
Loading