Repository navigation
Expand file tree
/
Copy pathcode_cmd.go
More file actions
488 lines (417 loc) · 15.6 KB
/
Copy pathcode_cmd.go
File metadata and controls
488 lines (417 loc) · 15.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/cache"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/cliclient"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/index"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/secret"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/server"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/storage"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/truncate"
"github.com/smart-mcp-proxy/mcpproxy-go/internal/upstream"
"github.com/mark3labs/mcp-go/mcp"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var (
codeCmd = &cobra.Command{
Use: "code",
Short: "JavaScript/TypeScript code execution for multi-tool orchestration",
Long: "Execute JavaScript or TypeScript code that orchestrates multiple upstream MCP tools in a single request",
}
codeExecCmd = &cobra.Command{
Use: "exec",
Short: "Execute JavaScript or TypeScript code",
Long: `Execute JavaScript or TypeScript code that can orchestrate multiple upstream MCP tools.
Use --language typescript to write TypeScript code with type annotations,
interfaces, enums, and generics. Types are automatically stripped before execution.
The code has access to:
- input: Global variable containing the input data (from --input or --input-file)
- call_tool(serverName, toolName, args): Function to invoke upstream MCP tools
The code must return a JSON-serializable value. The sandbox prevents access to:
- require() - No module loading
- setTimeout/setInterval - No timers
- Filesystem, network, or environment variables
Exit codes:
0 - Successful execution
1 - Execution failed (syntax error, runtime error, timeout, etc.)
2 - Invalid arguments or configuration`,
RunE: runCodeExec,
}
// Command flags for code exec
codeSource string
codeFile string
codeInput string
codeInputFile string
codeTimeout int
codeMaxToolCalls int
codeAllowedSrvs []string
codeLogLevel string
codeConfigPath string
codeLanguage string
)
// GetCodeCommand returns the code command for adding to the root command
func GetCodeCommand() *cobra.Command {
return codeCmd
}
func init() {
// Add exec subcommand to code command
codeCmd.AddCommand(codeExecCmd)
// Define flags for code exec command
codeExecCmd.Flags().StringVar(&codeSource, "code", "", "JavaScript code to execute (required if --file is not provided)")
codeExecCmd.Flags().StringVar(&codeFile, "file", "", "Path to JavaScript file to execute (required if --code is not provided)")
codeExecCmd.Flags().StringVar(&codeInput, "input", "{}", "Input data as JSON string (default: {})")
codeExecCmd.Flags().StringVar(&codeInputFile, "input-file", "", "Path to JSON file containing input data")
codeExecCmd.Flags().IntVar(&codeTimeout, "timeout", 120000, "Execution timeout in milliseconds (1-600000)")
codeExecCmd.Flags().IntVar(&codeMaxToolCalls, "max-tool-calls", 0, "Maximum number of tool calls (0 = unlimited)")
codeExecCmd.Flags().StringSliceVar(&codeAllowedSrvs, "allowed-servers", []string{}, "Comma-separated list of allowed server names (empty = all allowed)")
codeExecCmd.Flags().StringVarP(&codeLogLevel, "log-level", "l", "info", "Log level (trace, debug, info, warn, error)")
codeExecCmd.Flags().StringVarP(&codeConfigPath, "config", "c", "", "Path to MCP configuration file (default: ~/.mcpproxy/mcp_config.json)")
codeExecCmd.Flags().StringVar(&codeLanguage, "language", "javascript", "Source code language: javascript, typescript")
// Add examples
codeExecCmd.Example = ` # Execute inline code with input
mcpproxy code exec --code="({ result: input.value * 2 })" --input='{"value": 21}'
# Execute TypeScript code
mcpproxy code exec --language typescript --code="const x: number = 42; ({ result: x })"
# Execute code from file
mcpproxy code exec --file=script.js --input-file=params.json
# Execute TypeScript from file
mcpproxy code exec --language typescript --file=script.ts --input-file=params.json
# Call upstream tools
mcpproxy code exec --code="call_tool('github', 'get_user', {username: input.user})" --input='{"user":"octocat"}'
# With timeout and tool call limits
mcpproxy code exec --code="..." --timeout=60000 --max-tool-calls=10
# Restrict to specific servers
mcpproxy code exec --code="..." --allowed-servers=github,gitlab
# With trace logging for debugging
mcpproxy code exec --code="..." --log-level=trace`
}
func runCodeExec(_ *cobra.Command, _ []string) error {
// Validate arguments
if codeSource == "" && codeFile == "" {
fmt.Fprintf(os.Stderr, "Error: either --code or --file must be provided\n")
return exitError(2)
}
if codeSource != "" && codeFile != "" {
fmt.Fprintf(os.Stderr, "Error: --code and --file are mutually exclusive\n")
return exitError(2)
}
// Load code and input
code, inputData, err := loadCodeAndInput()
if err != nil {
return exitError(2)
}
// Validate options
if err := validateOptions(); err != nil {
return exitError(2)
}
// Load config to get data directory
globalConfig, err := loadCodeConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading configuration: %v\n", err)
return exitError(2)
}
// Check if code_execution is enabled
if !globalConfig.EnableCodeExecution {
fmt.Fprintf(os.Stderr, "Error: code_execution is disabled in configuration. Set 'enable_code_execution: true' in config file.\n")
return exitError(2)
}
// Create logger
logger, err := createCodeLogger(codeLogLevel)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating logger: %v\n", err)
return exitError(2)
}
// Detect daemon (socket first, then TCP fallback) and choose mode
if client, ok := newDaemonClient(globalConfig, logger.Sugar()); ok {
logger.Info("Detected running daemon, using client mode")
return runCodeExecClientMode(client, code, inputData, logger)
}
logger.Info("No daemon detected, using standalone mode")
return runCodeExecStandalone(globalConfig, code, inputData, logger)
}
// codeExecClientSlack is the head-room added to the server-side execution
// budget when deriving the client-side deadline: transport, queueing and
// (de)serialization all happen outside the daemon's own timeout accounting.
const codeExecClientSlack = 30 * time.Second
// codeExecClientTimeout returns how long the CLI waits for the daemon to
// answer a code execution request. The daemon enforces the --timeout budget
// itself, so the client deadline only has to outlive it.
func codeExecClientTimeout(timeoutMS int) time.Duration {
return time.Duration(timeoutMS)*time.Millisecond + codeExecClientSlack
}
// runCodeExecClientMode executes code via the daemon HTTP API.
func runCodeExecClientMode(client *cliclient.Client, code string, input map[string]interface{}, logger *zap.Logger) error {
// Ping daemon to verify connectivity
pingCtx, pingCancel := context.WithTimeout(context.Background(), 2*time.Second)
defer pingCancel()
if err := client.Ping(pingCtx); err != nil {
logger.Warn("Failed to ping daemon, falling back to standalone mode",
zap.Error(err),
zap.Duration("ping_timeout", 2*time.Second),
zap.String("fallback_mode", "standalone"))
// Fall back to standalone mode, which needs a usable configuration of
// its own (data dir, limits) - without one there is nothing to run.
cfg, cfgErr := loadCodeConfig()
if cfgErr == nil && cfg == nil {
cfgErr = errors.New("no configuration available")
}
if cfgErr != nil {
fmt.Fprintf(os.Stderr, "Error loading configuration for standalone fallback: %v\n", cfgErr)
return exitError(2)
}
return runCodeExecStandalone(cfg, code, input, logger)
}
// ADD CLI mode indicator
fmt.Fprintf(os.Stderr, "ℹ️ Using daemon mode - fast execution\n")
// Execute code via daemon
clientTimeout := codeExecClientTimeout(codeTimeout)
execCtx, execCancel := context.WithTimeout(context.Background(), clientTimeout)
defer execCancel()
result, err := client.CodeExec(
execCtx,
code,
input,
codeTimeout,
codeMaxToolCalls,
codeAllowedSrvs,
cliclient.CodeExecOptions{Language: codeLanguage},
)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
fmt.Fprintf(os.Stderr,
"Error: client-side timeout after %s waiting for the daemon to return results (execution budget --timeout=%dms); the daemon may still be running the code\n",
clientTimeout, codeTimeout)
return exitError(1)
}
// T029: Use formatErrorWithRequestID to include request_id in error output
fmt.Fprintf(os.Stderr, "Error calling daemon: %s\n", formatErrorWithRequestID(err))
return exitError(1)
}
// Output result
return outputResult(result)
}
// runCodeExecStandalone executes code locally (existing logic).
func runCodeExecStandalone(globalConfig *config.Config, code string, input map[string]interface{}, logger *zap.Logger) error {
// ADD standalone mode indicator
fmt.Fprintf(os.Stderr, "⚠️ Using standalone mode - daemon not detected (slower startup)\n")
// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(codeTimeout+5000)*time.Millisecond)
defer cancel()
// Create storage manager
storageManager, err := storage.NewManager(globalConfig.DataDir, logger.Sugar())
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating storage manager: %v\n", err)
return exitError(2)
}
defer storageManager.Close()
// Create index manager
indexManager, err := index.NewManager(globalConfig.DataDir, logger)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating index manager: %v\n", err)
return exitError(2)
}
defer indexManager.Close()
// Create secret resolver
secretResolver := secret.NewResolver()
// Create upstream manager
upstreamManager := upstream.NewManager(logger, globalConfig, storageManager.GetBoltDB(), secretResolver, storageManager)
// Create cache manager
cacheManager, err := cache.NewManager(storageManager.GetDB(), logger)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating cache manager: %v\n", err)
return exitError(2)
}
defer cacheManager.Close()
// Create truncator
truncator := truncate.NewTruncator(globalConfig.ToolResponseLimit)
// Create MCP proxy server
mcpProxy := server.NewMCPProxyServer(
storageManager,
indexManager,
upstreamManager,
cacheManager,
func() *truncate.Truncator { return truncator },
logger,
nil,
false,
globalConfig,
nil, // standalone one-shot: no runtime-owned signature cache
)
defer mcpProxy.Close()
// Build arguments for code_execution tool
args := map[string]interface{}{
"code": code,
"input": input,
"options": map[string]interface{}{
"timeout_ms": codeTimeout,
"max_tool_calls": codeMaxToolCalls,
"allowed_servers": codeAllowedSrvs,
},
}
// Pass language if not the default
if codeLanguage != "" && codeLanguage != "javascript" {
args["language"] = codeLanguage
}
// Call the code_execution tool
result, err := mcpProxy.CallBuiltInTool(ctx, "code_execution", args)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling code_execution tool: %v\n", err)
return exitError(1)
}
// Parse and output result
return outputResultFromMCP(result)
}
// Helper functions
func loadCodeAndInput() (string, map[string]interface{}, error) {
var code string
if codeFile != "" {
codeBytes, err := os.ReadFile(codeFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading code file: %v\n", err)
return "", nil, err
}
code = string(codeBytes)
} else {
code = codeSource
}
var inputData map[string]interface{}
if codeInputFile != "" {
inputBytes, err := os.ReadFile(codeInputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading input file: %v\n", err)
return "", nil, err
}
if err := json.Unmarshal(inputBytes, &inputData); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing input file JSON: %v\n", err)
return "", nil, err
}
} else {
if err := json.Unmarshal([]byte(codeInput), &inputData); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing input JSON: %v\n", err)
return "", nil, err
}
}
return code, inputData, nil
}
func validateOptions() error {
if codeTimeout < 1 || codeTimeout > 600000 {
fmt.Fprintf(os.Stderr, "Error: timeout must be between 1 and 600000 milliseconds\n")
return fmt.Errorf("invalid timeout")
}
if codeMaxToolCalls < 0 {
fmt.Fprintf(os.Stderr, "Error: max-tool-calls cannot be negative\n")
return fmt.Errorf("invalid max-tool-calls")
}
if codeLanguage != "javascript" && codeLanguage != "typescript" {
fmt.Fprintf(os.Stderr, "Error: unsupported language %q. Supported languages: javascript, typescript\n", codeLanguage)
return fmt.Errorf("invalid language")
}
return nil
}
func outputResult(result *cliclient.CodeExecResult) error {
output, err := json.MarshalIndent(result, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error formatting result: %v\n", err)
return exitError(1)
}
fmt.Println(string(output))
if !result.OK {
return exitError(1)
}
return nil
}
func outputResultFromMCP(result *mcp.CallToolResult) error {
// Existing logic to parse MCP result
for _, content := range result.Content {
if textContent, ok := mcp.AsTextContent(content); ok {
var execResult map[string]interface{}
if err := json.Unmarshal([]byte(textContent.Text), &execResult); err == nil {
output, err := json.MarshalIndent(execResult, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error formatting result: %v\n", err)
return exitError(1)
}
fmt.Println(string(output))
if okValue, exists := execResult["ok"].(bool); exists && !okValue {
return exitError(1)
}
return nil
}
}
}
fmt.Fprintf(os.Stderr, "Error: unexpected result format\n")
return exitError(1)
}
// loadCodeConfig loads the MCP configuration file for code command
func loadCodeConfig() (*config.Config, error) {
var configFilePath string
if codeConfigPath != "" {
configFilePath = codeConfigPath
} else {
// Use default path
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get user home directory: %w", err)
}
configFilePath = filepath.Join(homeDir, ".mcpproxy", "mcp_config.json")
}
// Check if config file exists
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
return nil, fmt.Errorf("configuration file not found at %s. Please run 'mcpproxy serve' first to create the config", configFilePath)
}
// Load configuration
globalConfig, err := config.LoadFromFile(configFilePath)
if err != nil {
return nil, fmt.Errorf("failed to load config from %s: %w", configFilePath, err)
}
// Respect global --data-dir flag
if dataDir != "" {
globalConfig.DataDir = dataDir
}
return globalConfig, nil
}
// createCodeLogger creates a zap logger with the specified level
func createCodeLogger(level string) (*zap.Logger, error) {
var zapLevel zap.AtomicLevel
switch strings.ToLower(level) {
case "trace", "debug":
zapLevel = zap.NewAtomicLevelAt(zap.DebugLevel)
case "info":
zapLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
case "warn":
zapLevel = zap.NewAtomicLevelAt(zap.WarnLevel)
case "error":
zapLevel = zap.NewAtomicLevelAt(zap.ErrorLevel)
default:
zapLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
}
config := zap.Config{
Level: zapLevel,
Development: false,
Encoding: "console",
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
return config.Build()
}
// exitError wraps an error with the given exit code
type exitCodeError struct {
code int
}
func (e exitCodeError) Error() string {
return fmt.Sprintf("exit code %d", e.code)
}
func exitError(code int) error {
os.Exit(code)
return exitCodeError{code: code}
}