-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.go
More file actions
278 lines (250 loc) · 8.2 KB
/
args.go
File metadata and controls
278 lines (250 loc) · 8.2 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
package cobrax
import (
"fmt"
"regexp"
"strings"
"github.com/google/jsonschema-go/jsonschema"
"github.com/spf13/cobra"
)
// Cobra command annotation keys for positional argument metadata.
// Set these in cmd.Annotations to provide semantic descriptions for each
// positional argument, which are then surfaced in the MCP Tool JSON Schema
// so that LLMs know exactly what to supply for each argument position.
//
// Convention:
//
// AnnotationArgPrefix + "0" = description for the first positional arg
// AnnotationArgPrefix + "1" = description for the second positional arg
// ... and so on
//
// Example:
//
// cmd.Annotations = map[string]string{
// cobrax.AnnotationArgPrefix + "0": "The on-call module name (e.g. 'bke', 'kafka')",
// cobrax.AnnotationArgPrefix + "1": "Brief title describing the incident",
// }
const AnnotationArgPrefix = "cobrax.arg."
// ArgSpec describes a single positional argument parsed from cmd.Use.
type ArgSpec struct {
// Name is the canonical field name used in the JSON Schema (e.g. "module", "title").
// Derived from the bracket-stripped token in cmd.Use (e.g. "<module>" → "module").
Name string
// Description is a human-readable explanation of what this argument means.
// Populated from the AnnotationArgPrefix+index annotation when available,
// or synthesised from the usage pattern token otherwise.
Description string
// Required indicates whether this argument must be present.
// true → angle-bracket syntax e.g. <name>
// false → square-bracket syntax e.g. [name]
Required bool
// Variadic indicates this argument accepts one or more values.
// true when the token ends with "..." (e.g. [targets...] or <files...>).
Variadic bool
// Index is the zero-based position of this argument in the CLI invocation.
Index int
}
// argTokenRe matches a single positional-argument token in cmd.Use.
// It captures:
//
// group 1: bracket type ("<" required, "[" optional)
// group 2: argument name (stripped of trailing "...")
// group 3: variadic marker "..." (may be empty)
//
// Examples that match:
//
// <module> → required, name=module
// [title] → optional, name=title
// [targets...] → optional, variadic, name=targets
// <files...> → required, variadic, name=files
var argTokenRe = regexp.MustCompile(`([<\[])([^>\]\s.]+)(\.{3})?[>\]]`)
// parseArgSpecs analyses cmd.Use and cmd.Annotations to produce an ordered
// slice of ArgSpec values — one per distinct positional argument token.
//
// Parsing rules for cmd.Use tokens:
// - "<name>" → required, single-value
// - "[name]" → optional, single-value
// - "<name...>" → required, variadic
// - "[name...]" → optional, variadic
// - "[flags]" is silently skipped (cobra's standard sentinel)
//
// Descriptions are sourced (in priority order) from:
// 1. cmd.Annotations["cobrax.arg.<index>"] (highest priority, explicit)
// 2. Synthesised from the token and its required/variadic attributes
//
// If cmd.Use contains no argument tokens the returned slice is nil.
func parseArgSpecs(cmd *cobra.Command) []ArgSpec {
// Strip the command name (first word) from cmd.Use.
use := cmd.Use
if spaceIdx := strings.IndexByte(use, ' '); spaceIdx != -1 {
use = use[spaceIdx+1:]
} else {
// No arguments at all.
return nil
}
// Remove the "[flags]" sentinel that cobra appends to some Use strings.
use = strings.ReplaceAll(use, "[flags]", "")
use = strings.TrimSpace(use)
if use == "" {
return nil
}
matches := argTokenRe.FindAllStringSubmatch(use, -1)
if len(matches) == 0 {
return nil
}
specs := make([]ArgSpec, 0, len(matches))
for i, m := range matches {
bracketType := m[1] // "<" or "["
rawName := m[2] // argument name without brackets/dots
variadicMark := m[3]
required := bracketType == "<"
variadic := variadicMark == "..."
// Normalise name: lowercase, replace non-alphanum with underscore.
name := normaliseName(rawName)
// Build a default description from the usage token.
tokenRepr := reconstructToken(bracketType, rawName, variadic)
var desc string
if variadic {
if required {
desc = fmt.Sprintf("One or more %s values (required). Usage: %s", name, tokenRepr)
} else {
desc = fmt.Sprintf("Zero or more %s values (optional). Usage: %s", name, tokenRepr)
}
} else {
if required {
desc = fmt.Sprintf("The %s value (required). Usage: %s", name, tokenRepr)
} else {
desc = fmt.Sprintf("The %s value (optional). Usage: %s", name, tokenRepr)
}
}
// Override description with annotation if provided.
annotKey := fmt.Sprintf("%s%d", AnnotationArgPrefix, i)
if annotDesc, ok := cmd.Annotations[annotKey]; ok && annotDesc != "" {
desc = annotDesc
}
specs = append(specs, ArgSpec{
Name: name,
Description: desc,
Required: required,
Variadic: variadic,
Index: i,
})
}
return specs
}
// normaliseName converts a raw argument name from cmd.Use into a safe JSON
// Schema property name: lowercase letters, digits, and underscores only.
func normaliseName(raw string) string {
raw = strings.ToLower(raw)
var b strings.Builder
for _, r := range raw {
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '_' {
b.WriteRune(r)
} else {
b.WriteRune('_')
}
}
return b.String()
}
// reconstructToken rebuilds a canonical usage token for display purposes.
func reconstructToken(bracketType, name string, variadic bool) string {
open := bracketType
close := ">"
if bracketType == "[" {
close = "]"
}
dots := ""
if variadic {
dots = "..."
}
return open + name + dots + close
}
// buildArgsSchema constructs the JSON Schema fragment that describes all
// positional arguments for a command.
//
// Each positional argument becomes a named property of an object schema:
//
// {
// "type": "object",
// "properties": {
// "module": { "type": "string", "description": "..." },
// "title": { "type": "string", "description": "..." }
// },
// "required": ["module"]
// }
//
// Variadic arguments are represented as string arrays:
//
// "targets": { "type": "array", "items": { "type": "string" }, "description": "..." }
//
// The resulting object replaces the top-level "args" property of the ToolInput
// JSON Schema. Callers must not call this function with an empty specs slice.
func buildArgsSchema(specs []ArgSpec) *jsonschema.Schema {
props := make(map[string]*jsonschema.Schema, len(specs))
var required []string
for _, spec := range specs {
var propSchema *jsonschema.Schema
if spec.Variadic {
propSchema = &jsonschema.Schema{
Type: "array",
Description: spec.Description,
Items: &jsonschema.Schema{Type: "string"},
}
} else {
propSchema = &jsonschema.Schema{
Type: "string",
Description: spec.Description,
}
}
props[spec.Name] = propSchema
if spec.Required {
required = append(required, spec.Name)
}
}
s := &jsonschema.Schema{
Type: "object",
Description: "Positional command line arguments",
Properties: props,
}
if len(required) > 0 {
s.Required = required
}
// Disallow unexpected extra fields.
s.AdditionalProperties = &jsonschema.Schema{Not: &jsonschema.Schema{}}
return s
}
// collectPositionalArgs reassembles the ordered positional-argument slice
// from a structured PositionalArgs map, using specs to determine the correct
// order and handle variadic expansion.
//
// Each non-variadic spec contributes at most one string value; each variadic
// spec contributes zero or more strings. Arguments for specs not present in
// the map are simply omitted (optional args).
func collectPositionalArgs(specs []ArgSpec, positional map[string]any) []string {
if len(specs) == 0 || len(positional) == 0 {
return nil
}
var out []string
for _, spec := range specs {
val, ok := positional[spec.Name]
if !ok || val == nil {
continue
}
if spec.Variadic {
// Accept []any or []string from JSON decoding.
switch v := val.(type) {
case []any:
for _, item := range v {
out = append(out, fmt.Sprintf("%v", item))
}
case []string:
out = append(out, v...)
default:
// Single value provided for variadic — treat as one element.
out = append(out, fmt.Sprintf("%v", val))
}
} else {
out = append(out, fmt.Sprintf("%v", val))
}
}
return out
}