Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Microsoft Semantic kernel Plugins AddFromType

From Leeroopedia
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:

  1. Instantiates T via the service provider.
  2. Enumerates all public, non-public, instance, and static methods.
  3. Filters to those with the [KernelFunction] attribute.
  4. Creates a KernelFunction for each, incorporating [Description] metadata.
  5. Derives the plugin name from the type name if not explicitly provided.
  6. 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)));

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment