Repository navigation
Expand file tree
/
Copy pathoptions.go
More file actions
627 lines (546 loc) · 21.8 KB
/
Copy pathoptions.go
File metadata and controls
627 lines (546 loc) · 21.8 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"fmt"
"net/http"
"slices"
"golang.org/x/oauth2"
)
// ----- Client Options -----
// ClientOption configures a ToolboxClient at creation time.
type ClientOption func(*ToolboxClient) error
// WithSupportedProtocols configures a user-defined list of supported protocols for auto-negotiation.
// The provided protocols are prioritized from newest to oldest version.
func WithSupportedProtocols(protocols []Protocol) ClientOption {
return func(tc *ToolboxClient) error {
if len(protocols) == 0 {
return fmt.Errorf("WithSupportedProtocols: protocols slice cannot be empty")
}
if tc.protocolSet {
return fmt.Errorf("protocol is already set and cannot be overridden")
}
allVersions := GetSupportedMcpVersions()
for _, p := range protocols {
if !slices.Contains(allVersions, string(p)) {
return fmt.Errorf("invalid protocol version '%s'. Must be one of: %v", p, allVersions)
}
}
var sorted []Protocol
for _, verStr := range allVersions {
p := Protocol(verStr)
if slices.Contains(protocols, p) {
sorted = append(sorted, p)
}
}
tc.supportedProtocols = sorted
tc.protocol = sorted[0]
return nil
}
}
// Constructor for a newToolConfig which initializes the maps for auth token sources, bound parameters, and secure parameters
func newToolConfig() *ToolConfig {
return &ToolConfig{
AuthTokenSources: make(map[string]oauth2.TokenSource),
BoundParams: make(map[string]any),
SecureParams: make(map[string]any),
}
}
// WithClientName sets the client name used in the MCP protocol handshake.
// Defaults to "toolbox-core-go" if not set.
func WithClientName(name string) ClientOption {
return func(tc *ToolboxClient) error {
tc.clientName = name
return nil
}
}
// WithClientVersion sets the client version used in the MCP protocol handshake.
// Defaults to the core SDK version if not set.
func WithClientVersion(version string) ClientOption {
return func(tc *ToolboxClient) error {
tc.clientVersion = version
return nil
}
}
// WithProtocol provides a the underlying transport protocol to the ToolboxClient..
func WithProtocol(p Protocol) ClientOption {
return func(tc *ToolboxClient) error {
if tc.protocolSet {
return fmt.Errorf("protocol is already set and cannot be overridden")
}
tc.protocol = p
tc.protocolSet = true
return nil
}
}
// WithHTTPClient provides a custom http.Client to the ToolboxClient.
func WithHTTPClient(client *http.Client) ClientOption {
return func(tc *ToolboxClient) error {
if client == nil {
return fmt.Errorf("WithHTTPClient: provided http.Client cannot be nil")
}
tc.httpClient = client
return nil
}
}
// WithClientHeaderString adds a static string value as a client-wide HTTP header.
func WithClientHeaderString(headerName string, value string) ClientOption {
return func(tc *ToolboxClient) error {
if _, exists := tc.clientHeaderSources[headerName]; exists {
return fmt.Errorf("client header '%s' is already set and cannot be overridden", headerName)
}
staticToken := &oauth2.Token{AccessToken: value}
tc.clientHeaderSources[headerName] = oauth2.StaticTokenSource(staticToken)
return nil
}
}
// WithClientHeaderTokenSource adds a dynamic client-wide HTTP header from a TokenSource.
func WithClientHeaderTokenSource(headerName string, value oauth2.TokenSource) ClientOption {
return func(tc *ToolboxClient) error {
if _, exists := tc.clientHeaderSources[headerName]; exists {
return fmt.Errorf("client header '%s' is already set and cannot be overridden", headerName)
}
if value == nil {
return fmt.Errorf("WithClientHeaderTokenSource: provided oauth2.TokenSource for header '%s' cannot be nil", headerName)
}
tc.clientHeaderSources[headerName] = value
return nil
}
}
// WithDefaultToolOptions provides default Options that will be applied to every tool
// loaded by this client.
func WithDefaultToolOptions(opts ...ToolOption) ClientOption {
return func(tc *ToolboxClient) error {
if tc.defaultOptionsSet {
return fmt.Errorf("default tool options have already been set and cannot be modified")
}
tc.defaultToolOptions = append(tc.defaultToolOptions, opts...)
tc.defaultOptionsSet = true
return nil
}
}
// ----- Tool Options -----
// ToolConfig holds all configurable aspects for creating or deriving a tool.
type ToolConfig struct {
AuthTokenSources map[string]oauth2.TokenSource
BoundParams map[string]any
SecureParams map[string]any
Strict bool
strictSet bool
}
// ToolOption defines a single, universal type for a functional option that configures a tool.
type ToolOption func(*ToolConfig) error
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
type Float interface {
~float32 | ~float64
}
// WithStrict provides an option to enable strict validation for LoadToolset.
func WithStrict(strict bool) ToolOption {
return func(c *ToolConfig) error {
if c.strictSet {
return fmt.Errorf("strict mode is already set and cannot be overridden")
}
c.Strict = strict
c.strictSet = true // Set the flag after successful assignment
return nil
}
}
// WithAuthTokenSource provides an authentication token from a standard TokenSource.
func WithAuthTokenSource(authSourceName string, idToken oauth2.TokenSource) ToolOption {
return func(c *ToolConfig) error {
if _, exists := c.AuthTokenSources[authSourceName]; exists {
return fmt.Errorf("authentication source '%s' is already set and cannot be overridden", authSourceName)
}
c.AuthTokenSources[authSourceName] = idToken
return nil
}
}
// WithAuthTokenString provides a static string authentication token.
func WithAuthTokenString(authSourceName string, idToken string) ToolOption {
return func(c *ToolConfig) error {
if _, exists := c.AuthTokenSources[authSourceName]; exists {
return fmt.Errorf("authentication source '%s' is already set and cannot be overridden", authSourceName)
}
staticToken := &oauth2.Token{AccessToken: idToken}
c.AuthTokenSources[authSourceName] = oauth2.StaticTokenSource(staticToken)
return nil
}
}
// Helper function
func createBoundParamToolOption(name string, value any) ToolOption {
return func(c *ToolConfig) error {
if _, exists := c.BoundParams[name]; exists {
return fmt.Errorf("duplicate parameter binding: parameter '%s' is already set", name)
}
if _, exists := c.SecureParams[name]; exists {
return fmt.Errorf("duplicate parameter binding: parameter '%s' is already set as a secure parameter", name)
}
c.BoundParams[name] = value
return nil
}
}
// Helper function for secure parameters
func createBoundSecureParamToolOption(name string, value any) ToolOption {
return func(c *ToolConfig) error {
if c.SecureParams == nil {
c.SecureParams = make(map[string]any)
}
if _, exists := c.SecureParams[name]; exists {
return fmt.Errorf("duplicate secure parameter binding: parameter '%s' is already set", name)
}
if _, exists := c.BoundParams[name]; exists {
return fmt.Errorf("duplicate parameter binding: parameter '%s' is already set as a regular bound parameter", name)
}
c.SecureParams[name] = value
return nil
}
}
// WithBindParamString binds a static string value to a parameter.
func WithBindParamString(name string, value string) ToolOption {
return createBoundParamToolOption(name, value)
}
// WithBindParamStringFunc binds a function that returns a string to a parameter.
func WithBindParamStringFunc(name string, fn func() (string, error)) ToolOption {
return createBoundParamToolOption(name, fn)
}
// WithBindParamInt binds a static integer value to a parameter.
func WithBindParamInt[T Integer](name string, value T) ToolOption {
return createBoundParamToolOption(name, int(value))
}
// WithBindParamIntFunc binds a function that returns an integer to a parameter.
func WithBindParamIntFunc[T Integer](name string, fn func() (T, error)) ToolOption {
return createBoundParamToolOption(name, func() (int, error) {
v, err := fn()
return int(v), err
})
}
// WithBindParamFloat binds a static float value to a parameter.
func WithBindParamFloat[T Float](name string, value T) ToolOption {
return createBoundParamToolOption(name, float64(value))
}
// WithBindParamFloatFunc binds a function that returns a float to a parameter.
func WithBindParamFloatFunc[T Float](name string, fn func() (T, error)) ToolOption {
return createBoundParamToolOption(name, func() (float64, error) {
v, err := fn()
return float64(v), err
})
}
// WithBindParamBool binds a static boolean value to a parameter.
func WithBindParamBool(name string, value bool) ToolOption {
return createBoundParamToolOption(name, value)
}
// WithBindParamBoolFunc binds a function that returns a boolean to a parameter.
func WithBindParamBoolFunc(name string, fn func() (bool, error)) ToolOption {
return createBoundParamToolOption(name, fn)
}
// --- Array Bindings ---
// WithBindParamStringArray binds a static slice of strings to a parameter.
func WithBindParamStringArray(name string, value []string) ToolOption {
return createBoundParamToolOption(name, value)
}
// WithBindParamStringArrayFunc binds a function that returns a slice of strings.
func WithBindParamStringArrayFunc(name string, fn func() ([]string, error)) ToolOption {
return createBoundParamToolOption(name, fn)
}
// WithBindParamIntArray binds a static slice of integers to a parameter.
func WithBindParamIntArray[T Integer](name string, value []T) ToolOption {
normalized := make([]int, len(value))
for i, v := range value {
normalized[i] = int(v)
}
return createBoundParamToolOption(name, normalized)
}
// WithBindParamIntArrayFunc binds a function that returns a slice of integers.
func WithBindParamIntArrayFunc[T Integer](name string, fn func() ([]T, error)) ToolOption {
return createBoundParamToolOption(name, func() ([]int, error) {
val, err := fn()
if err != nil {
return nil, err
}
normalized := make([]int, len(val))
for i, v := range val {
normalized[i] = int(v)
}
return normalized, nil
})
}
// WithBindParamFloatArray binds a static slice of floats to a parameter.
func WithBindParamFloatArray[T Float](name string, value []T) ToolOption {
normalized := make([]float64, len(value))
for i, v := range value {
normalized[i] = float64(v)
}
return createBoundParamToolOption(name, normalized)
}
// WithBindParamFloatArrayFunc binds a function that returns a slice of floats.
func WithBindParamFloatArrayFunc[T Float](name string, fn func() ([]T, error)) ToolOption {
return createBoundParamToolOption(name, func() ([]float64, error) {
val, err := fn()
if err != nil {
return nil, err
}
normalized := make([]float64, len(val))
for i, v := range val {
normalized[i] = float64(v)
}
return normalized, nil
})
}
// WithBindParamBoolArray binds a static slice of booleans to a parameter.
func WithBindParamBoolArray(name string, value []bool) ToolOption {
return createBoundParamToolOption(name, value)
}
// WithBindParamBoolArrayFunc binds a function that returns a slice of booleans.
func WithBindParamBoolArrayFunc(name string, fn func() ([]bool, error)) ToolOption {
return createBoundParamToolOption(name, fn)
}
// --- Map Bindings ---
// WithBindParamStringMap binds a static map of strings to a parameter.
func WithBindParamStringMap(name string, value map[string]string) ToolOption {
return createBoundParamToolOption(name, value)
}
// WithBindParamStringMapFunc binds a function that returns a map of strings to a parameter.
func WithBindParamStringMapFunc(name string, fn func() (map[string]string, error)) ToolOption {
return createBoundParamToolOption(name, fn)
}
// WithBindParamIntMap binds a static map of integers to a parameter.
func WithBindParamIntMap[T Integer](name string, value map[string]T) ToolOption {
normalized := make(map[string]int, len(value))
for k, v := range value {
normalized[k] = int(v)
}
return createBoundParamToolOption(name, normalized)
}
// WithBindParamIntMapFunc binds a function that returns a map of integers to a parameter.
func WithBindParamIntMapFunc[T Integer](name string, fn func() (map[string]T, error)) ToolOption {
return createBoundParamToolOption(name, func() (map[string]int, error) {
val, err := fn()
if err != nil {
return nil, err
}
normalized := make(map[string]int, len(val))
for k, v := range val {
normalized[k] = int(v)
}
return normalized, nil
})
}
// WithBindParamFloatMap binds a static map of floats to a parameter.
func WithBindParamFloatMap[T Float](name string, value map[string]T) ToolOption {
normalized := make(map[string]float64, len(value))
for k, v := range value {
normalized[k] = float64(v)
}
return createBoundParamToolOption(name, normalized)
}
// WithBindParamFloatMapFunc binds a function that returns a map of floats to a parameter.
func WithBindParamFloatMapFunc[T Float](name string, fn func() (map[string]T, error)) ToolOption {
return createBoundParamToolOption(name, func() (map[string]float64, error) {
val, err := fn()
if err != nil {
return nil, err
}
normalized := make(map[string]float64, len(val))
for k, v := range val {
normalized[k] = float64(v)
}
return normalized, nil
})
}
// WithBindParamBoolMap binds a static map of booleans to a parameter.
func WithBindParamBoolMap(name string, value map[string]bool) ToolOption {
return createBoundParamToolOption(name, value)
}
// WithBindParamBoolMapFunc binds a function that returns a map of booleans to a parameter.
func WithBindParamBoolMapFunc(name string, fn func() (map[string]bool, error)) ToolOption {
return createBoundParamToolOption(name, fn)
}
// WithBindParamAnyMap binds a generic map to a parameter.
func WithBindParamAnyMap(name string, value map[string]any) ToolOption {
return createBoundParamToolOption(name, value)
}
// WithBindParamAnyMapFunc binds a function that returns a generic map to a parameter.
func WithBindParamAnyMapFunc(name string, fn func() (map[string]any, error)) ToolOption {
return createBoundParamToolOption(name, fn)
}
// ----- Secure Parameter Tool Options -----
// WithBindSecureParamString binds a static string value to a secure parameter.
func WithBindSecureParamString(name string, value string) ToolOption {
return createBoundSecureParamToolOption(name, value)
}
// WithBindSecureParamStringFunc binds a function that returns a string to a secure parameter.
func WithBindSecureParamStringFunc(name string, fn func() (string, error)) ToolOption {
return createBoundSecureParamToolOption(name, fn)
}
// WithBindSecureParamInt binds a static integer value to a secure parameter.
func WithBindSecureParamInt[T Integer](name string, value T) ToolOption {
return createBoundSecureParamToolOption(name, int(value))
}
// WithBindSecureParamIntFunc binds a function that returns an integer to a secure parameter.
func WithBindSecureParamIntFunc[T Integer](name string, fn func() (T, error)) ToolOption {
return createBoundSecureParamToolOption(name, func() (int, error) {
v, err := fn()
return int(v), err
})
}
// WithBindSecureParamFloat binds a static float value to a secure parameter.
func WithBindSecureParamFloat[T Float](name string, value T) ToolOption {
return createBoundSecureParamToolOption(name, float64(value))
}
// WithBindSecureParamFloatFunc binds a function that returns a float to a secure parameter.
func WithBindSecureParamFloatFunc[T Float](name string, fn func() (T, error)) ToolOption {
return createBoundSecureParamToolOption(name, func() (float64, error) {
v, err := fn()
return float64(v), err
})
}
// WithBindSecureParamBool binds a static boolean value to a secure parameter.
func WithBindSecureParamBool(name string, value bool) ToolOption {
return createBoundSecureParamToolOption(name, value)
}
// WithBindSecureParamBoolFunc binds a function that returns a boolean to a secure parameter.
func WithBindSecureParamBoolFunc(name string, fn func() (bool, error)) ToolOption {
return createBoundSecureParamToolOption(name, fn)
}
// WithBindSecureParamStringArray binds a static slice of strings to a secure parameter.
func WithBindSecureParamStringArray(name string, value []string) ToolOption {
return createBoundSecureParamToolOption(name, value)
}
// WithBindSecureParamStringArrayFunc binds a function that returns a slice of strings to a secure parameter.
func WithBindSecureParamStringArrayFunc(name string, fn func() ([]string, error)) ToolOption {
return createBoundSecureParamToolOption(name, fn)
}
// WithBindSecureParamIntArray binds a static slice of integers to a secure parameter.
func WithBindSecureParamIntArray[T Integer](name string, value []T) ToolOption {
normalized := make([]int, len(value))
for i, v := range value {
normalized[i] = int(v)
}
return createBoundSecureParamToolOption(name, normalized)
}
// WithBindSecureParamIntArrayFunc binds a function that returns a slice of integers to a secure parameter.
func WithBindSecureParamIntArrayFunc[T Integer](name string, fn func() ([]T, error)) ToolOption {
return createBoundSecureParamToolOption(name, func() ([]int, error) {
val, err := fn()
if err != nil {
return nil, err
}
normalized := make([]int, len(val))
for i, v := range val {
normalized[i] = int(v)
}
return normalized, nil
})
}
// WithBindSecureParamFloatArray binds a static slice of floats to a secure parameter.
func WithBindSecureParamFloatArray[T Float](name string, value []T) ToolOption {
normalized := make([]float64, len(value))
for i, v := range value {
normalized[i] = float64(v)
}
return createBoundSecureParamToolOption(name, normalized)
}
// WithBindSecureParamFloatArrayFunc binds a function that returns a slice of floats to a secure parameter.
func WithBindSecureParamFloatArrayFunc[T Float](name string, fn func() ([]T, error)) ToolOption {
return createBoundSecureParamToolOption(name, func() ([]float64, error) {
val, err := fn()
if err != nil {
return nil, err
}
normalized := make([]float64, len(val))
for i, v := range val {
normalized[i] = float64(v)
}
return normalized, nil
})
}
// WithBindSecureParamBoolArray binds a static slice of booleans to a secure parameter.
func WithBindSecureParamBoolArray(name string, value []bool) ToolOption {
return createBoundSecureParamToolOption(name, value)
}
// WithBindSecureParamBoolArrayFunc binds a function that returns a slice of booleans to a secure parameter.
func WithBindSecureParamBoolArrayFunc(name string, fn func() ([]bool, error)) ToolOption {
return createBoundSecureParamToolOption(name, fn)
}
// WithBindSecureParamStringMap binds a static map of strings to a secure parameter.
func WithBindSecureParamStringMap(name string, value map[string]string) ToolOption {
return createBoundSecureParamToolOption(name, value)
}
// WithBindSecureParamStringMapFunc binds a function that returns a map of strings to a secure parameter.
func WithBindSecureParamStringMapFunc(name string, fn func() (map[string]string, error)) ToolOption {
return createBoundSecureParamToolOption(name, fn)
}
// WithBindSecureParamIntMap binds a static map of integers to a secure parameter.
func WithBindSecureParamIntMap[T Integer](name string, value map[string]T) ToolOption {
normalized := make(map[string]int, len(value))
for k, v := range value {
normalized[k] = int(v)
}
return createBoundSecureParamToolOption(name, normalized)
}
// WithBindSecureParamIntMapFunc binds a function that returns a map of integers to a secure parameter.
func WithBindSecureParamIntMapFunc[T Integer](name string, fn func() (map[string]T, error)) ToolOption {
return createBoundSecureParamToolOption(name, func() (map[string]int, error) {
val, err := fn()
if err != nil {
return nil, err
}
normalized := make(map[string]int, len(val))
for k, v := range val {
normalized[k] = int(v)
}
return normalized, nil
})
}
// WithBindSecureParamFloatMap binds a static map of floats to a secure parameter.
func WithBindSecureParamFloatMap[T Float](name string, value map[string]T) ToolOption {
normalized := make(map[string]float64, len(value))
for k, v := range value {
normalized[k] = float64(v)
}
return createBoundSecureParamToolOption(name, normalized)
}
// WithBindSecureParamFloatMapFunc binds a function that returns a map of floats to a secure parameter.
func WithBindSecureParamFloatMapFunc[T Float](name string, fn func() (map[string]T, error)) ToolOption {
return createBoundSecureParamToolOption(name, func() (map[string]float64, error) {
val, err := fn()
if err != nil {
return nil, err
}
normalized := make(map[string]float64, len(val))
for k, v := range val {
normalized[k] = float64(v)
}
return normalized, nil
})
}
// WithBindSecureParamBoolMap binds a static map of booleans to a secure parameter.
func WithBindSecureParamBoolMap(name string, value map[string]bool) ToolOption {
return createBoundSecureParamToolOption(name, value)
}
// WithBindSecureParamBoolMapFunc binds a function that returns a map of booleans to a secure parameter.
func WithBindSecureParamBoolMapFunc(name string, fn func() (map[string]bool, error)) ToolOption {
return createBoundSecureParamToolOption(name, fn)
}
// WithBindSecureParamAnyMap binds a generic map to a secure parameter.
func WithBindSecureParamAnyMap(name string, value map[string]any) ToolOption {
return createBoundSecureParamToolOption(name, value)
}
// WithBindSecureParamAnyMapFunc binds a function that returns a generic map to a secure parameter.
func WithBindSecureParamAnyMapFunc(name string, fn func() (map[string]any, error)) ToolOption {
return createBoundSecureParamToolOption(name, fn)
}