-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmcp.go
More file actions
171 lines (147 loc) · 4.81 KB
/
Copy pathmcp.go
File metadata and controls
171 lines (147 loc) · 4.81 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cmd
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/charmbracelet/glamour"
"github.com/spachava753/cpe/internal/commands"
"github.com/spachava753/cpe/internal/config"
"github.com/spf13/cobra"
)
var (
mcpServerName string
mcpToolName string
mcpToolArgs string
)
// mcpCmd represents the mcp command
var mcpCmd = &cobra.Command{
Use: "mcp",
Short: "Model Context Protocol client",
Long: `Interact with Model Context Protocol (MCP) servers.`,
RunE: func(cmd *cobra.Command, args []string) error {
// If no subcommand is specified, show help
return cmd.Help()
},
}
// mcpListServersCmd represents the 'mcp list-servers' subcommand
var mcpListServersCmd = &cobra.Command{
Use: "list-servers",
Short: "List configured MCP servers",
Long: `List all MCP servers defined in .cpemcp.json configuration file.`,
Aliases: []string{"ls-servers"},
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.ResolveConfig(configPath, config.RuntimeOptions{})
if err != nil {
return err
}
return commands.MCPListServers(cmd.Context(), commands.MCPListServersOptions{
MCPServers: cfg.MCPServers,
Writer: os.Stdout,
})
},
}
// mcpInfoCmd represents the 'mcp info' subcommand
var mcpInfoCmd = &cobra.Command{
Use: "info [server_name]",
Short: "Get information about an MCP server",
Long: `Initialize connection to an MCP server and show its information.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.ResolveConfig(configPath, config.RuntimeOptions{})
if err != nil {
return err
}
return commands.MCPInfo(cmd.Context(), commands.MCPInfoOptions{
MCPServers: cfg.MCPServers,
ServerName: args[0],
Writer: os.Stdout,
Timeout: 30 * time.Second,
})
},
}
// mcpListToolsCmd represents the 'mcp list-tools' subcommand
var mcpListToolsCmd = &cobra.Command{
Use: "list-tools [server_name]",
Short: "List tools available on an MCP server",
Long: `Connect to an MCP server and list available tools with their input schemas.`,
Example: ` # List tools with human-readable schema
cpe mcp list-tools my-server
# List tools with JSON schema format
cpe mcp list-tools my-server --json
# Show all tools including filtered ones
cpe mcp list-tools my-server --show-all
# Show only filtered-out tools
cpe mcp list-tools my-server --show-filtered`,
Aliases: []string{"ls-tools"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.ResolveConfig(configPath, config.RuntimeOptions{})
if err != nil {
return err
}
showAll, _ := cmd.Flags().GetBool("show-all")
showFiltered, _ := cmd.Flags().GetBool("show-filtered")
renderer, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(120),
)
if err != nil {
return fmt.Errorf("failed to create markdown renderer: %w", err)
}
return commands.MCPListTools(cmd.Context(), commands.MCPListToolsOptions{
MCPServers: cfg.MCPServers,
ServerName: args[0],
Writer: os.Stdout,
ShowAll: showAll,
ShowFiltered: showFiltered,
Renderer: renderer,
})
},
}
// mcpCallToolCmd represents the 'mcp call-tool' subcommand
var mcpCallToolCmd = &cobra.Command{
Use: "call-tool",
Short: "Call a tool on an MCP server",
Long: `Call a specific tool on an MCP server with arguments.`,
RunE: func(cmd *cobra.Command, args []string) error {
if mcpServerName == "" {
return fmt.Errorf("--server is required")
}
if mcpToolName == "" {
return fmt.Errorf("--tool is required")
}
cfg, err := config.ResolveConfig(configPath, config.RuntimeOptions{})
if err != nil {
return err
}
toolArgs := make(map[string]any)
if mcpToolArgs != "" {
if err := json.Unmarshal([]byte(mcpToolArgs), &toolArgs); err != nil {
return fmt.Errorf("invalid tool arguments JSON: %w", err)
}
}
return commands.MCPCallTool(cmd.Context(), commands.MCPCallToolOptions{
MCPServers: cfg.MCPServers,
ServerName: mcpServerName,
ToolName: mcpToolName,
ToolArgs: toolArgs,
Writer: os.Stdout,
})
},
}
func init() {
rootCmd.AddCommand(mcpCmd)
// Add subcommands to mcp command
mcpCmd.AddCommand(mcpListServersCmd)
mcpCmd.AddCommand(mcpInfoCmd)
mcpCmd.AddCommand(mcpListToolsCmd)
mcpCmd.AddCommand(mcpCallToolCmd)
// Add flags to mcp list-tools command
mcpListToolsCmd.Flags().Bool("show-all", false, "Show all tools including filtered ones")
mcpListToolsCmd.Flags().Bool("show-filtered", false, "Show only filtered-out tools")
// Add flags to call-tool command
mcpCallToolCmd.Flags().StringVar(&mcpServerName, "server", "", "MCP server name")
mcpCallToolCmd.Flags().StringVar(&mcpToolName, "tool", "", "Tool name to call")
mcpCallToolCmd.Flags().StringVar(&mcpToolArgs, "args", "{}", "Tool arguments in JSON format")
}