()
| 153 | ) |
| 154 | |
| 155 | func main() { |
| 156 | rootCmd.AddCommand(schemaCmd) |
| 157 | |
| 158 | // Add global flag for stdio server command |
| 159 | rootCmd.PersistentFlags().String("stdio-server-cmd", "", "Shell command to invoke MCP server via stdio (required)") |
| 160 | _ = rootCmd.MarkPersistentFlagRequired("stdio-server-cmd") |
| 161 | |
| 162 | // Add global flag for pretty printing |
| 163 | rootCmd.PersistentFlags().Bool("pretty", true, "Pretty print MCP response (only for JSON or JSONL responses)") |
| 164 | |
| 165 | // Add the tools command to the root command |
| 166 | rootCmd.AddCommand(toolsCmd) |
| 167 | |
| 168 | // Execute the root command once to parse flags |
| 169 | _ = rootCmd.ParseFlags(os.Args[1:]) |
| 170 | |
| 171 | // Get pretty flag |
| 172 | prettyPrint, err := rootCmd.Flags().GetBool("pretty") |
| 173 | if err != nil { |
| 174 | _, _ = fmt.Fprintf(os.Stderr, "Error getting pretty flag: %v\n", err) |
| 175 | os.Exit(1) |
| 176 | } |
| 177 | // Get server command |
| 178 | serverCmd, err := rootCmd.Flags().GetString("stdio-server-cmd") |
| 179 | if err == nil && serverCmd != "" { |
| 180 | // Fetch schema from server |
| 181 | jsonRequest, err := buildJSONRPCRequest("tools/list", "", nil) |
| 182 | if err == nil { |
| 183 | response, err := executeServerCommand(serverCmd, jsonRequest) |
| 184 | if err == nil { |
| 185 | // Parse the schema response |
| 186 | var schemaResp SchemaResponse |
| 187 | if err := json.Unmarshal([]byte(response), &schemaResp); err == nil { |
| 188 | // Add all the generated commands as subcommands of tools |
| 189 | for _, tool := range schemaResp.Result.Tools { |
| 190 | addCommandFromTool(toolsCmd, &tool, prettyPrint) |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Execute |
| 198 | if err := rootCmd.Execute(); err != nil { |
| 199 | _, _ = fmt.Fprintf(os.Stderr, "Error executing command: %v\n", err) |
| 200 | os.Exit(1) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // addCommandFromTool creates a cobra command from a tool schema |
| 205 | func addCommandFromTool(toolsCmd *cobra.Command, tool *Tool, prettyPrint bool) { |
nothing calls this directly
no test coverage detected