using Microsoft.Extensions.AI; using ModelContextProtocol.Protocol.Types; using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace ModelContextProtocol.Server; /// Represents an invocable tool used by Model Context Protocol clients and servers. public abstract class McpServerTool : IMcpServerPrimitive { /// Initializes a new instance of the class. protected McpServerTool() { } /// Gets the protocol type for this instance. public abstract Tool ProtocolTool { get; } /// Invokes the . /// The request information resulting in the invocation of this tool. /// The to monitor for cancellation requests. The default is . /// The call response from invoking the tool. /// is . public abstract Task InvokeAsync( RequestContext request, CancellationToken cancellationToken = default); /// /// Creates an instance for a method, specified via a instance. /// /// The method to be represented via the created . /// Optional options used in the creation of the to control its behavior. /// The created for invoking . /// is . public static McpServerTool Create( Delegate method, McpServerToolCreateOptions? options = null) => AIFunctionMcpServerTool.Create(method, options); /// /// Creates an instance for a method, specified via a instance. /// /// The method to be represented via the created . /// The instance if is an instance method; otherwise, . /// Optional options used in the creation of the to control its behavior. /// The created for invoking . /// is . /// is an instance method but is . public static McpServerTool Create( MethodInfo method, object? target = null, McpServerToolCreateOptions? options = null) => AIFunctionMcpServerTool.Create(method, target, options); /// /// Creates an instance for a method, specified via an for /// and instance method, along with a representing the type of the target object to /// instantiate each time the method is invoked. /// /// The instance method to be represented via the created . /// /// The to construct an instance of on which to invoke when /// the resulting is invoked. If services are provided, /// ActivatorUtilities.CreateInstance will be used to construct the instance using those services; otherwise, /// is used, utilizing the type's public parameterless constructor. /// If an instance can't be constructed, an exception is thrown during the function's invocation. /// /// Optional options used in the creation of the to control its behavior. /// The created for invoking . /// is . public static McpServerTool Create( MethodInfo method, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType, McpServerToolCreateOptions? options = null) => AIFunctionMcpServerTool.Create(method, targetType, options); /// Creates an that wraps the specified . /// The function to wrap. /// Optional options used in the creation of the to control its behavior. /// is . /// /// Unlike the other overloads of Create, the created by /// does not provide all of the special parameter handling for MCP-specific concepts, like . /// public static McpServerTool Create( AIFunction function, McpServerToolCreateOptions? options = null) => AIFunctionMcpServerTool.Create(function, options); /// public override string ToString() => ProtocolTool.Name; /// string IMcpServerPrimitive.Name => ProtocolTool.Name; }