Skip to content

Commit 5dc0841

Browse files
committed
use tool registry in mcp command for tool execution
1 parent a744b31 commit 5dc0841

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

cmd/src/mcp.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"strings"
99

10-
"github.com/sourcegraph/src-cli/internal/api"
1110
"github.com/sourcegraph/src-cli/internal/mcp"
1211

1312
"github.com/sourcegraph/sourcegraph/lib/errors"
@@ -37,8 +36,8 @@ func mcpMain(args []string) error {
3736
apiClient := cfg.apiClient(nil, mcpFlagSet.Output())
3837

3938
ctx := context.Background()
40-
tools, err := mcp.FetchToolDefinitions(ctx, apiClient)
41-
if err != nil {
39+
registry := mcp.NewToolRegistry()
40+
if err := registry.LoadTools(ctx, apiClient); err != nil {
4241
return err
4342
}
4443

@@ -50,7 +49,7 @@ func mcpMain(args []string) error {
5049
subcmd := args[0]
5150
if subcmd == "list-tools" {
5251
fmt.Println("The following tools are available:")
53-
for name := range tools {
52+
for name := range registry.All() {
5453
fmt.Printf(" %s\n", name)
5554
}
5655
fmt.Println("\nUSAGE:")
@@ -59,7 +58,7 @@ func mcpMain(args []string) error {
5958
fmt.Println(" src mcp <tool-name> -h List the available flags of a tool")
6059
return nil
6160
}
62-
tool, ok := tools[subcmd]
61+
tool, ok := registry.Get(subcmd)
6362
if !ok {
6463
return errors.Newf("tool definition for %q not found - run src mcp list-tools to see a list of available tools", subcmd)
6564
}
@@ -82,7 +81,17 @@ func mcpMain(args []string) error {
8281
return err
8382
}
8483

85-
return handleMcpTool(context.Background(), apiClient, tool, vars)
84+
result, err := registry.CallTool(ctx, apiClient, tool.Name, vars)
85+
if err != nil {
86+
return err
87+
}
88+
89+
output, err := json.MarshalIndent(result, "", " ")
90+
if err != nil {
91+
return err
92+
}
93+
fmt.Println(string(output))
94+
return nil
8695
}
8796

8897
func printSchemas(tool *mcp.ToolDef) error {
@@ -112,23 +121,3 @@ func validateToolArgs(inputSchema mcp.SchemaObject, args []string, vars map[stri
112121

113122
return nil
114123
}
115-
116-
func handleMcpTool(ctx context.Context, client api.Client, tool *mcp.ToolDef, vars map[string]any) error {
117-
resp, err := mcp.DoToolCall(ctx, client, tool.RawName, vars)
118-
if err != nil {
119-
return err
120-
}
121-
122-
result, err := mcp.DecodeToolResponse(resp)
123-
if err != nil {
124-
return err
125-
}
126-
defer resp.Body.Close()
127-
128-
output, err := json.MarshalIndent(result, "", " ")
129-
if err != nil {
130-
return err
131-
}
132-
fmt.Println(string(output))
133-
return nil
134-
}

0 commit comments

Comments
 (0)