Skip to content

Commit f243dec

Browse files
committed
add mcp list-tools to list available tool calls
1 parent 73b6673 commit f243dec

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

cmd/src/mcp.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
8+
func init() {
9+
flagSet := flag.NewFlagSet("mcp", flag.ExitOnError)
10+
handler := func(args []string) error {
11+
return mcpMain(args)
12+
}
13+
14+
commands = append(commands, &command{
15+
flagSet: flagSet,
16+
handler: handler,
17+
})
18+
}
19+
func mcpMain(args []string) error {
20+
fmt.Println("NOTE: This command is still experimental")
21+
tools, err := LoadMCPToolDefinitions(mcpToolListJSON)
22+
if err != nil {
23+
return err
24+
}
25+
26+
subcmd := args[0]
27+
if subcmd == "list-tools" {
28+
fmt.Println("Available tools")
29+
for name := range tools {
30+
fmt.Printf("- %s\n", name)
31+
}
32+
return nil
33+
}
34+
35+
tool, ok := tools[subcmd]
36+
if !ok {
37+
return fmt.Errorf("tool definition for %q not found - run src mcp list-tools to see a list of available tools", subcmd)
38+
}
39+
return handleMcpTool(tool, args[1:])
40+
}
41+
42+
func handleMcpTool(tool *MCPToolDef, args []string) error {
43+
fmt.Printf("handling tool %q args: %+v", tool.Name, args)
44+
return nil
45+
}

cmd/src/mcp_parse.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
_ "embed"
66
"encoding/json"
77
"fmt"
8+
"strings"
89

910
"github.com/sourcegraph/sourcegraph/lib/errors"
1011
)
@@ -143,6 +144,12 @@ func (p *Parser) parseProperties(props map[string]json.RawMessage) map[string]Sc
143144
return res
144145
}
145146

147+
// normalizeToolName takes mcp tool names like 'sg_keyword_search' and normalizes it to 'keyword-search"
148+
func normalizeToolName(toolName string) string {
149+
toolName, _ = strings.CutPrefix(toolName, "sg_")
150+
return strings.ReplaceAll(toolName, "_", "-")
151+
}
152+
146153
func LoadMCPToolDefinitions(data []byte) (map[string]*MCPToolDef, error) {
147154
defs := struct {
148155
Tools []struct {
@@ -154,15 +161,15 @@ func LoadMCPToolDefinitions(data []byte) (map[string]*MCPToolDef, error) {
154161
}{}
155162

156163
if err := json.Unmarshal(data, &defs); err != nil {
157-
// TODO: think we should panic instead
158164
return nil, err
159165
}
160166

161167
tools := map[string]*MCPToolDef{}
162168
parser := &Parser{}
163169

164170
for _, t := range defs.Tools {
165-
tools[t.Name] = &MCPToolDef{
171+
name := normalizeToolName(t.Name)
172+
tools[name] = &MCPToolDef{
166173
Name: t.Name,
167174
Description: t.Description,
168175
InputSchema: parser.parseRootSchema(t.InputSchema),

0 commit comments

Comments
 (0)