diff --git a/cmd/mcpcurl/main.go b/cmd/mcpcurl/main.go index bc192587a..7c0a67d7c 100644 --- a/cmd/mcpcurl/main.go +++ b/cmd/mcpcurl/main.go @@ -174,24 +174,11 @@ func main() { _, _ = fmt.Fprintf(os.Stderr, "Error getting pretty flag: %v\n", err) os.Exit(1) } - // Get server command + + // Get server command and load tools serverCmd, err := rootCmd.Flags().GetString("stdio-server-cmd") if err == nil && serverCmd != "" { - // Fetch schema from server - jsonRequest, err := buildJSONRPCRequest("tools/list", "", nil) - if err == nil { - response, err := executeServerCommand(serverCmd, jsonRequest) - if err == nil { - // Parse the schema response - var schemaResp SchemaResponse - if err := json.Unmarshal([]byte(response), &schemaResp); err == nil { - // Add all the generated commands as subcommands of tools - for _, tool := range schemaResp.Result.Tools { - addCommandFromTool(toolsCmd, &tool, prettyPrint) - } - } - } - } + loadToolsFromServer(serverCmd, prettyPrint) } // Execute @@ -201,6 +188,28 @@ func main() { } } +// loadToolsFromServer fetches the schema from the MCP server and registers tool commands +func loadToolsFromServer(serverCmd string, prettyPrint bool) { + jsonRequest, err := buildJSONRPCRequest("tools/list", "", nil) + if err != nil { + return + } + + response, err := executeServerCommand(serverCmd, jsonRequest) + if err != nil { + return + } + + var schemaResp SchemaResponse + if err := json.Unmarshal([]byte(response), &schemaResp); err != nil { + return + } + + for _, tool := range schemaResp.Result.Tools { + addCommandFromTool(toolsCmd, &tool, prettyPrint) + } +} + // addCommandFromTool creates a cobra command from a tool schema func addCommandFromTool(toolsCmd *cobra.Command, tool *Tool, prettyPrint bool) { // Create command from tool diff --git a/pkg/github/repositories.go b/pkg/github/repositories.go index 29f776a05..cade0f2b4 100644 --- a/pkg/github/repositories.go +++ b/pkg/github/repositories.go @@ -19,6 +19,10 @@ import ( "github.com/mark3labs/mcp-go/server" ) +const ( + refsHeadsPrefix = "refs/heads/" +) + func GetCommit(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { return mcp.NewTool("get_commit", mcp.WithDescription(t("TOOL_GET_COMMITS_DESCRIPTION", "Get details for a commit from a GitHub repository")), @@ -752,7 +756,7 @@ func DeleteFile(getClient GetClientFn, t translations.TranslationHelperFunc) (to } // Get the reference for the branch - ref, resp, err := client.Git.GetRef(ctx, owner, repo, "refs/heads/"+branch) + ref, resp, err := client.Git.GetRef(ctx, owner, repo, refsHeadsPrefix+branch) if err != nil { return nil, fmt.Errorf("failed to get branch reference: %w", err) } @@ -931,7 +935,7 @@ func CreateBranch(getClient GetClientFn, t translations.TranslationHelperFunc) ( } // Get SHA of source branch - ref, resp, err := client.Git.GetRef(ctx, owner, repo, "refs/heads/"+fromBranch) + ref, resp, err := client.Git.GetRef(ctx, owner, repo, refsHeadsPrefix+fromBranch) if err != nil { return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to get reference", @@ -943,7 +947,7 @@ func CreateBranch(getClient GetClientFn, t translations.TranslationHelperFunc) ( // Create new branch newRef := &github.Reference{ - Ref: github.Ptr("refs/heads/" + branch), + Ref: github.Ptr(refsHeadsPrefix + branch), Object: &github.GitObject{SHA: ref.Object.SHA}, } @@ -1041,7 +1045,7 @@ func PushFiles(getClient GetClientFn, t translations.TranslationHelperFunc) (too } // Get the reference for the branch - ref, resp, err := client.Git.GetRef(ctx, owner, repo, "refs/heads/"+branch) + ref, resp, err := client.Git.GetRef(ctx, owner, repo, refsHeadsPrefix+branch) if err != nil { return ghErrors.NewGitHubAPIErrorResponse(ctx, "failed to get branch reference",