How to Define an Agent Skill in Code with a Script in Microsoft Agent Framework in C#
A Microsoft Agent Framework skill doesn't have to live in a file: AgentInlineSkill defines one in C#, and AddScript gives it a script that is a plain C# lambda. Here's a bill-splitting skill whose script does the arithmetic, run with a person approving the script before it runs.
Syntax
AgentInlineSkill <skill> = new AgentInlineSkill(
name: "<skill-name>",
description: "<what the skill does and when to use it>",
instructions: "<instructions>")
.AddScript(
"<script-name>",
(<Type1> <parameter1>, <Type2> <parameter2>) => <result>,
description: "<what the script returns>");
AgentSkillsProvider <provider> = new(
[<skill>],
new AgentSkillsProviderOptions { DisableLoadSkillApproval = true });
| Part | What it is |
|---|---|
AgentInlineSkill | A skill written in C#. Agent Framework's reference: A skill defined entirely in code with resources (static values or delegates) and scripts (delegates). |
<skill-name> | The skill's name, documented as Skill name in kebab-case.: lowercase words joined by hyphens. |
<what the skill does and when to use it> | The description, Skill description for discovery. The agent sees it in every run and decides from it when to load the skill. |
<instructions> | The steps the agent follows once it loads the skill with the load_skill tool. |
AddScript | Adds a script. Its reference: Registers a script with this skill, backed by a C# delegate. |
<script-name> | The name the agent uses when it asks to run the script. |
(<Type1> <parameter1>, ...) => <result> | The script itself: any C# delegate, here a lambda. Its parameters are what the agent has to pass, and its return value goes back to the agent. |
<what the script returns> | An optional description of the script. |
new([<skill>], ...) | The provider for skills that are already in memory. It takes a list, so one provider can hold several skills. |
DisableLoadSkillApproval = true | Lets the agent load the skill without asking. Running a script still asks, because DisableRunSkillScriptApproval stays false. |
Where the script runs: Microsoft's Agent Skills guide: Code-defined scripts run in-process as direct delegate calls. No script runner is needed, and nothing starts a separate program: the lambda runs inside your app like any other C# code.
How the agent passes arguments: The delegate's typed parameters are automatically converted into a JSON Schema that the agent uses to pass arguments. A lambda that takes a decimal called total becomes a script whose arguments are a JSON object with a number called total, and the run below shows exactly that.
Approval: every skill tool needs approval by default. The guide's own example turns it off for loading skills and reading resources and leaves it on for scripts, with this comment in the code: DisableRunSkillScriptApproval remains false - scripts still require approval. This post does the same, so loading the skill is automatic and every script run waits for a yes.
The Example
A console app called BillSplitter defines a skill called bill-splitter in code. Its instructions tell the agent to run the split-bill script with the bill's total, the number of people and the tip percentage (10 when no tip is given), and then to say how much each person pays. The script is one C# lambda: the total plus the tip, divided by the number of people, rounded to two decimal places. The app asks how to split a bill of 84.60 between 4 people with a 15% tip, and asks you before the script runs.
The model is billed per token. gpt-5.6-luna on a Global Standard deployment costs $0.20 per million input tokens and $1.20 per million output tokens, and the run below made 3 requests to the model with 1,660 input and 102 output tokens in all: $0.00045.
Prerequisites
- The .NET 10 SDK.
- A Microsoft Foundry project with a deployed chat model, and the Foundry User role on it.
- The Azure CLI, signed in with
az login.
Step 1: Create a Console App
dotnet new console -n BillSplitter
cd BillSplitter
dotnet add package Microsoft.Agents.AI.Foundry --version 1.28.0-preview.261106.1
dotnet add package Azure.Identity --version 1.21.0
dotnet new console -n BillSplittercreates a console app in a newBillSplitterfolder, andcdmoves into it.Microsoft.Agents.AI.Foundryis Agent Framework's package for Microsoft Foundry. Version 1.28.0-preview.261106.1 is a preview release. It brings in Agent Framework itself,Microsoft.Agents.AI1.28.0, which is generally available.Azure.Identitylets the app sign in to Foundry with your Azure CLI account.
Step 2: Define the Skill and Its Script
Replace everything in Program.cs with the skill:
using System.Text.Json;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
// A skill defined in code. Its script is a C# delegate that runs inside this app.
AgentInlineSkill billSkill = new AgentInlineSkill(
name: "bill-splitter",
description: "Splits a restaurant bill between people. Use when someone asks how much each person pays.",
instructions: """
Use this skill to split a restaurant bill.
1. Run the split-bill script with the bill total, the number of people and the tip percentage. Use 10 if no tip is given.
2. Say how much each person pays, tip included.
""")
.AddScript(
"split-bill",
(decimal total, int people, int tipPercent) => Math.Round(total * (100 + tipPercent) / 100 / people, 2),
description: "Returns how much each person pays, tip included.");
nameanddescriptionwork like the front matter of aSKILL.mdfile: the description says what the skill does and when to use it.instructionsis a C# raw string literal, the text between the two"""lines. It names the script the agent should run, so the agent knows when to call it.AddScript("split-bill", ...)adds the script and returns the same skill, which is why it can follow the constructor directly.- The lambda takes the total as a
decimal, and the number of people and the tip percentage as whole numbers (int).total * (100 + tipPercent) / 100adds the tip,/ peoplesplits it, andMath.Round(..., 2)rounds the share to two decimal places. description: "Returns how much each person pays, tip included."tells the agent what the script gives back.
Step 3: Give the Agent the Skill
Add the provider and the agent below the skill:
// Loading the skill only reads its instructions, so the agent may do it without asking.
// Running the script still needs a person's approval.
AgentSkillsProvider skills = new(
[billSkill],
new AgentSkillsProviderOptions { DisableLoadSkillApproval = true });
AIAgent agent = new AIProjectClient(
new Uri("https://foundry-devblogs-exp.services.ai.azure.com/api/projects/proj-devblogs-exp"),
new AzureCliCredential())
.AsAIAgent(new ChatClientAgentOptions
{
Name = "BillHelper",
ChatOptions = new ChatOptions
{
ModelId = "gpt-5.6-luna",
Instructions = "You help friends who eat out together. Answer in plain text, without Markdown.",
},
AIContextProviders = [skills],
});
new AgentSkillsProvider([billSkill], ...)gives the provider the skill from Step 2. The[billSkill]collection expression is a list with one skill in it.DisableLoadSkillApproval = truelets the agent load the skill on its own. The script's approval isn't touched, so it stays on.new AIProjectClient(...)connects to the Foundry project at that endpoint, andAzureCliCredentialsigns in with the account you used foraz login. Use your own project endpoint and model deployment.AIContextProviders = [skills]adds the provider to the agent, so every run gets the skill's name and description and the skill tools.
Step 4: Ask, and Answer Each Approval
Add the question and an approval loop:
AgentSession session = await agent.CreateSessionAsync();
AgentResponse response = await agent.RunAsync(
"The bill is 84.60 for the 4 of us, and we want to leave a 15% tip. How much does each person pay?", session);
PrintToolCalls(response);
// A person answers every script run before it happens.
List<ToolApprovalRequestContent> requests = ApprovalRequests(response);
while (requests.Count > 0)
{
List<AIContent> answers = [];
foreach (ToolApprovalRequestContent request in requests)
{
FunctionCallContent call = (FunctionCallContent)request.ToolCall;
Console.Write($"Run {call.Name} {JsonSerializer.Serialize(call.Arguments)}? (y/n) ");
bool approved = Console.ReadLine()?.Trim().Equals("y", StringComparison.OrdinalIgnoreCase) == true;
answers.Add(request.CreateResponse(approved));
}
response = await agent.RunAsync(new ChatMessage(ChatRole.User, answers), session);
PrintToolCalls(response);
requests = ApprovalRequests(response);
}
Console.WriteLine();
Console.WriteLine(response.Text);
When the agent wants to run the script, the run stops and its response holds a ToolApprovalRequestContent instead of the script's result. The loop shows each request with the script's arguments, reads y or n, turns the answer into a response with request.CreateResponse(approved), and sends the answers back in the same session. It repeats until the agent asks for nothing more. How to Require Human Approval Before a Microsoft Agent Framework Agent Calls a Tool in C# explains each part of this loop.
Add the two helpers at the end of the file:
// Prints the tools the agent called.
static void PrintToolCalls(AgentResponse response)
{
foreach (FunctionCallContent call in response.Messages.SelectMany(message => message.Contents).OfType<FunctionCallContent>())
{
Console.WriteLine($"Tool call: {call.Name} {JsonSerializer.Serialize(call.Arguments)}");
}
}
// The approval requests in a response.
static List<ToolApprovalRequestContent> ApprovalRequests(AgentResponse response) =>
response.Messages.SelectMany(message => message.Contents).OfType<ToolApprovalRequestContent>().ToList();
PrintToolCallsprints every tool call in a response, with its arguments as JSON.ApprovalRequestscollects the approval requests in a response. When there are none, the loop ends.
Step 5: Run It
dotnet run
When the app asks (y/n), type y and press Enter:
Tool call: load_skill {"skillName":"bill-splitter"}
Run run_skill_script {"skillName":"bill-splitter","scriptName":"split-bill","arguments":{"total":84.6,"people":4,"tipPercent":15}}? (y/n) y
Tool call: run_skill_script {"skillName":"bill-splitter","scriptName":"split-bill","arguments":{"total":84.6,"people":4,"tipPercent":15}}
Each person pays $24.32, including the 15% tip.

