From Chat to Code: Building Intelligent Agents with Semantic Kernel Plugins

From Chat to Code: Building Intelligent Agents with Semantic Kernel Plugins


As .NET developers, we've long been masters of structure, types, and services. But now, with the rise of conversational AI and powerful orchestration frameworks like Microsoft Semantic Kernel, we're entering a new era—where code responds not only to structured APIs but also to human intent. We can now turn natural language into real actions through intelligent agents that bridge the gap between chat and executable logic.

In this second article of the series, we're diving into a real, working application that brings this to life. We’ll show how to integrate Semantic Kernel with your C# backend to build a plugin-enabled agent that can:

  • Solve logical problems (e.g., math equations)
  • Schedule meetings based on user prompts
  • Chain functions together based on user intent
  • Generate structured outputs through semantic understanding

Let’s roll up our sleeves and see how chat becomes code—and code becomes intelligence.


The Vision: Intelligent Agents in .NET

What if your app could take a request like:

“Can you schedule a meeting for 3 days from now at 5 pm, and tell me the result of (5+5+5+2+210)10?”

…and automatically:

  1. Calculate the date three days ahead,
  2. Schedule a meeting at 5 PM,
  3. Solve the math equation,
  4. Respond in clean, conversational text…

All without any hardcoded branching logic. With Semantic Kernel and function plugins, that’s exactly what we can do.


Semantic Kernel + Function Calling: What It Is

Semantic Kernel (SK) acts as an orchestration engine. It allows you to:

  • Use natural language as the interface,
  • Register native C# methods as tools (plugins),
  • Let LLMs (like Azure OpenAI’s GPT-4o) intelligently select and invoke those tools.

This turns your C# codebase into a dynamic toolkit, letting the AI mix logic with reasoning in ways traditional command handling could never achieve.

Code Walkthrough: From Setup to Intelligence

Let’s look at the heart of the application:

var builder = Kernel.CreateBuilder()
    .AddAzureOpenAIChatCompletion("gpt-4o",
        endpoint: "https://khaledsemantickernelkh.openai.azure.com/",
        apiKey: "");

var kernel = builder.Build();

kernel.ImportPluginFromObject(new MyTools(), "tools");
kernel.ImportPluginFromObject(new ScheduleMeeting(), "meetingScheduler");

var chat = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
Console.Write("Please enter your prompt: ");
var userInput = Console.ReadLine();
history.AddUserMessage(userInput);

var resultChat = await chat.GetChatMessageContentAsync(history,
    new PromptExecutionSettings {
        FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
    },
    kernel);

Console.WriteLine(resultChat.Content);
Console.WriteLine($"Chat History : {JsonSerializer.Serialize(history)}");
        


Key Components:

  • AddAzureOpenAIChatCompletion: Connects SK to Azure-hosted GPT-4o.
  • ImportPluginFromObject: Registers your C# classes as callable plugins.
  • FunctionChoiceBehavior.Auto(): Gives the LLM power to decide which function(s) to invoke.
  • GetChatMessageContentAsync(...): Orchestrates chat + function calls + context—all in one pass.

Plugins: The Secret Sauce of Intelligence

Let’s unpack the plugins:

public class MyTools
{
    [KernelFunction, Description("Add two integers")]
    public int Add(int a, int b) => a + b;

    [KernelFunction, Description("Multiply two integers")]
    public int Multiply(int a, int b) => a * b;
}

public class ScheduleMeeting
{
    [KernelFunction, Description("Get the current date and time")]
    public DateTime Now() => DateTime.Now;

    [KernelFunction, Description("Schedule a meeting with a given date and time")]
    public string Schedule(string date, string time) =>
        $"Meeting scheduled on {date} at {time}";
}        


How Plugins Work:

  • [KernelFunction]: Tells SK this method is eligible for AI invocation.
  • Description(...): Provides semantic hints the model uses to pick functions based on the prompt.
  • Automatic Parameter Mapping: The LLM knows how to bind natural language values to method parameters like date, time, a, b, etc.

FunctionChoiceBehavior.Auto(): Empowering Dynamic Execution

The real magic happens with:

FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()        

This lets GPT-4o decide—based on the user prompt and the available plugin descriptions—what tool(s) to call.

Unlike older versions where developers had to manually choose which function to call, this new auto mode enables true orchestration:

  • Parses intent from language
  • Determines appropriate function(s)
  • Invokes them with mapped arguments
  • Returns composed results back in natural language

This is what unlocks goal-driven automation.


Sample Run: From Prompt to Actions

Let's walk through this user input:

"Could you please schedule a meeting for me for the next 3 days at 5 pm and also would like to know the result of the following equations (5+5+5+2+210)10?"

Here’s what SK does under the hood:

  1. Interprets Time:
  2. Schedules Meeting:
  3. Solves Math:
  4. Responds:


Chat History and Memory

var history = new ChatHistory();
history.AddUserMessage(userInput);        

The chat history tracks all interactions. This is essential for multi-turn conversations and memory-driven reasoning in future iterations—think “remind me to reschedule that meeting” or “repeat the math from before.”

Why This Matters: From Responding to Reasoning

What we’ve built isn’t just a chatbot.

It’s the foundation of an intelligent agent—a system that doesn’t just understand what you say, but can:

  • Interpret context (e.g., “next 3 days”)
  • Invoke logic (math, scheduling, reminders)
  • Chain functions together (calculate date → schedule → reply)
  • Adapt its response based on your goals

Semantic Kernel isn’t a toy—it’s a new runtime layer for smart automation. One where your C# methods are tools in a reasoning loop.


What You Can Do Next

Now that you’ve seen plugins in action, consider building:

  • A task assistant that integrates with calendars and todo lists.
  • A finance bot that analyzes data and generates charts.
  • A developer agent that turns specs into skeleton code.

The sky’s the limit when you combine language, logic, and plugins.


Ready to plug intelligence into your .NET codebase? Let Semantic Kernel be your AI runtime—and if you’ve already started, I’d love to hear about it. What’s been your experience with Semantic Kernel so far, and what’s the most exciting thing you’ve built using it?

To view or add a comment, sign in

More articles by khaled D.

Others also viewed

Explore content categories