Overview
Tools extend agent capabilities by allowing them to execute code, query databases, call APIs, and interact with external systems. The .NET SDK providesAITool and AIFunction abstractions for defining and using tools with agents.
Core Concepts
AIFunction
AIFunction represents a callable function that an agent can invoke. Functions are created from .NET methods with:
- Parameters: Strongly-typed parameters with descriptions
- Return Values: Typed return values
- Metadata: Name, description, and parameter information
- Execution: Automatic parameter binding and invocation
AITool
AITool is the base abstraction that represents any tool an agent can use. AIFunction is a specific type of AITool.
Creating Functions
Using AIFunctionFactory
The simplest way to create functions is usingAIFunctionFactory.Create():
Use
[Description] attributes from System.ComponentModel to provide descriptions for the function and parameters. These help the AI model understand when and how to use the function.Async Functions
Functions can be asynchronous:Complex Parameters
Functions can accept complex types:Adding Tools to Agents
At Agent Creation
Per-Request Tools
Add tools for specific requests usingAgentRunOptions:
With ChatClientAgentOptions
Function Execution
Automatic Invocation
By default, when an agent decides to call a function, the framework automatically:- Extracts parameters from the model’s response
- Invokes the function with the parameters
- Returns the result to the model
- Continues the conversation
Manual Function Approval
For sensitive operations, require human approval before execution:Human-in-the-Loop
Learn about implementing approval workflows
Function Examples
Database Query Function
API Call Function
File System Function
Calculator Function
Agents as Tools
Convert an agent into a tool that can be used by other agents:Multi-Agent Systems
Learn about building multi-agent architectures
Function Calling with Streaming
Function calls work with streaming responses:Custom Function Metadata
Customize function metadata withAIFunctionFactoryOptions:
Best Practices
Provide Clear Descriptions
Provide Clear Descriptions
Use descriptive
[Description] attributes for functions and parameters. This helps the AI model understand when and how to use the function.Handle Errors Gracefully
Handle Errors Gracefully
Functions should handle errors and return meaningful messages:
Use CancellationToken
Use CancellationToken
Support cancellation for long-running operations:
Validate Parameters
Validate Parameters
Validate parameters before execution:
Limit Function Scope
Limit Function Scope
Each function should do one thing well. Break complex operations into multiple focused functions.
Consider Security
Consider Security
- Validate and sanitize all inputs
- Implement authorization checks
- Require approval for destructive operations
- Limit file system and network access
- Never expose sensitive credentials
Tool Configuration
Function Choice Control
Control how the model uses functions:Parallel Function Calls
Some models support calling multiple functions in parallel:Next Steps
Middleware
Add middleware for function approval and logging
Workflows
Orchestrate agents with tools in workflows
MCP Tools
Use Model Context Protocol tools
RAG
Combine tools with retrieval-augmented generation