AI Kernel Plugins
AI-first map for creating AiKernelPlugin tools, retrieval plugins, and approval-aware AI extensions.
AI Kernel Plugins Discovery Map
AiKernelPlugin is the main extension point for making host capabilities
available to AI sessions as callable tools.
User Intents
- Create a custom AI tool plugin.
- Let a model inspect or modify host state through safe methods.
- Add session instructions for a specific tool surface.
- Add local retrieval to a session.
- Require approval before risky tool calls.
- Reuse the same plugin in chat and MCP.
Concept Model
- A plugin is a normal .NET object derived from
AiKernelPlugin. - Public instance methods marked with
[KernelFunction("tool_name")]become tools after the plugin is registered. [Description]on methods and parameters becomes tool metadata and schema guidance.IAiSession.AddOrUpdatePlugin("plugin_name", plugin)attaches the plugin to a chat, MCP, or coding-agent session.IAiPluginHoststores registrations and builds a flattened tool catalog.- Tool identity has two levels:
- plugin registration name, for example
docs; - explicit function name, for example
doc_search.
- plugin registration name, for example
- External tool names must contain only letters, digits,
_, and-. - Tool function names must be unique across registered plugins in the same plugin host.
- Parameters are bound by name from
KernelArguments; supported conversions include strings, numbers, enums, GUIDs, URIs, JSON elements, nullable types, and simple object serialization. CancellationTokenparameters are injected by the runtime and are not exposed as model arguments.
Host / Runtime Contexts
- App services and Razor view models usually create plugin instances through constructor injection or local composition.
- Script editor AI uses configurators such as
IScriptAiChatSessionConfiguratorto attach workspace-aware plugins to a session. - Plain helper classes must receive the plugin, session, or host services explicitly. Do not assume top-level script conveniences are available.
- MCP hosting uses the same
AiKernelPluginmethods, but instructions come fromGetMcpServerInstructions.
Minimal Plugin Shape
using System.ComponentModel;
using EyeAuras.AI.SemanticKernel;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
public sealed class WorkspaceSummaryPlugin : AiKernelPlugin
{
private readonly IWorkspaceIndex workspaceIndex;
public WorkspaceSummaryPlugin(IWorkspaceIndex workspaceIndex)
{
this.workspaceIndex = workspaceIndex;
}
public override void InitializeChatHistory(in ChatHistory chatHistory)
{
chatHistory.AddSystemMessage(
"Use workspace_summary_get before making claims about workspace state.");
}
[KernelFunction("workspace_summary_get")]
[Description("Return a concise summary of the current workspace state.")]
public WorkspaceSummary GetSummary(
[Description("Maximum number of items to include.")] int maxItems = 20,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return workspaceIndex.GetSummary(maxItems);
}
}
Register it:
session.AddOrUpdatePlugin("workspace", new WorkspaceSummaryPlugin(index));
API Details
AiKernelPlugin.InitializeKernel- customize the session-scoped Semantic Kernel after plugin registration.AiKernelPlugin.InitializeChatHistory- add bootstrap system messages toChatHistory.AiKernelPlugin.GetChatHistoryBootstrapSystemMessages- extracts distinct system messages fromInitializeChatHistory.AiKernelPlugin.GetMcpServerInstructions- exports instructions when hosted through MCP; default is the same system messages.AiKernelPlugin.GetRetrievalServices- returnsIAiRetrievalServiceinstances that can inject context into model requests.AiKernelPlugin.AiFunctionApprovalCallback- plugin-level approval hook for risky tool calls.AiKernelPluginRegistration- plugin name plus instance.IAiPluginHost.AddOrUpdatePlugin- validates and stores a plugin.IAiPluginHost.ListTools- reflects[KernelFunction]methods intoAiToolDescriptorvalues.IAiPluginHost.InvokeToolAsync- invokes a registered tool by plugin/tool name.AiToolDescriptorandAiToolParameterDescriptor- normalized tool catalog records.AiFunctionInvocationPolicy- session-level allowlist, denylist, default approval mode, and approval callback.AiToolCallFilter- runtime enforcement and telemetry for Semantic Kernel tool calls.AiScriptingModeToolsPluginBase- shared base for source-mode-specific scripting plugin adapters such as in-memory and disk-backed EyePad surfaces.AiScriptingInMemoryToolsPlugin- prefixed in-memory scripting surface withscript_inmemory_*inspection, edit, diagnostics, symbol, and NuGet/package analysis tools.AiScriptingOnDiskToolsPlugin- prefixed disk-backed scripting surface withscript_ondisk_*diagnostics, semantic lookup, symbol documentation, and package/reference analysis tools; file text discovery and source edits remain the responsibility of filesystem/workspace tools.
Retrieval Plugins
Use retrieval when the model needs local context before deciding whether to call tools.
- Implement or reuse
IAiRetrievalService. - Return it from
AiKernelPlugin.GetRetrievalServices. - The session combines retrieval services into
CompositeAiRetrievalService. - Retrieval payloads are injected into the request context by
AiConversationEngine. AiDocsKnowledgeBasePluginis the built-in example for markdown docs:IBm25KnowledgeBase,Bm25Document,Bm25SearchQuery, andBm25SearchHit.
Tool Approval
- Use
AiFunctionInvocationPolicywhen a session contains tools that can mutate state, touch files, send input, call external services, or execute expensive operations. - Match rules accept unqualified tool names or
plugin.toolnames. - Whitelisted tools run without approval.
- Blacklisted tools require approval even when default mode allows calls.
- With
AiFunctionInvocationDefaultMode.RequireApproval, all non-whitelisted tools require an approval callback. AiFunctionApprovalResult.Allowexecutes the tool.AiFunctionApprovalResult.Denyblocks it and can return a synthetic result.AiFunctionApprovalResult.Handledskips invocation and returns a callback result.
Common Flows
Add A Simple Tool
- Create a class derived from
AiKernelPlugin. - Add public methods with
[KernelFunction("stable_tool_name")]. - Add
[Description]to the method and each user-facing parameter. - Add
CancellationTokento long-running tools. - Register with
session.AddOrUpdatePlugin. - Verify with
session.PluginHost.ListTools.
Add A Tool With Instructions
- Override
InitializeChatHistory. - Add concise system guidance that tells the model when and how to use tools.
- Keep instructions host-neutral where possible.
- Register the plugin before the first user turn.
- If the same plugin is hosted through MCP, override
GetMcpServerInstructionsonly when MCP needs different guidance.
Add Docs Search
- Create
AiDocsKnowledgeBasePlugin. - Add markdown documents with
AddOrUpdateDocumentor directory helpers. - Mark standing guidance as pinned with
Bm25Document.IsPinned. - Call
ReindexAsyncbefore exposing the chat UI. - Register the plugin with a stable name such as
docs.
Prefer
- Prefer stable snake_case tool names.
- Prefer small purpose-built plugins over one giant mixed plugin.
- Prefer structured return records over plain text for machine-readable results.
- Prefer explicit dependencies in plugin constructors.
- Prefer
CancellationTokenon tools that read files, scan data, call services, or wait on external state. - Prefer approval policies for mutating or high-impact tools.
- Prefer pinned docs for standing behavior rules.
Avoid
- Avoid duplicate tool names across registered plugins.
- Avoid relying on C# method names as public tool names; use explicit
[KernelFunction("...")]. - Avoid returning huge strings when a paged/list/read workflow would be safer.
- Avoid writing prompts that tell the model to use unavailable services.
- Avoid tool methods that silently mutate state without clear naming and descriptions.
- Avoid blocking forever inside tools; respect cancellation.
Research Anchors
AiKernelPluginKernelFunctionAttributeDescriptionAttributeAiKernelPluginRegistrationIAiPluginHostAiPluginHostAiToolDescriptorAiToolInvocationResultAiFunctionInvocationPolicyAiFunctionApprovalRequestAiFunctionApprovalResultAiToolCallFilterIAiRetrievalServiceAiDocsKnowledgeBasePluginAiArtifactStorePluginAiScriptingToolsPluginAiScriptingModeToolsPluginBaseAiScriptingInMemoryToolsPluginAiScriptingOnDiskToolsPluginReToolsPluginEyeAurasBlazorWindowPlugin
Search Synonyms
- AI tool
- tool calling
- function calling
- kernel function
- kernel plugin
- Semantic Kernel plugin
- plugin host
- tool schema
- tool catalog
- approval callback
- allowlist
- denylist
- retrieval plugin
- RAG plugin
- docs plugin
- MCP tools
Related Maps
ml/ai-runtime.mdml/ai.mdscripting/runtime.mdreverse-engineering/reprocess.mdrecipes/script-ai-editor-assistant.md