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
TThe 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 a value that indicates 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
A 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.
DeferChangedEvents()
Begins a deferred-change scope. Changed notifications are suppressed until the returned scope is disposed, at which point a single notification is raised if any mutation occurred during the scope. Multiple scopes may be active simultaneously; the notification fires once all active scopes have been disposed.
public IDisposable DeferChangedEvents()
Returns
- IDisposable
An IDisposable that ends the deferral scope when disposed.
Remarks
The scope is exception-safe: even if an exception is thrown inside a using block,
the deferral is ended on dispose. If any mutation occurred before the exception, a single
Changed notification is raised.
Mutations from any thread during an open scope are coalesced. A single Changed notification fires on the thread that disposes the last active scope, only if at least one mutation occurred. All deferral state transitions are guarded by an internal lock, so concurrent mutations and concurrent scope disposal are both safe. Disposing the same scope instance more than once is safe and has no additional effect.
GetEnumerator()
public virtual IEnumerator<T> GetEnumerator()
Returns
- IEnumerator<T>
RaiseChanged()
Raises Changed if there are registered handlers.
protected void RaiseChanged()
Remarks
If a DeferChangedEvents() scope is active, the notification is deferred until all active scopes are disposed. Derived types that override mutation methods and call RaiseChanged() will automatically participate in deferral.
Remove(T)
Removes the specified primitive from the collection.
public virtual bool Remove(T primitive)
Parameters
primitiveTThe primitive to be removed from the collection.
Returns
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
Exceptions
- ArgumentNullException
nameis null.
Events
Changed
Occurs when the collection is changed.
public event EventHandler? Changed
Event Type
Remarks
By default, this event is raised when a primitive is added or removed. However, a derived implementation might raise this event for other reasons, such as when a primitive is modified.