MCPcopy Index your code
hub / github.com/github/github-mcp-server / addCommandFromTool

Function addCommandFromTool

cmd/mcpcurl/main.go:205–308  ·  view source on GitHub ↗

addCommandFromTool creates a cobra command from a tool schema

(toolsCmd *cobra.Command, tool *Tool, prettyPrint bool)

Source from the content-addressed store, hash-verified

203
204// addCommandFromTool creates a cobra command from a tool schema
205func addCommandFromTool(toolsCmd *cobra.Command, tool *Tool, prettyPrint bool) {
206 // Create command from tool
207 cmd := &cobra.Command{
208 Use: tool.Name,
209 Short: tool.Description,
210 Run: func(cmd *cobra.Command, _ []string) {
211 // Build a map of arguments from flags
212 arguments, err := buildArgumentsMap(cmd, tool)
213 if err != nil {
214 _, _ = fmt.Fprintf(os.Stderr, "failed to build arguments map: %v\n", err)
215 return
216 }
217
218 jsonData, err := buildJSONRPCRequest("tools/call", tool.Name, arguments)
219 if err != nil {
220 _, _ = fmt.Fprintf(os.Stderr, "failed to build JSONRPC request: %v\n", err)
221 return
222 }
223
224 // Execute the server command
225 serverCmd, err := cmd.Flags().GetString("stdio-server-cmd")
226 if err != nil {
227 _, _ = fmt.Fprintf(os.Stderr, "failed to get stdio-server-cmd: %v\n", err)
228 return
229 }
230 response, err := executeServerCommand(serverCmd, jsonData)
231 if err != nil {
232 _, _ = fmt.Fprintf(os.Stderr, "error executing server command: %v\n", err)
233 return
234 }
235 if err := printResponse(response, prettyPrint); err != nil {
236 _, _ = fmt.Fprintf(os.Stderr, "error printing response: %v\n", err)
237 return
238 }
239 },
240 }
241
242 // Initialize viper for this command
243 viperInit := func() {
244 viper.Reset()
245 viper.AutomaticEnv()
246 viper.SetEnvPrefix(strings.ToUpper(tool.Name))
247 viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
248 }
249
250 // We'll call the init function directly instead of with cobra.OnInitialize
251 // to avoid conflicts between commands
252 viperInit()
253
254 // Add flags based on schema properties
255 for name, prop := range tool.InputSchema.Properties {
256 isRequired := slices.Contains(tool.InputSchema.Required, name)
257
258 // Enhance description to indicate if parameter is optional
259 description := prop.Description
260 if !isRequired {
261 description += " (optional)"
262 }

Callers 1

mainFunction · 0.85

Calls 5

buildArgumentsMapFunction · 0.85
buildJSONRPCRequestFunction · 0.85
executeServerCommandFunction · 0.85
printResponseFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected