How does function calling work?
Function calling can reach out to external services, perform calculations, and access real-time data all while having a conversation. You can use it for teaching your AI to use tools, like using the calculator, searching the web, or checking the weather. Here’s the flow behind function calling:- You define functions - These are tools you define for the AI (LLM/Realtime) to use
- The AI decides when to use them - Based on what the user asks, the AI figures out which tools it needs
- Functions get called automatically - The AI executes the right function with the right parameters
- Results flow back naturally - The AI incorporates the results into its response
Defining functions for agents to use
Making functions available to your AI agent is straightforward. You simply use the@llm.register_function()
decorator on any Python function you want the AI to be able to call.
Basic Function Registration
Function Parameters and Types
Your functions can have any combination of parameters, and the AI will automatically understand how to use them:Custom Function Names
Sometimes you want a function to have a different name when the AI calls it:Using Functions in Conversation
Once registered, your functions are automatically available during conversations:Overview of an MCP Server
MCP (Model Context Protocol) servers are specialized toolboxes that provide your AI agent with access to external services and data sources.What is an MCP Server?
An MCP server is essentially a service that:- Exposes tools - Functions that your AI can call
- Provides resources - Data sources your AI can read from
- Offers prompts - Pre-built conversation starters or templates
- Handles authentication - Manages access to external services
Types of MCP Servers
Local MCP Servers run on your machine and communicate via standard input/output:What MCP Servers Can Do
MCP servers can provide your AI with access to:- Database queries - Search and retrieve data
- API integrations - Connect to external services
- File operations - Read and write files
- System commands - Execute shell commands
- Web scraping - Extract data from websites
- Authentication - Handle login and permissions
Connecting an MCP Server
Connecting an MCP server to your AI agent is a breeze. You simply pass it to your agent when you create it, and the agent handles all the connection details automatically.Basic MCP Server Connection
Multiple MCP Servers
You can connect multiple MCP servers to give your agent access to different services:Automatic Tool Registration
When you connect an MCP server, the agent automatically:- Connects to the server - Establishes communication
- Discovers available tools - Finds out what functions are available
- Registers them with the LLM - Makes them available for function calling
- Handles errors gracefully - Continues working even if some servers fail