-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmcp.go
More file actions
304 lines (247 loc) · 9.13 KB
/
mcp.go
File metadata and controls
304 lines (247 loc) · 9.13 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package cmd
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/timescale/tiger-cli/internal/tiger/logging"
"github.com/timescale/tiger-cli/internal/tiger/mcp"
)
// buildMCPCmd creates the MCP server command with subcommands
func buildMCPCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "mcp",
Short: "Tiger Model Context Protocol (MCP) server",
Long: `Tiger Model Context Protocol (MCP) server for AI assistant integration.
The MCP server provides programmatic access to Tiger Cloud platform resources
through Claude and other AI assistants. It exposes Tiger CLI functionality as MCP
tools that can be called by AI agents.
Configuration:
The server automatically uses the CLI's stored authentication and configuration.
No additional setup is required beyond running 'tiger auth login'.
Use 'tiger mcp start' to launch the MCP server.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// Show help when no subcommand is specified
cmd.Help()
},
}
// Add subcommands
cmd.AddCommand(buildMCPInstallCmd())
cmd.AddCommand(buildMCPStartCmd())
return cmd
}
// buildMCPInstallCmd creates the install subcommand for configuring editors
func buildMCPInstallCmd() *cobra.Command {
var noBackup bool
var configPath string
cmd := &cobra.Command{
Use: "install [client]",
Short: "Install and configure Tiger MCP server for a client",
Long: fmt.Sprintf(`Install and configure the Tiger MCP server for a specific MCP client or AI assistant.
This command automates the configuration process by modifying the appropriate
configuration files for the specified client.
%s
The command will:
- Automatically detect the appropriate configuration file location
- Create the configuration directory if it doesn't exist
- Create a backup of existing configuration by default
- Merge with existing MCP server configurations (doesn't overwrite other servers)
- Validate the configuration after installation
If no client is specified, you'll be prompted to select one interactively.
Examples:
# Interactive client selection
tiger mcp install
# Install for Claude Code (User scope - available in all projects)
tiger mcp install claude-code
# Install for Cursor IDE
tiger mcp install cursor
# Install without creating backup
tiger mcp install claude-code --no-backup
# Use custom configuration file path
tiger mcp install claude-code --config-path ~/custom/config.json`, generateSupportedEditorsHelp()),
Args: cobra.MaximumNArgs(1),
ValidArgs: getValidEditorNames(),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
var clientName string
if len(args) == 0 {
// No client specified, prompt user to select one
var err error
clientName, err = selectClientInteractively(cmd.OutOrStdout())
if err != nil {
return fmt.Errorf("failed to select client: %w", err)
}
if clientName == "" {
return fmt.Errorf("no client selected")
}
} else {
clientName = args[0]
}
return installMCPForClient(clientName, !noBackup, configPath)
},
}
// Add flags
cmd.Flags().BoolVar(&noBackup, "no-backup", false, "Skip creating backup of existing configuration (default: create backup)")
cmd.Flags().StringVar(&configPath, "config-path", "", "Custom path to configuration file (overrides default locations)")
return cmd
}
// buildMCPStartCmd creates the start subcommand with transport options
func buildMCPStartCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start the Tiger MCP server",
Long: `Start the Tiger Model Context Protocol (MCP) server for AI assistant integration.
The MCP server provides programmatic access to Tiger Cloud platform resources
through Claude and other AI assistants. By default, it uses stdio transport.
Examples:
# Start with stdio transport (default)
tiger mcp start
# Start with stdio transport (explicit)
tiger mcp start stdio
# Start with HTTP transport
tiger mcp start http`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
// Default behavior when no subcommand is specified - use stdio
cmd.SilenceUsage = true
return startStdioServer(cmd.Context())
},
}
// Add transport subcommands
cmd.AddCommand(buildMCPStdioCmd())
cmd.AddCommand(buildMCPHTTPCmd())
return cmd
}
// buildMCPStdioCmd creates the stdio subcommand
func buildMCPStdioCmd() *cobra.Command {
return &cobra.Command{
Use: "stdio",
Short: "Start MCP server with stdio transport",
Long: `Start the MCP server using standard input/output transport.
Examples:
# Start with stdio transport
tiger mcp start stdio`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return startStdioServer(cmd.Context())
},
}
}
// buildMCPHTTPCmd creates the http subcommand with port/host flags
func buildMCPHTTPCmd() *cobra.Command {
var httpPort int
var httpHost string
cmd := &cobra.Command{
Use: "http",
Short: "Start MCP server with HTTP transport",
Long: `Start the MCP server using HTTP transport.
The server will automatically find an available port if the specified port is busy.
Examples:
# Start HTTP server on default port 8080
tiger mcp start http
# Start HTTP server on custom port
tiger mcp start http --port 3001
# Start HTTP server on all interfaces
tiger mcp start http --host 0.0.0.0 --port 8080
# Start server and bind to specific interface
tiger mcp start http --host 192.168.1.100 --port 9000`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return startHTTPServer(cmd.Context(), httpHost, httpPort)
},
}
// Add HTTP-specific flags
cmd.Flags().IntVar(&httpPort, "port", 8080, "Port to run HTTP server on")
cmd.Flags().StringVar(&httpHost, "host", "localhost", "Host to bind to")
return cmd
}
// startStdioServer starts the MCP server with stdio transport
func startStdioServer(ctx context.Context) error {
logging.Info("Starting Tiger MCP server", zap.String("transport", "stdio"))
// Create MCP server
server, err := mcp.NewServer(ctx)
if err != nil {
return fmt.Errorf("failed to create MCP server: %w", err)
}
// Start the stdio transport
err = server.StartStdio(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("failed to start MCP server: %w", err)
}
// Close the MCP server when finished
if err := server.Close(); err != nil {
return fmt.Errorf("failed to close MCP server: %w", err)
}
return nil
}
// startHTTPServer starts the MCP server with HTTP transport
func startHTTPServer(ctx context.Context, host string, port int) error {
logging.Info("Starting Tiger MCP server", zap.String("transport", "http"))
// Create MCP server
server, err := mcp.NewServer(ctx)
if err != nil {
return fmt.Errorf("failed to create MCP server: %w", err)
}
// Find available port and get the listener
listener, actualPort, err := getListener(host, port)
if err != nil {
return fmt.Errorf("failed to get listener: %w", err)
}
defer listener.Close()
if actualPort != port {
logging.Info("Specified port was busy, using alternative port",
zap.Int("requested_port", port),
zap.Int("actual_port", actualPort),
)
}
address := fmt.Sprintf("%s:%d", host, actualPort)
// Create HTTP server
httpServer := &http.Server{
Handler: server.HTTPHandler(),
}
fmt.Printf("🚀 Tiger MCP server listening on http://%s\n", address)
fmt.Printf("💡 Use Ctrl+C to stop the server\n")
// Start server in goroutine using the existing listener
go func() {
if err := httpServer.Serve(listener); err != nil && err != http.ErrServerClosed {
logging.Error("HTTP server error", zap.Error(err))
}
}()
// Wait for context cancellation. Once canceled, stop handling signals and
// revert to default signal handling behavior. This allows a second
// SIGINT/SIGTERM to forcibly kill the server (useful if there's currently
// an active MCP session but you want to kill it anyways). Note that stop()
// is idempotent and safe to call multiple times, so it's okay that it's
// called here and via the deferred call above.
<-ctx.Done()
// Shutdown server gracefully
logging.Info("Gracefully shutting down HTTP server..., press control-C twice to immediately shutdown")
if err := httpServer.Shutdown(context.Background()); err != nil {
return fmt.Errorf("failed to shut down HTTP server: %w", err)
}
// Close the MCP server when finished
if err := server.Close(); err != nil {
return fmt.Errorf("failed to close MCP server: %w", err)
}
return nil
}
// getListener finds an available port starting from the specified port and returns the listener
func getListener(host string, startPort int) (net.Listener, int, error) {
for port := startPort; port < startPort+100; port++ {
address := fmt.Sprintf("%s:%d", host, port)
listener, err := net.Listen("tcp", address)
if err == nil {
return listener, port, nil
}
}
return nil, 0, fmt.Errorf("no available port found in range %d-%d", startPort, startPort+99)
}