Implementation:Microsoft Semantic kernel Agent Kernel Plugins Add
Overview
agent.Kernel.Plugins.Add(plugin) and KernelPluginFactory.CreateFromType<T>() are the primary APIs for extending an agent's capabilities with callable tools in Microsoft Semantic Kernel. The factory creates a KernelPlugin from an annotated C# class, and the Plugins.Add method registers it on the agent's kernel so that the underlying chat completion service can advertise and invoke these tools during conversation.
Source file: dotnet/src/SemanticKernel.Core/Functions/KernelPluginFactory.cs:L37-41
Principle page: Agent Plugin Equipment
Principle:Microsoft_Semantic_kernel_Agent_Plugin_Equipment
Code Reference
KernelPluginFactory.CreateFromType<T>()
public static class KernelPluginFactory
{
/// <summary>
/// Creates a KernelPlugin from a type, discovering all methods
/// decorated with [KernelFunction].
/// </summary>
public static KernelPlugin CreateFromType<T>(
string? pluginName = null,
IServiceProvider? serviceProvider = null);
}
This factory method:
- Inspects the type
Tfor methods decorated with[KernelFunction]. - Creates a
KernelFunctionfor each discovered method, capturing parameter metadata and descriptions. - Groups them into a
KernelPluginnamed after the type (or using the optionalpluginName).
Kernel.Plugins.Add()
// KernelPluginCollection inherits from ICollection<KernelPlugin>
kernel.Plugins.Add(plugin);
This registers the plugin on the kernel's plugin collection. Once added, all functions within the plugin become available to any agent or prompt that uses this kernel.
Plugin Class Convention
public class TimePlugin
{
[KernelFunction, Description("Gets the current UTC time")]
public string GetCurrentTime() => DateTime.UtcNow.ToString("o");
[KernelFunction, Description("Gets the current date in the specified format")]
public string GetDate(
[Description("The date format string")] string format = "yyyy-MM-dd")
=> DateTime.UtcNow.ToString(format);
}
Each public method decorated with [KernelFunction] becomes a tool. The [Description] attribute provides the tool description sent to the AI model, helping it decide when and how to invoke each function.
I/O Contract
Input (Plugin Creation)
KernelPluginFactory.CreateFromType<T>(pluginName?, serviceProvider?)
├── T: Type → The plugin class with [KernelFunction] methods
├── pluginName: string? → Optional override for the plugin name (defaults to type name)
└── serviceProvider: IServiceProvider? → Optional service provider for dependency injection
Returns: KernelPlugin
├── Name: string → Plugin identifier
├── Functions: IEnumerable<KernelFunction> → Discovered functions
└── Each function includes:
├── Name: string → Method name
├── Description: string → From [Description] attribute
└── Parameters: metadata → Parameter names, types, descriptions, defaults
Input (Plugin Registration)
kernel.Plugins.Add(plugin)
├── plugin: KernelPlugin → The plugin to register
└── Effect: Plugin functions become available as tools for chat completion
Output (Tool Availability)
Once a plugin is added, its functions are serialized as tool definitions in every chat completion request made through this kernel. The AI model can then:
- Decide to call a function based on the user's message and the tool descriptions.
- Semantic Kernel automatically invokes the function and returns the result to the model.
- The model incorporates the result into its response.
Usage Examples
Basic Plugin Attachment
// Create kernel
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion("gpt-4o", apiKey)
.Build();
// Create and add plugin
var timePlugin = KernelPluginFactory.CreateFromType<TimePlugin>();
kernel.Plugins.Add(timePlugin);
// Create agent — TimePlugin is now available
ChatCompletionAgent agent = new()
{
Name = "Assistant",
Instructions = "Answer user questions. Use tools when helpful.",
Kernel = kernel
};
Multiple Plugins
kernel.Plugins.Add(KernelPluginFactory.CreateFromType<TimePlugin>());
kernel.Plugins.Add(KernelPluginFactory.CreateFromType<WeatherPlugin>());
kernel.Plugins.Add(KernelPluginFactory.CreateFromType<CalculatorPlugin>());
ChatCompletionAgent agent = new()
{
Name = "MultiToolAgent",
Instructions = "You have access to time, weather, and calculator tools.",
Kernel = kernel
};
Post-Creation Attachment via Agent's Kernel
ChatCompletionAgent agent = new()
{
Name = "Expandable",
Instructions = "You are a helpful assistant.",
Kernel = kernel
};
// Add plugins after agent creation
agent.Kernel.Plugins.Add(KernelPluginFactory.CreateFromType<FilePlugin>());
Custom Plugin Name
// Override the default plugin name (which would be "TimePlugin")
var plugin = KernelPluginFactory.CreateFromType<TimePlugin>("DateTimeTools");
kernel.Plugins.Add(plugin);
Specialized Agents with Different Plugin Sets
// Writer agent — no tools needed
Kernel writerKernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion("gpt-4o", apiKey)
.Build();
ChatCompletionAgent writer = new()
{
Name = "Writer",
Instructions = "Write creative content on the given topic.",
Kernel = writerKernel
};
// Researcher agent — equipped with search tools
Kernel researcherKernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion("gpt-4o", apiKey)
.Build();
researcherKernel.Plugins.Add(KernelPluginFactory.CreateFromType<SearchPlugin>());
researcherKernel.Plugins.Add(KernelPluginFactory.CreateFromType<WebScraperPlugin>());
ChatCompletionAgent researcher = new()
{
Name = "Researcher",
Instructions = "Research the given topic using your search tools.",
Kernel = researcherKernel
};
Related Pages
- Agent Plugin Equipment (Principle) -- The conceptual foundation for plugin attachment.
- ChatCompletionAgent (Implementation) -- The agent class that hosts the kernel and plugins.
- Chat Completion Agent Creation (Principle) -- Creating agents that can be equipped with plugins.
- Agent InvokeAsync (Implementation) -- Invoking agents that use plugins during conversation.
- Orchestration Patterns (Implementation) -- Multi-agent patterns where different agents have different plugins.