From a3ca77e6b2e26e934cd3fa245cff7818d7f3f20c Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 19 Sep 2025 18:22:58 +0000 Subject: [PATCH] Fix SonarQube issues: extract stdio-server-cmd constant and reduce main() cognitive complexity - Extract 'stdio-server-cmd' string literal to constant (fixes go:S1192) - Refactor main() function to reduce cognitive complexity from 18 to under 15 (fixes go:S3776) - Break down main() into smaller focused functions: setupFlags() and loadSchemaAndAddCommands() - All 6 occurrences of the string literal now use the stdioServerCmdFlag constant - Maintains existing functionality while improving code maintainability Co-Authored-By: Zoheb Munshi --- cmd/mcpcurl/main.go | 83 ++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/cmd/mcpcurl/main.go b/cmd/mcpcurl/main.go index bc192587a..2eebadbab 100644 --- a/cmd/mcpcurl/main.go +++ b/cmd/mcpcurl/main.go @@ -16,6 +16,10 @@ import ( "github.com/spf13/viper" ) +const ( + stdioServerCmdFlag = "stdio-server-cmd" +) + type ( // SchemaResponse represents the top-level response containing tools SchemaResponse struct { @@ -107,9 +111,9 @@ var ( } // Check if the required global flag is provided - serverCmd, _ := cmd.Flags().GetString("stdio-server-cmd") + serverCmd, _ := cmd.Flags().GetString(stdioServerCmdFlag) if serverCmd == "" { - return fmt.Errorf("--stdio-server-cmd is required") + return fmt.Errorf("--%s is required", stdioServerCmdFlag) } return nil }, @@ -119,11 +123,11 @@ var ( schemaCmd = &cobra.Command{ Use: "schema", Short: "Fetch schema from MCP server", - Long: "Fetches the tools schema from the MCP server specified by --stdio-server-cmd", + Long: "Fetches the tools schema from the MCP server specified by --" + stdioServerCmdFlag, RunE: func(cmd *cobra.Command, _ []string) error { - serverCmd, _ := cmd.Flags().GetString("stdio-server-cmd") + serverCmd, _ := cmd.Flags().GetString(stdioServerCmdFlag) if serverCmd == "" { - return fmt.Errorf("--stdio-server-cmd is required") + return fmt.Errorf("--%s is required", stdioServerCmdFlag) } // Build the JSON-RPC request for tools/list @@ -153,17 +157,7 @@ var ( ) func main() { - rootCmd.AddCommand(schemaCmd) - - // Add global flag for stdio server command - rootCmd.PersistentFlags().String("stdio-server-cmd", "", "Shell command to invoke MCP server via stdio (required)") - _ = rootCmd.MarkPersistentFlagRequired("stdio-server-cmd") - - // Add global flag for pretty printing - rootCmd.PersistentFlags().Bool("pretty", true, "Pretty print MCP response (only for JSON or JSONL responses)") - - // Add the tools command to the root command - rootCmd.AddCommand(toolsCmd) + setupFlags() // Execute the root command once to parse flags _ = rootCmd.ParseFlags(os.Args[1:]) @@ -174,24 +168,11 @@ func main() { _, _ = fmt.Fprintf(os.Stderr, "Error getting pretty flag: %v\n", err) os.Exit(1) } - // Get server command - serverCmd, err := rootCmd.Flags().GetString("stdio-server-cmd") + + // Get server command and load schema if available + serverCmd, err := rootCmd.Flags().GetString(stdioServerCmdFlag) 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) - } - } - } - } + loadSchemaAndAddCommands(serverCmd, prettyPrint) } // Execute @@ -201,6 +182,38 @@ func main() { } } +func setupFlags() { + rootCmd.AddCommand(schemaCmd) + + // Add global flag for stdio server command + rootCmd.PersistentFlags().String(stdioServerCmdFlag, "", "Shell command to invoke MCP server via stdio (required)") + _ = rootCmd.MarkPersistentFlagRequired(stdioServerCmdFlag) + + // Add global flag for pretty printing + rootCmd.PersistentFlags().Bool("pretty", true, "Pretty print MCP response (only for JSON or JSONL responses)") + + // Add the tools command to the root command + rootCmd.AddCommand(toolsCmd) +} + +func loadSchemaAndAddCommands(serverCmd string, prettyPrint bool) { + // 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) + } + } + } + } +} + // addCommandFromTool creates a cobra command from a tool schema func addCommandFromTool(toolsCmd *cobra.Command, tool *Tool, prettyPrint bool) { // Create command from tool @@ -222,9 +235,9 @@ func addCommandFromTool(toolsCmd *cobra.Command, tool *Tool, prettyPrint bool) { } // Execute the server command - serverCmd, err := cmd.Flags().GetString("stdio-server-cmd") + serverCmd, err := cmd.Flags().GetString(stdioServerCmdFlag) if err != nil { - _, _ = fmt.Fprintf(os.Stderr, "failed to get stdio-server-cmd: %v\n", err) + _, _ = fmt.Fprintf(os.Stderr, "failed to get %s: %v\n", stdioServerCmdFlag, err) return } response, err := executeServerCommand(serverCmd, jsonData)