Class McpServerPrimitiveCollection<T>
- Namespace
- ModelContextProtocol.Server
- Assembly
- ModelContextProtocol.Core.dll
Provides a thread-safe collection of T instances, indexed by their names.
public class McpServerPrimitiveCollection<T> : ICollection<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable where T : IMcpServerPrimitive
Type Parameters
TSpecifies the type of primitive stored in the collection.
- Inheritance
-
McpServerPrimitiveCollection<T>
- Implements
-
ICollection<T>IEnumerable<T>
- Derived
- Inherited Members
Constructors
McpServerPrimitiveCollection(IEqualityComparer<string>?)
Initializes a new instance of the McpServerPrimitiveCollection<T> class.
public McpServerPrimitiveCollection(IEqualityComparer<string>? keyComparer = null)
Parameters
keyComparerIEqualityComparer<string>
Properties
Count
Gets the number of primitives in the collection.
public int Count { get; }
Property Value
IsEmpty
Gets whether there are any primitives in the collection.
public bool IsEmpty { get; }
Property Value
this[string]
Gets the T with the specified name from the collection.
public T this[string name] { get; }
Parameters
namestringThe name of the primitive to retrieve.
Property Value
- T
The
Twith the specified name.
Exceptions
- ArgumentNullException
nameis null.- KeyNotFoundException
An primitive with the specified name does not exist in the collection.
PrimitiveNames
Gets the names of all of the primitives in the collection.
public virtual ICollection<string> PrimitiveNames { get; }
Property Value
Methods
Add(T)
Adds the specified T to the collection.
public void Add(T primitive)
Parameters
primitiveTThe primitive to be added.
Exceptions
- ArgumentNullException
primitiveis null.- ArgumentException
A primitive with the same name as
primitivealready exists in the collection.
Clear()
Clears all primitives from the collection.
public virtual void Clear()
Contains(T)
Checks if a specific primitive is present in the collection of primitives.
public virtual bool Contains(T primitive)
Parameters
primitiveTThe primitive to search for in the collection.
Returns
Exceptions
- ArgumentNullException
primitiveis null.
CopyTo(T[], int)
Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.
public virtual void CopyTo(T[] array, int arrayIndex)
Parameters
arrayT[]The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.
arrayIndexintThe zero-based index in
arrayat which copying begins.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis less than 0.- ArgumentException
The number of elements in the source ICollection<T> is greater than the available space from
arrayIndexto the end of the destinationarray.
GetEnumerator()
public virtual IEnumerator<T> GetEnumerator()
Returns
- IEnumerator<T>
RaiseChanged()
Raises Changed if there are registered handlers.
protected void RaiseChanged()
Remove(T)
Removes the specified primitivefrom the collection.
public virtual bool Remove(T primitive)
Parameters
primitiveTThe primitive to be removed from the collection.
Returns
- bool
true if the primitive was found in the collection and removed; otherwise, false if it couldn't be found.
Exceptions
- ArgumentNullException
primitiveis null.
ToArray()
Creates an array containing all of the primitives in the collection.
public virtual T[] ToArray()
Returns
- T[]
An array containing all of the primitives in the collection.
TryAdd(T)
Adds the specified T to the collection.
public virtual bool TryAdd(T primitive)
Parameters
primitiveTThe primitive to be added.
Returns
Exceptions
- ArgumentNullException
primitiveis null.
TryGetPrimitive(string, out T?)
Attempts to get the primitive with the specified name from the collection.
public virtual bool TryGetPrimitive(string name, out T? primitive)
Parameters
namestringThe name of the primitive to retrieve.
primitiveTThe primitive, if found; otherwise, null.
Returns
- bool
true if the primitive was found in the collection and return; otherwise, false if it couldn't be found.
Exceptions
- ArgumentNullException
nameis null.
Events
Changed
Occurs when the collection is changed.
public event EventHandler? Changed
Event Type
Remarks
By default, this is raised when a primitive is added or removed. However, a derived implementation may raise this event for other reasons, such as when a primitive is modified.