Repository navigation
Expand file tree
/
Copy pathdynamic.go
More file actions
291 lines (251 loc) · 7.58 KB
/
Copy pathdynamic.go
File metadata and controls
291 lines (251 loc) · 7.58 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
package cmd
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/pipeboard-co/pipeboard-cli/internal/client"
"github.com/spf13/cobra"
)
const autoRefreshInterval = 1 * time.Hour
// registerDynamicCommands loads the tools cache and registers platform commands.
func registerDynamicCommands() {
cache, err := loadToolsCache()
if err != nil {
return // No cache — user needs to run 'pipeboard refresh'
}
for _, server := range knownServers {
serverCache, ok := cache.Servers[server.Path]
if !ok || len(serverCache.Tools) == 0 {
continue
}
platformCmd := &cobra.Command{
Use: server.CommandName,
Short: server.Description,
}
for _, tool := range serverCache.Tools {
cmd := buildToolCommand(server, tool)
platformCmd.AddCommand(cmd)
}
rootCmd.AddCommand(platformCmd)
}
// Check for tool updates in the background
maybeAutoRefresh(cache)
}
// maybeAutoRefresh checks whether the tools cache is stale and, if so,
// spawns a detached background process to check the hash and refresh if
// needed. The current command always uses the existing cache; updates
// take effect on the next invocation.
func maybeAutoRefresh(cache *ToolsCache) {
// Guard against recursive spawning: the child sets this env var so
// it won't spawn another child during its own init().
if os.Getenv("PIPEBOARD_AUTO_REFRESH") == "1" {
return
}
if time.Since(cache.LastCheckedAt) < autoRefreshInterval {
return
}
// Spawn a detached "pipeboard refresh --if-changed" subprocess.
// It survives parent exit and writes the cache for the next run.
self, err := os.Executable()
if err != nil {
return
}
cmd := exec.Command(self, "refresh", "--if-changed")
cmd.Env = append(os.Environ(), "PIPEBOARD_AUTO_REFRESH=1")
setSysProcAttr(cmd)
cmd.Stdout = nil
cmd.Stderr = nil
_ = cmd.Start()
}
// runAutoRefresh is the implementation behind "refresh --if-changed".
// It checks the hash first and only does a full refresh if tools changed.
func runAutoRefresh(baseURL, token string) error {
cache, _ := loadToolsCache()
hashResult, err := client.FetchToolsHash(baseURL)
if err != nil {
return err
}
if cache != nil && hashResult.Hash == cache.Hash {
// Tools unchanged — just bump the check timestamp
cache.LastCheckedAt = time.Now()
return saveToolsCache(cache)
}
// Hash changed or no cache — do a full refresh
_, err = fetchAndCacheTools(baseURL, token, false)
return err
}
// toolNameToCommandName converts an MCP tool name to a CLI command name.
// It strips the platform prefix and converts underscores to hyphens.
//
// Examples:
//
// get_google_ads_campaigns → get-campaigns
// execute_google_ads_gaql_query → execute-gaql-query
// get_campaigns → get-campaigns
func toolNameToCommandName(toolName, prefix string) string {
name := toolName
if prefix != "" {
name = strings.Replace(name, prefix, "", 1)
}
return strings.ReplaceAll(name, "_", "-")
}
// InputSchema is a simplified JSON Schema for MCP tool inputs.
type InputSchema struct {
Type string `json:"type"`
Properties map[string]PropertySchema `json:"properties"`
Required []string `json:"required"`
}
// PropertySchema describes a single property in the input schema.
type PropertySchema struct {
Type interface{} `json:"type"` // string or []string (e.g. ["string", "null"])
Description string `json:"description"`
Enum []string `json:"enum"`
}
// resolveType returns the primary type as a string, handling union types like ["string", "null"].
func (p PropertySchema) resolveType() string {
switch t := p.Type.(type) {
case string:
return t
case []interface{}:
for _, v := range t {
if s, ok := v.(string); ok && s != "null" {
return s
}
}
}
return "string"
}
func buildToolCommand(server ServerConfig, tool client.ToolDefinition) *cobra.Command {
cmdName := toolNameToCommandName(tool.Name, server.ToolPrefix)
var schema InputSchema
if len(tool.InputSchema) > 0 {
_ = json.Unmarshal(tool.InputSchema, &schema)
}
requiredSet := make(map[string]bool)
for _, r := range schema.Required {
requiredSet[r] = true
}
// Flag storage — one map per type we support
stringFlags := make(map[string]*string)
boolFlags := make(map[string]*bool)
cmd := &cobra.Command{
Use: cmdName,
Short: truncate(tool.Description, 80),
Long: tool.Description,
RunE: func(cmd *cobra.Command, args []string) error {
return runDynamicTool(server, tool.Name, schema, stringFlags, boolFlags)
},
}
for propName, prop := range schema.Properties {
flagName := strings.ReplaceAll(propName, "_", "-")
desc := prop.Description
if len(prop.Enum) > 0 {
desc += " (options: " + strings.Join(prop.Enum, ", ") + ")"
}
switch prop.resolveType() {
case "boolean":
boolFlags[propName] = cmd.Flags().Bool(flagName, false, desc)
default:
// string, number, integer, array, object — all accepted as strings
stringFlags[propName] = cmd.Flags().String(flagName, "", desc)
}
if requiredSet[propName] {
cmd.MarkFlagRequired(flagName)
}
}
return cmd
}
func runDynamicTool(server ServerConfig, toolName string, schema InputSchema, stringFlags map[string]*string, boolFlags map[string]*bool) error {
token, err := getToken()
if err != nil {
return err
}
c := client.New(apiURL, token)
fmt.Fprintf(os.Stderr, "Connecting to %s/%s...\n", apiURL, server.Path)
if err := c.Initialize(server.Path); err != nil {
return fmt.Errorf("handshake failed: %w", err)
}
// Build arguments from flag values, coercing types based on schema
toolArgs := make(map[string]interface{})
for propName, val := range stringFlags {
if val == nil || *val == "" {
continue
}
prop := schema.Properties[propName]
switch prop.resolveType() {
case "integer":
if n, err := strconv.Atoi(*val); err == nil {
toolArgs[propName] = n
} else {
toolArgs[propName] = *val
}
case "number":
if n, err := strconv.ParseFloat(*val, 64); err == nil {
toolArgs[propName] = n
} else {
toolArgs[propName] = *val
}
case "object", "array":
var parsed interface{}
if err := json.Unmarshal([]byte(*val), &parsed); err == nil {
toolArgs[propName] = parsed
} else {
toolArgs[propName] = *val
}
default:
toolArgs[propName] = *val
}
}
for propName, val := range boolFlags {
if val != nil && *val {
toolArgs[propName] = true
}
}
fmt.Fprintf(os.Stderr, "Calling %s...\n", toolName)
result, err := c.CallTool(server.Path, toolName, toolArgs)
if err != nil {
return fmt.Errorf("tool call failed: %w", err)
}
return printToolResult(result)
}
// printToolResult extracts text content from the MCP tools/call response and prints it.
func printToolResult(raw json.RawMessage) error {
var toolResult struct {
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
IsError bool `json:"isError"`
}
if err := json.Unmarshal(raw, &toolResult); err == nil && len(toolResult.Content) > 0 {
for _, item := range toolResult.Content {
if item.Type != "text" {
continue
}
// Try to pretty-print as JSON; fall back to plain text
var parsed interface{}
if err := json.Unmarshal([]byte(item.Text), &parsed); err == nil {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(parsed)
} else {
fmt.Println(item.Text)
}
}
if toolResult.IsError {
return fmt.Errorf("tool returned an error")
}
return nil
}
// Fallback: print raw result
var parsed interface{}
if err := json.Unmarshal(raw, &parsed); err == nil {
return printJSON(parsed)
}
fmt.Println(string(raw))
return nil
}