Implementation:Microsoft Semantic kernel KernelFunction Attribute
| Knowledge Sources | |
|---|---|
| Domains | AI_Orchestration, Plugin_Architecture, Function_Calling |
| Last Updated | 2026-02-11 19:00 GMT |
Overview
Concrete tool for declaratively exposing C# methods as AI-callable functions using the [KernelFunction] and [Description] attributes in Microsoft Semantic Kernel.
Description
The [KernelFunction] attribute is a marker attribute applied to methods within a plugin class to indicate that the method should be exposed as a callable function to AI models. When the framework scans a plugin class (via reflection or source generation), only methods decorated with this attribute are included in the resulting KernelPlugin. The attribute optionally accepts a name parameter to override the default function name derived from the method name.
The [Description("...")] attribute (from System.ComponentModel) is applied to the class, method, and each parameter to provide natural-language descriptions. These descriptions are serialized into the JSON schema sent to the AI model during function calling, enabling the model to understand what each function does and what arguments it expects.
Method visibility (public, private, internal) does not affect eligibility: any method tagged with [KernelFunction] will be discovered. Parameters are automatically mapped from KernelArguments by name, with support for type conversion via TypeConverter. Special parameter types such as Kernel, CancellationToken, and ILogger are automatically injected from the runtime context rather than from the AI model's arguments.
Usage
Use [KernelFunction] on every method in a plugin class that should be callable by the AI model. Use [Description] on the method and each parameter to provide clear, concise descriptions that guide the model's tool selection and argument generation.
Code Reference
Source Location
- Repository: semantic-kernel
- File (KernelFunctionAttribute):
dotnet/src/SemanticKernel.Abstractions/Functions/KernelFunctionAttribute.cs - File (Sample Usage):
dotnet/samples/GettingStarted/Step2_Add_Plugins.cs:L54-78
Signature
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class KernelFunctionAttribute : Attribute
{
public KernelFunctionAttribute() { }
public KernelFunctionAttribute(string? name) => this.Name = name;
public string? Name { get; }
}
Import
using Microsoft.SemanticKernel;
using System.ComponentModel; // for [Description]
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | string? |
No | Optional override for the function name. If null, the method name is used. |
The [Description] attribute (applied separately) takes a single required string parameter providing the description text.
Outputs
| Name | Type | Description |
|---|---|---|
| (attribute metadata) | N/A | The attribute itself produces no runtime output. It serves as metadata consumed by KernelPluginFactory during plugin creation to generate KernelFunction instances with associated JSON schema. |
Usage Examples
Simple Plugin with Single Function
using Microsoft.SemanticKernel;
using System.ComponentModel;
public class TimeInformation
{
[KernelFunction]
[Description("Retrieves the current time in UTC.")]
public string GetCurrentUtcTime() => DateTime.UtcNow.ToString("R");
}
Plugin with Complex Parameters (Enums and Arrays)
using Microsoft.SemanticKernel;
using System.ComponentModel;
using System.Text.Json.Serialization;
public class WidgetFactory
{
[KernelFunction]
[Description("Creates a new widget of the specified type and colors")]
public WidgetDetails CreateWidget(
[Description("The type of widget to be created")] WidgetType widgetType,
[Description("The colors of the widget to be created")] WidgetColor[] widgetColors)
{
var colors = string.Join('-', widgetColors.Select(c => c.GetDisplayName()).ToArray());
return new()
{
SerialNumber = $"{widgetType}-{colors}-{Guid.NewGuid()}",
Type = widgetType,
Colors = widgetColors
};
}
}
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum WidgetType
{
[Description("A widget that is useful.")]
Useful,
[Description("A widget that is decorative.")]
Decorative
}
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum WidgetColor
{
[Description("Use when creating a red item.")]
Red,
[Description("Use when creating a green item.")]
Green,
[Description("Use when creating a blue item.")]
Blue
}
Plugin with Custom Function Name
using Microsoft.SemanticKernel;
using System.ComponentModel;
public class MathPlugin
{
[KernelFunction("add")]
[Description("Adds two numbers together and returns the sum.")]
public double AddNumbers(
[Description("The first number")] double a,
[Description("The second number")] double b)
{
return a + b;
}
}