-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.go
More file actions
64 lines (54 loc) · 1.6 KB
/
tools.go
File metadata and controls
64 lines (54 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cobrax
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"github.com/spf13/cobra"
)
// toolCommandFlags holds flags for the tools command.
type toolCommandFlags struct {
logLevel string
}
// toolCommand creates the 'mcp tools' command to export tool definitions.
func toolCommand(config *Config) *cobra.Command {
toolFlags := &toolCommandFlags{}
cmd := &cobra.Command{
Use: "tools",
Short: "Export tools as JSON",
Long: `Export available MCP tools to mcp-tools.json for inspection`,
RunE: func(cmd *cobra.Command, _ []string) error {
if config == nil {
config = &Config{}
}
if toolFlags.logLevel != "" {
if config.SloggerOptions == nil {
config.SloggerOptions = &slog.HandlerOptions{}
}
config.SloggerOptions.Level = parseLogLevel(toolFlags.logLevel)
}
file, err := os.OpenFile("mcp-tools.json", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
return fmt.Errorf("failed to create or open mcp-tools.json file: %w", err)
}
defer func() {
if closeErr := file.Close(); closeErr != nil {
cmd.Printf("Warning: failed to close file: %v\n", closeErr)
}
}()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
config.registerTools(cmd)
err = encoder.Encode(config.tools)
if err != nil {
return fmt.Errorf("failed to encode MCP tools to JSON: %w", err)
}
cmd.Printf("Successfully exported %d tools to mcp-tools.json\n", len(config.tools))
return nil
},
}
// Add flags
flags := cmd.Flags()
flags.StringVar(&toolFlags.logLevel, "log-level", "", "Log level (debug, info, warn, error)")
return cmd
}