Skip to content

Commit 53c8b10

Browse files
Merge branch 'main' into bo/include-verbose-logging
2 parents 38bc959 + 2835e8c commit 53c8b10

File tree

3 files changed

+23
-42
lines changed

3 files changed

+23
-42
lines changed

cmd/src/mcp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func mcpMain(args []string) error {
8585
return err
8686
}
8787

88-
output, err := json.MarshalIndent(result, "", " ")
88+
output, err := json.Marshal(result)
8989
if err != nil {
9090
return err
9191
}
@@ -94,11 +94,11 @@ func mcpMain(args []string) error {
9494
}
9595

9696
func printSchemas(tool *mcp.ToolDef) error {
97-
input, err := json.MarshalIndent(tool.InputSchema, "", " ")
97+
input, err := json.Marshal(tool.InputSchema)
9898
if err != nil {
9999
return err
100100
}
101-
output, err := json.MarshalIndent(tool.OutputSchema, "", " ")
101+
output, err := json.Marshal(tool.OutputSchema)
102102
if err != nil {
103103
return err
104104
}

internal/mcp/mcp_request.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ import (
1414
)
1515

1616
const MCPURLPath = ".api/mcp/v1"
17-
const MCPDeepSearchURLPath = ".api/mcp/deepsearch"
1817

1918
func fetchToolDefinitions(ctx context.Context, client api.Client, endpoint string) (map[string]*ToolDef, error) {
2019
resp, err := doJSONRPC(ctx, client, endpoint, "tools/list", nil)
2120
if err != nil {
22-
return nil, errors.Wrap(err, "failed to list tools from mcp endpoint")
21+
return nil, errors.Wrapf(err, "JSON-RPC tools/list request failed to %q", MCPURLPath)
2322
}
2423
defer resp.Body.Close()
2524

2625
data, err := readSSEResponseData(resp)
2726
if err != nil {
28-
return nil, errors.Wrap(err, "failed to read list tools SSE response")
27+
return nil, errors.Wrap(err, "failed to read tools/list SSE response")
2928
}
3029

3130
var rpcResp struct {
@@ -54,7 +53,11 @@ func doToolCall(ctx context.Context, client api.Client, endpoint string, tool st
5453
Arguments: vars,
5554
}
5655

57-
return doJSONRPC(ctx, client, endpoint, "tools/call", params)
56+
resp, err := doJSONRPC(ctx, client, endpoint, "tools/call", params)
57+
if err != nil {
58+
return nil, errors.Wrapf(err, "JSON-RPC tools/call request failed to %q", MCPURLPath)
59+
}
60+
return resp, err
5861
}
5962

6063
func doJSONRPC(ctx context.Context, client api.Client, endpoint string, method string, params any) (*http.Response, error) {
@@ -123,18 +126,19 @@ func decodeToolResponse(resp *http.Response) (map[string]json.RawMessage, error)
123126
return nil, errors.Wrapf(err, "failed to unmarshal MCP JSON-RPC response")
124127
}
125128
if jsonRPCResp.Error != nil {
126-
return nil, errors.Newf("MCP tools/call failed: %d %s", jsonRPCResp.Error.Code, jsonRPCResp.Error.Message)
129+
return nil, errors.Newf("MCP JSON-RPC error: %d %s", jsonRPCResp.Error.Code, jsonRPCResp.Error.Message)
127130
}
128131

129132
if jsonRPCResp.Result.IsError {
130-
if len(jsonRPCResp.Result.Content) > 0 {
133+
content := jsonRPCResp.Result.Content[0]
134+
if len(content) > 0 {
131135
var textContent struct {
132136
Text string `json:"text"`
133137
}
134-
if err := json.Unmarshal(jsonRPCResp.Result.Content[0], &textContent); err == nil && textContent.Text != "" {
138+
if err := json.Unmarshal(content, &textContent); err == nil && textContent.Text != "" {
135139
return nil, errors.Newf("MCP tool error: %s", textContent.Text)
136140
}
137-
return nil, errors.Newf("MCP tool error: %s", string(jsonRPCResp.Result.Content[0]))
141+
return nil, errors.Newf("MCP tool error: %s", string(content))
138142
}
139143
return nil, errors.New("MCP tool returned an error")
140144
}

internal/mcp/registry.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,29 @@ import (
88
"slices"
99

1010
"github.com/sourcegraph/src-cli/internal/api"
11-
12-
"github.com/sourcegraph/sourcegraph/lib/errors"
1311
)
1412

1513
// ToolRegistry keeps track of tools and the endpoints they originated from
1614
type ToolRegistry struct {
17-
tools map[string]*ToolDef
18-
endpoints map[string]string
15+
tools map[string]*ToolDef
1916
}
2017

2118
func NewToolRegistry() *ToolRegistry {
2219
return &ToolRegistry{
23-
tools: make(map[string]*ToolDef),
24-
endpoints: make(map[string]string),
20+
tools: make(map[string]*ToolDef),
2521
}
2622
}
2723

28-
// LoadTools loads the tool definitions from the Mcp tool endpoints constants McpURLPath and McpDeepSearchURLPath
24+
// LoadTools loads the tool definitions from the Mcp tool endpoints constants McpURLPath
2925
func (r *ToolRegistry) LoadTools(ctx context.Context, client api.Client) error {
30-
endpoints := []string{MCPURLPath, MCPDeepSearchURLPath}
31-
32-
var errs []error
33-
for _, endpoint := range endpoints {
34-
tools, err := fetchToolDefinitions(ctx, client, endpoint)
35-
if err != nil {
36-
errs = append(errs, errors.Wrapf(err, "failed to load tools from %s", endpoint))
37-
continue
38-
}
39-
r.register(endpoint, tools)
40-
}
41-
42-
if len(errs) > 0 {
43-
return errors.Append(nil, errs...)
26+
tools, err := fetchToolDefinitions(ctx, client, MCPURLPath)
27+
if err != nil {
28+
return err
4429
}
30+
r.tools = tools
4531
return nil
4632
}
4733

48-
// register associates a collection of tools with the given endpoint
49-
func (r *ToolRegistry) register(endpoint string, tools map[string]*ToolDef) {
50-
for name, def := range tools {
51-
r.tools[name] = def
52-
r.endpoints[name] = endpoint
53-
}
54-
}
55-
5634
// Get returns the tool definition for the given name
5735
func (r *ToolRegistry) Get(name string) (*ToolDef, bool) {
5836
tool, ok := r.tools[name]
@@ -62,8 +40,7 @@ func (r *ToolRegistry) Get(name string) (*ToolDef, bool) {
6240
// CallTool calls the given tool with the given arguments. It constructs the Tool request and decodes the Tool response
6341
func (r *ToolRegistry) CallTool(ctx context.Context, client api.Client, name string, args map[string]any) (map[string]json.RawMessage, error) {
6442
tool := r.tools[name]
65-
endpoint := r.endpoints[name]
66-
resp, err := doToolCall(ctx, client, endpoint, tool.RawName, args)
43+
resp, err := doToolCall(ctx, client, MCPURLPath, tool.RawName, args)
6744
if err != nil {
6845
return nil, err
6946
}

0 commit comments

Comments
 (0)