-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations.go
More file actions
102 lines (86 loc) · 2.77 KB
/
annotations.go
File metadata and controls
102 lines (86 loc) · 2.77 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
package cobrax
import (
"log/slog"
"strconv"
"github.com/mark3labs/mcp-go/mcp"
"github.com/spf13/cobra"
)
// Cobra command annotation keys for MCP tool annotations.
// Set these in cmd.Annotations to populate mcp.ToolAnnotation on the generated tool.
//
// Boolean values are parsed with strconv.ParseBool (accepts "1", "t", "true", "0", "f", "false", etc.).
//
// Example:
//
// cmd.Annotations = map[string]string{
// cobrax.AnnotationReadOnly: "true",
// cobrax.AnnotationTitle: "List files",
// }
const (
// AnnotationTitle sets the human-readable title for the tool.
AnnotationTitle = "title"
// AnnotationReadOnly hints that the tool does not modify its environment.
AnnotationReadOnly = "readOnlyHint"
// AnnotationDestructive hints that the tool may perform destructive updates.
// Only meaningful when ReadOnlyHint is false.
AnnotationDestructive = "destructiveHint"
// AnnotationIdempotent hints that calling the tool repeatedly with the same
// arguments has no additional effect. Only meaningful when ReadOnlyHint is false.
AnnotationIdempotent = "idempotentHint"
// AnnotationOpenWorld hints that the tool may interact with external entities
// outside its closed domain.
AnnotationOpenWorld = "openWorldHint"
)
// toolAnnotations reads MCP annotation keys from cmd.Annotations and
// returns a populated *mcp.ToolAnnotation, or nil if no MCP annotations are found.
func toolAnnotations(cmd *cobra.Command) *mcp.ToolAnnotation {
if len(cmd.Annotations) == 0 {
return nil
}
var annotations mcp.ToolAnnotation
found := false
if v, ok := cmd.Annotations[AnnotationTitle]; ok {
annotations.Title = v
found = true
}
if v, ok := cmd.Annotations[AnnotationReadOnly]; ok {
b, err := strconv.ParseBool(v)
if err != nil {
slog.Warn("invalid bool value for annotation, skipping", "key", AnnotationReadOnly, "value", v)
} else {
annotations.ReadOnlyHint = &b
found = true
}
}
if v, ok := cmd.Annotations[AnnotationDestructive]; ok {
b, err := strconv.ParseBool(v)
if err != nil {
slog.Warn("invalid bool value for annotation, skipping", "key", AnnotationDestructive, "value", v)
} else {
annotations.DestructiveHint = &b
found = true
}
}
if v, ok := cmd.Annotations[AnnotationIdempotent]; ok {
b, err := strconv.ParseBool(v)
if err != nil {
slog.Warn("invalid bool value for annotation, skipping", "key", AnnotationIdempotent, "value", v)
} else {
annotations.IdempotentHint = &b
found = true
}
}
if v, ok := cmd.Annotations[AnnotationOpenWorld]; ok {
b, err := strconv.ParseBool(v)
if err != nil {
slog.Warn("invalid bool value for annotation, skipping", "key", AnnotationOpenWorld, "value", v)
} else {
annotations.OpenWorldHint = &b
found = true
}
}
if !found {
return nil
}
return &annotations
}