|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/sourcegraph/src-cli/internal/mcp" |
| 8 | +) |
| 9 | + |
| 10 | +func init() { |
| 11 | + flagSet := flag.NewFlagSet("mcp", flag.ExitOnError) |
| 12 | + commands = append(commands, &command{ |
| 13 | + flagSet: flagSet, |
| 14 | + handler: mcpMain, |
| 15 | + }) |
| 16 | +} |
| 17 | +func mcpMain(args []string) error { |
| 18 | + fmt.Println("NOTE: This command is still experimental") |
| 19 | + tools, err := mcp.LoadToolDefinitions() |
| 20 | + if err != nil { |
| 21 | + return err |
| 22 | + } |
| 23 | + |
| 24 | + subcmd := args[0] |
| 25 | + if subcmd == "list-tools" { |
| 26 | + fmt.Println("The following tools are available:") |
| 27 | + for name := range tools { |
| 28 | + fmt.Printf(" • %s\n", name) |
| 29 | + } |
| 30 | + fmt.Println("\nUSAGE:") |
| 31 | + fmt.Printf(" • Invoke a tool\n") |
| 32 | + fmt.Printf(" src mcp <tool-name> <flags>\n") |
| 33 | + fmt.Printf("\n • View the Input / Output Schema of a tool\n") |
| 34 | + fmt.Printf(" src mcp <tool-name> schema\n") |
| 35 | + fmt.Printf("\n • List the available flags of a tool\n") |
| 36 | + fmt.Printf(" src mcp <tool-name> -h\n") |
| 37 | + fmt.Printf("\n • View the Input / Output Schema of a tool\n") |
| 38 | + fmt.Printf(" src mcp <tool-name> schema\n") |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + tool, ok := tools[subcmd] |
| 43 | + if !ok { |
| 44 | + return fmt.Errorf("tool definition for %q not found - run src mcp list-tools to see a list of available tools", subcmd) |
| 45 | + } |
| 46 | + |
| 47 | + flagArgs := args[1:] // skip subcommand name |
| 48 | + if len(args) > 1 && args[1] == "schema" { |
| 49 | + return printSchemas(tool) |
| 50 | + } |
| 51 | + |
| 52 | + flags, vars, err := mcp.BuildArgFlagSet(tool) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + if err := flags.Parse(flagArgs); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + mcp.DerefFlagValues(vars) |
| 60 | + |
| 61 | + if err := validateToolArgs(tool.InputSchema, args, vars); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + apiClient := cfg.apiClient(nil, flags.Output()) |
| 66 | + return handleMcpTool(context.Background(), apiClient, tool, vars) |
| 67 | +} |
| 68 | + |
| 69 | +func printSchemas(tool *mcp.ToolDef) error { |
| 70 | + input, err := json.MarshalIndent(tool.InputSchema, "", " ") |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + output, err := json.MarshalIndent(tool.OutputSchema, "", " ") |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + fmt.Printf("Input:\n%v\nOutput:\n%v\n", string(input), string(output)) |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func validateToolArgs(inputSchema mcp.SchemaObject, args []string, vars map[string]any) error { |
| 84 | + for _, reqName := range inputSchema.Required { |
| 85 | + if vars[reqName] == nil { |
| 86 | + return errors.Newf("no value provided for required flag --%s", reqName) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if len(args) < len(inputSchema.Required) { |
| 91 | + return errors.Newf("not enough arguments provided - the following flags are required:\n%s", strings.Join(inputSchema.Required, "\n")) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func handleMcpTool(ctx context.Context, client api.Client, tool *mcp.ToolDef, vars map[string]any) error { |
| 98 | + resp, err := mcp.DoToolRequest(ctx, client, tool, vars) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + result, err := mcp.DecodeToolResponse(resp) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + defer resp.Body.Close() |
| 108 | + |
| 109 | + output, err := json.MarshalIndent(result, "", " ") |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + fmt.Println(string(output)) |
| 114 | + return nil |
| 115 | +} |
0 commit comments