-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathtoolsets.go
More file actions
194 lines (170 loc) · 4.05 KB
/
toolsets.go
File metadata and controls
194 lines (170 loc) · 4.05 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
package toolsets
import (
"fmt"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
type ToolsetDoesNotExistError struct {
Name string
}
func (e *ToolsetDoesNotExistError) Error() string {
return fmt.Sprintf("toolset %s does not exist", e.Name)
}
func (e *ToolsetDoesNotExistError) Is(target error) bool {
if target == nil {
return false
}
if _, ok := target.(*ToolsetDoesNotExistError); ok {
return true
}
return false
}
func NewToolsetDoesNotExistError(name string) *ToolsetDoesNotExistError {
return &ToolsetDoesNotExistError{Name: name}
}
func NewServerTool(tool mcp.Tool, handler server.ToolHandlerFunc) server.ServerTool {
return server.ServerTool{Tool: tool, Handler: handler}
}
type Toolset struct {
Name string
Description string
Enabled bool
readOnly bool
writeTools []server.ServerTool
readTools []server.ServerTool
}
func (t *Toolset) GetActiveTools() []server.ServerTool {
if t.Enabled {
if t.readOnly {
return t.readTools
}
return append(t.readTools, t.writeTools...)
}
return nil
}
func (t *Toolset) GetAvailableTools() []server.ServerTool {
if t.readOnly {
return t.readTools
}
return append(t.readTools, t.writeTools...)
}
func (t *Toolset) RegisterTools(s *server.MCPServer) {
if !t.Enabled {
return
}
for _, tool := range t.readTools {
s.AddTool(tool.Tool, tool.Handler)
}
if !t.readOnly {
for _, tool := range t.writeTools {
s.AddTool(tool.Tool, tool.Handler)
}
}
}
func (t *Toolset) SetReadOnly() {
// Set the toolset to read-only
t.readOnly = true
}
func (t *Toolset) AddWriteTools(tools ...server.ServerTool) *Toolset {
// Silently ignore if the toolset is read-only to avoid any breach of that contract
for _, tool := range tools {
if *tool.Tool.Annotations.ReadOnlyHint {
panic(fmt.Sprintf("tool (%s) is incorrectly annotated as read-only", tool.Tool.Name))
}
}
if !t.readOnly {
t.writeTools = append(t.writeTools, tools...)
}
return t
}
func (t *Toolset) AddReadTools(tools ...server.ServerTool) *Toolset {
for _, tool := range tools {
if !*tool.Tool.Annotations.ReadOnlyHint {
panic(fmt.Sprintf("tool (%s) must be annotated as read-only", tool.Tool.Name))
}
}
t.readTools = append(t.readTools, tools...)
return t
}
type ToolsetGroup struct {
Toolsets map[string]*Toolset
everythingOn bool
readOnly bool
}
func NewToolsetGroup(readOnly bool) *ToolsetGroup {
return &ToolsetGroup{
Toolsets: make(map[string]*Toolset),
everythingOn: false,
readOnly: readOnly,
}
}
func (tg *ToolsetGroup) AddToolset(ts *Toolset) {
if tg.readOnly {
ts.SetReadOnly()
}
tg.Toolsets[ts.Name] = ts
}
func NewToolset(name string, description string) *Toolset {
return &Toolset{
Name: name,
Description: description,
Enabled: false,
readOnly: false,
}
}
func (tg *ToolsetGroup) IsEnabled(name string) bool {
// If everythingOn is true, all features are enabled
if tg.everythingOn {
return true
}
feature, exists := tg.Toolsets[name]
if !exists {
return false
}
return feature.Enabled
}
func (tg *ToolsetGroup) EnableToolsets(names []string) error {
// Special case for "all"
for _, name := range names {
if name == "all" {
tg.everythingOn = true
break
}
err := tg.EnableToolset(name)
if err != nil {
return err
}
}
// Do this after to ensure all toolsets are enabled if "all" is present anywhere in list
if tg.everythingOn {
for name := range tg.Toolsets {
err := tg.EnableToolset(name)
if err != nil {
return err
}
}
return nil
}
return nil
}
func (tg *ToolsetGroup) EnableToolset(name string) error {
toolset, exists := tg.Toolsets[name]
if !exists {
return NewToolsetDoesNotExistError(name)
}
toolset.Enabled = true
tg.Toolsets[name] = toolset
return nil
}
func (tg *ToolsetGroup) RegisterTools(s *server.MCPServer) {
for _, toolset := range tg.Toolsets {
toolset.RegisterTools(s)
}
}
func (tg *ToolsetGroup) GetToolset(name string) (*Toolset, error) {
toolset, exists := tg.Toolsets[name]
if !exists {
return nil, NewToolsetDoesNotExistError(name)
}
return toolset, nil
}