-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestcmd.go
More file actions
91 lines (75 loc) · 2.53 KB
/
restcmd.go
File metadata and controls
91 lines (75 loc) · 2.53 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
package cobrax
import (
"fmt"
"log/slog"
"github.com/spf13/cobra"
)
// restCommandFlags holds flags for the rest command.
type restCommandFlags struct {
logLevel string
host string
port int
baseURL string
}
// restCommand creates the 'mcp rest' subcommand.
//
// It starts a plain HTTP server that exposes every cobra command as a REST
// endpoint:
//
// POST /{toolName}
//
// The request body is a flat JSON object (flags and positional args at the same
// level). The response body contains stdout, stderr and the exit code.
//
// Example:
//
// # start the server
// myapp mcp rest --port 9090 --base-url http://api.example.com
//
// # call a tool
// curl -s -X POST http://localhost:9090/myapp_oncall \
// -H 'Content-Type: application/json' \
// -d '{"module":"bke","title":"pod crash","level":2}'
func restCommand(config *Config) *cobra.Command {
f := &restCommandFlags{}
cmd := &cobra.Command{
Use: "rest",
Short: "Serve cobra commands as REST API",
Long: `Start an HTTP server that exposes every cobra command as a REST endpoint.
Each command is reachable at:
POST /{toolName}
where toolName matches the MCP tool name (e.g. "myapp_sre_open").
Request body — flat JSON object, flags and positional args at the same level:
{ "namespace": "prod", "module": "bke", "level": 3 }
Response body:
{ "stdout": "...", "stderr": "...", "exitCode": 0 }`,
Example: ` # Start on the default port
myapp mcp rest
# Custom host, port and externally-visible base URL
myapp mcp rest --host 0.0.0.0 --port 9090 --base-url https://api.example.com
# Call a tool
curl -s -X POST http://localhost:8080/myapp_oncall \
-H 'Content-Type: application/json' \
-d '{"module":"bke","title":"pod crash","level":2}'`,
RunE: func(cmd *cobra.Command, _ []string) error {
if config == nil {
config = &Config{}
}
if f.logLevel != "" {
level := parseLogLevel(f.logLevel)
if config.SloggerOptions == nil {
config.SloggerOptions = &slog.HandlerOptions{}
}
config.SloggerOptions.Level = level
}
addr := fmt.Sprintf("%s:%d", f.host, f.port)
return config.serveREST(cmd, addr, f.baseURL)
},
}
flags := cmd.Flags()
flags.StringVar(&f.logLevel, "log-level", "", "Log level (debug, info, warn, error)")
flags.StringVar(&f.host, "host", "", "Host address to listen on (default: all interfaces)")
flags.IntVar(&f.port, "port", 8080, "Port number to listen on")
flags.StringVar(&f.baseURL, "base-url", "", "Externally-visible base URL logged at startup (e.g. https://api.example.com)")
return cmd
}