Line by line:
load_skillran without a question, because loading needs no approval here. The agent matched the question to the skill's description and loaded its instructions.- The approval prompt is the agent asking to run
split-billthrough therun_skill_scripttool. Its arguments are the lambda's parameters by name,total,peopleandtipPercent, with the numbers from the question. run_skill_scriptran after they. The lambda computed 84.60 × 115 ÷ 100 ÷ 4 = 24.3225 and rounded it to 24.32.- The answer is the model's sentence around that number. The question gave no currency; the model added the dollar sign itself.
The run made three requests to the model: one that ended in the load_skill call, one that ended in the request to run the script, and one, after the approval, that wrote the answer.
Clean Up
The agent works on Foundry's Responses API, and Foundry stores the responses a run creates, so the run left its three responses in the project. Microsoft's Responses API guide: By default, response data is retained for 30 days. How to Delete a Stored Response in Microsoft Foundry with the Azure CLI deletes one sooner by its ID, and How to Keep Microsoft Foundry from Storing an Agent's Conversations in C# shows an agent that stores nothing.
The Complete File
using System.Text.Json;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
// A skill defined in code. Its script is a C# delegate that runs inside this app.
AgentInlineSkill billSkill = new AgentInlineSkill(
name: "bill-splitter",
description: "Splits a restaurant bill between people. Use when someone asks how much each person pays.",
instructions: """
Use this skill to split a restaurant bill.
1. Run the split-bill script with the bill total, the number of people and the tip percentage. Use 10 if no tip is given.
2. Say how much each person pays, tip included.
""")
.AddScript(
"split-bill",
(decimal total, int people, int tipPercent) => Math.Round(total * (100 + tipPercent) / 100 / people, 2),
description: "Returns how much each person pays, tip included.");
// Loading the skill only reads its instructions, so the agent may do it without asking.
// Running the script still needs a person's approval.
AgentSkillsProvider skills = new(
[billSkill],
new AgentSkillsProviderOptions { DisableLoadSkillApproval = true });
AIAgent agent = new AIProjectClient(
new Uri("https://foundry-devblogs-exp.services.ai.azure.com/api/projects/proj-devblogs-exp"),
new AzureCliCredential())
.AsAIAgent(new ChatClientAgentOptions
{
Name = "BillHelper",
ChatOptions = new ChatOptions
{
ModelId = "gpt-5.6-luna",
Instructions = "You help friends who eat out together. Answer in plain text, without Markdown.",
},
AIContextProviders = [skills],
});
AgentSession session = await agent.CreateSessionAsync();
AgentResponse response = await agent.RunAsync(
"The bill is 84.60 for the 4 of us, and we want to leave a 15% tip. How much does each person pay?", session);
PrintToolCalls(response);
// A person answers every script run before it happens.
List<ToolApprovalRequestContent> requests = ApprovalRequests(response);
while (requests.Count > 0)
{
List<AIContent> answers = [];
foreach (ToolApprovalRequestContent request in requests)
{
FunctionCallContent call = (FunctionCallContent)request.ToolCall;
Console.Write($"Run {call.Name} {JsonSerializer.Serialize(call.Arguments)}? (y/n) ");
bool approved = Console.ReadLine()?.Trim().Equals("y", StringComparison.OrdinalIgnoreCase) == true;
answers.Add(request.CreateResponse(approved));
}
response = await agent.RunAsync(new ChatMessage(ChatRole.User, answers), session);
PrintToolCalls(response);
requests = ApprovalRequests(response);
}
Console.WriteLine();
Console.WriteLine(response.Text);
// Prints the tools the agent called.
static void PrintToolCalls(AgentResponse response)
{
foreach (FunctionCallContent call in response.Messages.SelectMany(message => message.Contents).OfType<FunctionCallContent>())
{
Console.WriteLine($"Tool call: {call.Name} {JsonSerializer.Serialize(call.Arguments)}");
}
}
// The approval requests in a response.
static List<ToolApprovalRequestContent> ApprovalRequests(AgentResponse response) =>
response.Messages.SelectMany(message => message.Contents).OfType<ToolApprovalRequestContent>().ToList();

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling AddScript after the skill is in a provider | The reference warns: Calls made after that point will not be reflected in the generated content. | Add every script before you create the provider |
Setting DisableRunSkillScriptApproval = true for scripts you don't control | The agent runs them without asking anyone | Keep script approval on unless you trust every script the skill has |
Leaving out DisableLoadSkillApproval = true | Every skill tool needs approval by default, so the app asks you before it loads the skill too | Turn the approval off for loading when you trust the skill |
FAQ
How does the agent know the script exists?
The provider lists it in the skill's content, which the agent gets from load_skill. AddScript's reference: The script is listed in the <available_scripts> block of the skill body so the LLM knows it can be called.
What if the model calls load_skill and the script in the same response?
Then the app asks you about both, load_skill included. The guide warns that this can happen: When some tools require approval and others do not in the same response, the model may call both types simultaneously. The function-calling layer of Microsoft.Extensions.AI then treats them alike: all received FunctionCallContent in that same response will also require approval. Setting EnableNonApprovalRequiredFunctionBypassing = true in ChatClientAgentOptions changes that, so that, in the guide's words, approval-free tools execute immediately while the user is prompted only for the remaining ones. In this run the two calls came in separate responses.
Can the skill also give the agent reference text?
Yes. AgentInlineSkill has AddResource too, for content the agent reads on demand with the read_skill_resource tool.
Conclusion
AgentInlineSkill puts a skill's name, description and instructions in C#, and AddScript turns a lambda into a script that runs inside the app, with its parameters as the script's arguments. Loading ran on its own, the script waited for a y, and the lambda turned 84.60, 4 people and a 15% tip into 24.32 for the agent's answer.
Additional Resources
- Agent Skills — code-defined skills and scripts, and tool approval.
- AgentInlineSkill Class — the skill class and when to add scripts.
- AgentInlineSkill.AddScript Method — the script's name, delegate and description.
- AgentSkillsProviderOptions.DisableRunSkillScriptApproval Property — the approval for running scripts.
