Skip to content

Commit ef93434

Browse files
authored
Merge branch 'main' into michal/lsp
2 parents 8c293cb + c921cc5 commit ef93434

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

cmd/src/mcp.go

Lines changed: 31 additions & 6 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
}
@@ -94,16 +115,20 @@ func mcpMain(args []string) error {
94115
}
95116

96117
func 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\nOutput:\n%v\n", string(input), string(output))
131+
fmt.Println(string(jsonVal))
107132
return nil
108133
}
109134

internal/api/api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ func (c *client) createHTTPRequest(ctx context.Context, method, p string, body i
174174
if *c.opts.Flags.trace {
175175
req.Header.Set("X-Sourcegraph-Should-Trace", "true")
176176
}
177+
178+
// Set before additional headers, in case of an override.
179+
req.Header.Set("Content-Type", "application/json")
180+
177181
for k, v := range c.opts.AdditionalHeaders {
178182
req.Header.Set(k, v)
179183
}
@@ -322,6 +326,10 @@ func (r *request) curlCmd() (string, error) {
322326
if r.client.opts.AccessToken != "" {
323327
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-H", "Authorization: token "+r.client.opts.AccessToken))
324328
}
329+
// Preserve overrides if Content-Type is set
330+
if r.client.opts.AdditionalHeaders["Content-Type"] == "" {
331+
r.client.opts.AdditionalHeaders["Content-Type"] = "application/json"
332+
}
325333
for k, v := range r.client.opts.AdditionalHeaders {
326334
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-H", k+": "+v))
327335
}

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)