| 1 | // Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package mcp |
| 6 | |
| 7 | import ( |
| 8 | "iter" |
| 9 | "maps" |
| 10 | "slices" |
| 11 | ) |
| 12 | |
| 13 | // This file contains implementations that are common to all features. |
| 14 | // A feature is an item provided to a peer. In the 2025-03-26 spec, |
| 15 | // the features are prompt, tool, resource and root. |
| 16 | |
| 17 | // A featureSet is a collection of features of type T. |
| 18 | // Every feature has a unique ID, and the spec never mentions |
| 19 | // an ordering for the List calls, so what it calls a "list" is actually a set. |
| 20 | // |
| 21 | // An alternative implementation would use an ordered map, but that's probably |
| 22 | // not necessary as adds and removes are rare, and usually batched. |
| 23 | type featureSet[T any] struct { |
| 24 | uniqueID func(T) string |
| 25 | features map[string]T |
| 26 | sortedKeys []string // lazily computed; nil after add or remove |
| 27 | } |
| 28 | |
| 29 | // newFeatureSet creates a new featureSet for features of type T. |
| 30 | // The argument function should return the unique ID for a single feature. |
| 31 | func newFeatureSet[T any](uniqueIDFunc func(T) string) *featureSet[T] { |
| 32 | return &featureSet[T]{ |
| 33 | uniqueID: uniqueIDFunc, |
| 34 | features: make(map[string]T), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // add adds each feature to the set if it is not present, |
| 39 | // or replaces an existing feature. |
| 40 | func (s *featureSet[T]) add(fs ...T) { |
| 41 | for _, f := range fs { |
| 42 | s.features[s.uniqueID(f)] = f |
| 43 | } |
| 44 | s.sortedKeys = nil |
| 45 | } |
| 46 | |
| 47 | // remove removes all features with the given uids from the set if present, |
| 48 | // and returns whether any were removed. |
| 49 | // It is not an error to remove a nonexistent feature. |
| 50 | func (s *featureSet[T]) remove(uids ...string) bool { |
| 51 | changed := false |
| 52 | for _, uid := range uids { |
| 53 | if _, ok := s.features[uid]; ok { |
| 54 | changed = true |
| 55 | delete(s.features, uid) |
| 56 | } |
| 57 | } |
| 58 | if changed { |
| 59 | s.sortedKeys = nil |
| 60 | } |
| 61 | return changed |
| 62 | } |
| 63 | |
| 64 | // get returns the feature with the given uid. |
| 65 | // If there is none, it returns zero, false. |
| 66 | func (s *featureSet[T]) get(uid string) (T, bool) { |
| 67 | t, ok := s.features[uid] |
| 68 | return t, ok |
| 69 | } |
| 70 | |
| 71 | // len returns the number of features in the set. |
| 72 | func (s *featureSet[T]) len() int { return len(s.features) } |
| 73 | |
| 74 | // all returns an iterator over of all the features in the set |
| 75 | // sorted by unique ID. |
| 76 | func (s *featureSet[T]) all() iter.Seq[T] { |
| 77 | s.sortKeys() |
| 78 | return func(yield func(T) bool) { |
| 79 | s.yieldFrom(0, yield) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // above returns an iterator over features in the set whose unique IDs are |
| 84 | // greater than `uid`, in ascending ID order. |
| 85 | func (s *featureSet[T]) above(uid string) iter.Seq[T] { |
| 86 | s.sortKeys() |
| 87 | index, found := slices.BinarySearch(s.sortedKeys, uid) |
| 88 | if found { |
| 89 | index++ |
| 90 | } |
| 91 | return func(yield func(T) bool) { |
| 92 | s.yieldFrom(index, yield) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // sortKeys is a helper that maintains a sorted list of feature IDs. It |
| 97 | // computes this list lazily upon its first call after a modification, or |
| 98 | // if it's nil. |
| 99 | func (s *featureSet[T]) sortKeys() { |
| 100 | if s.sortedKeys != nil { |
| 101 | return |
| 102 | } |
| 103 | s.sortedKeys = slices.Sorted(maps.Keys(s.features)) |
| 104 | } |
| 105 | |
| 106 | // yieldFrom is a helper that iterates over the features in the set, |
| 107 | // starting at the given index, and calls the yield function for each one. |
| 108 | func (s *featureSet[T]) yieldFrom(index int, yield func(T) bool) { |
| 109 | for i := index; i < len(s.sortedKeys); i++ { |
| 110 | if !yield(s.features[s.sortedKeys[i]]) { |
| 111 | return |
| 112 | } |
| 113 | } |
| 114 | } |