π§ Function Tools
Extend your agent with custom C# functions β and let the LLM decide when to call them.
Function tools are one of the most powerful features of Agent Framework. They allow you to give your agent access to real-world data, systems, or business logic. When the agent determines that a tool would help answer the user's question, it automatically calls it, interprets the result, and incorporates it into its response.
Agent Framework uses AIFunctionFactory.Create() from
Microsoft.Extensions.AI to convert any C# method into an AIFunction
that the model can invoke. The method signature, parameter names, and XML doc comments are
used to build the tool schema that is sent to the model.
Key Concepts
- AIFunctionFactory.Create() β wraps a C# method as a tool the LLM can call
- tools parameter β pass an array of
AIFunctionobjects when creating the agent - Automatic invocation β the model decides when to call a tool; Agent Framework handles the call/result cycle
- Parameter descriptions β use
[Description("...")]attribute or XML docs to improve tool discovery
NuGet Packages
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
Code Sample
using System.ComponentModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
// 1. Define a plain C# function β this becomes a tool the agent can call.
// Use [Description] attributes to help the LLM understand what the parameter means.
string GetWeather(
[Description("The city or location to get weather for")] string location)
=> $"The weather in {location} is sunny with a high of 22Β°C.";
// 2. Another tool β agents can have multiple tools.
string GetTime(
[Description("The timezone, e.g. 'UTC' or 'Europe/Amsterdam'")] string timezone)
=> $"The current time in {timezone} is 14:32.";
// 3. Create the agent, passing the tools as AIFunction objects.
AIAgent agent = new AzureOpenAIClient(
new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")!),
new AzureCliCredential())
.GetChatClient("gpt-4o-mini")
.AsAIAgent(
instructions: "You are a helpful assistant with access to weather and time information.",
tools:
[
AIFunctionFactory.Create(GetWeather),
AIFunctionFactory.Create(GetTime)
]);
// 4. The agent calls the appropriate tool(s) automatically.
Console.WriteLine(await agent.RunAsync(
"What is the weather in Amsterdam, and what time is it there?"));
Step-by-Step Explanation
-
Define your C# function β Any
staticor instance method works. The return type should be astringor a type that serialises cleanly. Use[Description]attributes fromSystem.ComponentModelto annotate parameters β this helps the LLM choose the right arguments. - Add multiple tools β You can provide as many tools as needed. The agent picks the most appropriate one based on the user's request.
-
Register tools via
AIFunctionFactory.Create()β This inspects the method using reflection and builds a JSON Schema descriptor. The schema is sent to the model so it knows what arguments to pass. - Automatic tool call loop β When the model decides to call a tool, Agent Framework executes it locally and feeds the result back to the model. This loop continues until the model has everything it needs to generate the final answer.
Expected Output
The weather in Amsterdam is sunny with a high of 22Β°C, and the current time there is 14:32.
Next Steps
All Examples
- π€ Hello Agent
- π§ Function Tools
- π¬ Multi-Turn Conversations
- β‘ Streaming Responses
- π¦ Structured Output
- π Sequential Workflows
- πΈοΈ Multi-Agent Orchestration
- π¦ Ollama β Local AI
- π₯οΈ LM Studio β Local AI
- π§ Agent Memory
- π RAG
- π MCP Tools
- π OpenTelemetry
- π§ Customer Support Triage
- π¬ Research Pipeline
- π€ Tools vs Sub-Agents
Concepts Used
π Tools Documentation