The ADK Utils Example demonstrates how to equip agents with powerful function tools using the Google Agent Development Kit (ADK). Tools extend agent capabilities beyond text generation, enabling them to perform actions, retrieve data, and generate structured content.
A mock tool demonstrating how to retrieve external data based on user input.
app/agents/agent1.ts
import { FunctionTool, LlmAgent } from "@google/adk";import { z } from "zod";const getCurrentTime = new FunctionTool({ name: "get_current_time", description: "Returns the current time in a specified city.", parameters: z.object({ city: z .string() .describe("The name of the city for which to retrieve the current time."), }), execute: ({ city }) => { return { status: "success", report: `The current time in ${city} is 10:30 AM`, }; },});
This is a mock implementation. In production, you would integrate with a real time/timezone API.
Usage Example:
User: What time is it in Tokyo?Agent: [Calls get_current_time tool with city="Tokyo"]Agent: The current time in Tokyo is 10:30 AM
Generates Mermaid.js diagrams directly in the chat interface.
app/agents/agent1.ts
const createMermaidDiagram = new FunctionTool({ name: "create_mermaid_diagram", description: "Creates a mermaid diagram using markdown.", parameters: z.object({ type: z .enum([ "flowchart", "sequence", "class", "state", "er", "gantt", "pie", "mindmap", "timeline", ]) .describe("The type of diagram to create."), definition: z.string().describe("The mermaid diagram definition."), }), execute: ({ definition }) => { return { status: "success", report: `\`\`\`mermaid\n${definition}\n\`\`\``, }; },});
The tool returns markdown-formatted mermaid code blocks that are automatically rendered by the ChatMessage component’s Streamdown integration.
Supported Diagram Types:
Flowchart
Process flows and decision trees
Sequence
Interaction timelines between entities
Class
Object-oriented class structures
State
State machine diagrams
ER
Entity relationship diagrams
Gantt
Project timelines and schedules
Pie
Data distribution charts
Mindmap
Hierarchical idea maps
Timeline
Chronological event sequences
Usage Example:
User: Create a flowchart showing how to make coffeeAgent: [Calls create_mermaid_diagram tool]Agent: Here's the flowchart:```mermaidflowchart TD A[Start] --> B[Boil Water] B --> C[Grind Beans] C --> D[Add Coffee to Filter] D --> E[Pour Water] E --> F[Wait 4 Minutes] F --> G[Enjoy Coffee!]
### viewSourceCode ToolDisplays formatted code examples with syntax highlighting.```typescript app/agents/agent1.tsconst viewSourceCode = new FunctionTool({ name: "view_source_code", description: "Shows the source code asked by the user", parameters: z.object({ definition: z.string().describe("The kind of source code the user wants to see.") }), execute: ({ definition }) => { return { status: "success", report: `\`\`\`sourcecode\n${definition}\n\`\`\``, }; },});
Usage Example:
User: Show me a JavaScript closure exampleAgent: [Calls view_source_code tool]Agent: Here's an example of a JavaScript closure:```javascriptfunction createCounter() { let count = 0; return function() { return ++count; };}const counter = createCounter();console.log(counter()); // 1console.log(counter()); // 2
## Registering Tools with AgentTools are registered when creating the `LlmAgent` instance:```typescript app/agents/agent1.tsimport { LlmAgent } from "@google/adk";import { OllamaModel } from "@yagolopez/adk-utils";export const rootAgent = new LlmAgent({ name: "agent1", model: new OllamaModel("gpt-oss:120b-cloud", "https://ollama.com"), description: "Agent with three function tools: get_current_time, create_mermaid_diagram and view_source_code.", instruction: `You are a helpful assistant. If the user asks for the time in a city, use the 'get_current_time' tool. If the user asks for a diagram or visual representation, use the 'create_mermaid_diagram' tool. If the user asks to view source code, use the 'view_source_code' tool.`, tools: [getCurrentTime, createMermaidDiagram, viewSourceCode],});
The agent’s instruction prompt is crucial for tool selection. Clearly describe when and how each tool should be used.
You can easily create your own tools following the same pattern:
import { FunctionTool } from "@google/adk";import { z } from "zod";const weatherTool = new FunctionTool({ name: "get_weather", description: "Fetches current weather for a location", parameters: z.object({ location: z.string().describe("City name or coordinates"), units: z.enum(["celsius", "fahrenheit"]).default("celsius"), }), execute: async ({ location, units }) => { // Call weather API const response = await fetch( `https://api.weather.com/data?location=${location}&units=${units}` ); const data = await response.json(); return { status: "success", report: `Temperature in ${location}: ${data.temp}°${units === "celsius" ? "C" : "F"}`, data, // Optional: include raw data for further processing }; },});
User: What time is it in Tokyo and show me a timeline?Agent: 1. [Calls get_current_time with city="Tokyo"]2. [Calls create_mermaid_diagram with type="timeline"]3. Combines both outputs in response