Implementation:Microsoft Semantic kernel Plugins AddFromType
| Knowledge Sources | |
|---|---|
| Domains | AI_Orchestration, Plugin_Architecture, Dependency_Injection |
| Last Updated | 2026-02-11 19:00 GMT |
Overview
Concrete tool for registering a plugin type with the kernel builder, enabling automatic instantiation and function discovery at build time in Microsoft Semantic Kernel.
Description
IKernelBuilderPlugins.AddFromType<T>() is an extension method on IKernelBuilderPlugins that registers a plugin type with the kernel's dependency injection container. When the kernel is built, the framework instantiates the type T using ActivatorUtilities.CreateInstance<T>(), resolving any constructor dependencies from the DI service provider. It then scans the instance for methods decorated with [KernelFunction] and creates a KernelPlugin containing the corresponding KernelFunction objects.
Internally, the method delegates to KernelPluginFactory.CreateFromType<T>(), which performs the actual reflection-based discovery. The factory:
- Instantiates T via the service provider.
- Enumerates all public, non-public, instance, and static methods.
- Filters to those with the [KernelFunction] attribute.
- Creates a KernelFunction for each, incorporating [Description] metadata.
- Derives the plugin name from the type name if not explicitly provided.
- Reads the class-level [Description] attribute as the plugin description.
If no [KernelFunction]-attributed methods are found on the type, the factory throws an ArgumentException.
Usage
Use AddFromType<T>() during kernel builder configuration to register native plugins. This is the recommended approach for plugins that benefit from dependency injection.
Code Reference
Source Location
- Repository: semantic-kernel
- File (Extension Method):
dotnet/src/SemanticKernel.Core/KernelExtensions.cs:L558-565 - File (Factory):
dotnet/src/SemanticKernel.Core/Functions/KernelPluginFactory.cs:L37-41
Signature
public static IKernelBuilderPlugins AddFromType<T>(
this IKernelBuilderPlugins plugins,
string? pluginName = null)
The underlying factory method:
public static KernelPlugin CreateFromType<T>(
string? pluginName = null,
IServiceProvider? serviceProvider = null)
Import
using Microsoft.SemanticKernel;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| T | Type parameter |
Yes | The plugin class type containing methods decorated with [KernelFunction]. |
| pluginName | string? |
No | Optional name for the plugin. If null, the name is derived from the type name of T. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | IKernelBuilderPlugins |
Returns the same plugins builder for fluent chaining. |
Usage Examples
Basic Plugin Registration
using Microsoft.SemanticKernel;
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatClient(modelId: "gpt-4o", apiKey: "your-api-key");
// Register plugins by type
kernelBuilder.Plugins.AddFromType<TimeInformation>();
kernelBuilder.Plugins.AddFromType<WidgetFactory>();
Kernel kernel = kernelBuilder.Build();
Registration with Custom Plugin Name
using Microsoft.SemanticKernel;
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatClient(modelId: "gpt-4o", apiKey: "your-api-key");
// Register with explicit plugin name
kernelBuilder.Plugins.AddFromType<TimeInformation>(pluginName: "Clock");
Kernel kernel = kernelBuilder.Build();
// The plugin is now accessible as "Clock" in prompt templates:
// {{Clock.GetCurrentUtcTime}}
Registering Multiple Plugins for Function Calling
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
// Build kernel with multiple plugins
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatClient(modelId: "gpt-4o", apiKey: "your-api-key");
kernelBuilder.Plugins.AddFromType<TimeInformation>();
kernelBuilder.Plugins.AddFromType<WidgetFactory>();
Kernel kernel = kernelBuilder.Build();
// All registered plugin functions are available for AI function calling
OpenAIPromptExecutionSettings settings = new()
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};
Console.WriteLine(await kernel.InvokePromptAsync("How many days until Christmas?", new(settings)));