Introduction
A client implementation allows you to build conversational applications that leverage AG-UI’s event-driven protocol. This approach creates a direct interface between your users and AI agents, demonstrating direct access to the AG-UI protocol.When to use a client implementation
Building your own client is useful if you want to explore/hack on the AG-UI protocol. For production use, use a full-featured client like CopilotKit.What you’ll build
In this guide, we’ll create a CLI client that:- Uses the
MastraAgent
from@ag-ui/mastra
- Connects to OpenAI’s GPT-4o model
- Implements a weather tool for real-world functionality
- Provides an interactive chat interface in the terminal
Prerequisites
Before we begin, make sure you have:1. Provide your OpenAI API key
First, let’s set up your API key:2. Install pnpm
If you don’t have pnpm installed:Step 1 – Initialize your project
Create a new directory for your AG-UI client:Set up TypeScript and basic configuration
Install TypeScript and essential development dependencies:tsconfig.json
file:
package.json
scripts:
Step 2 – Install AG-UI and dependencies
Install the core AG-UI packages and dependencies:Step 3 – Create your agent
Let’s create a basic conversational agent. Createsrc/agent.ts
:
What’s happening in the agent?
- MastraAgent – We wrap a Mastra Agent with the AG-UI protocol adapter
- Model Configuration – We use OpenAI’s GPT-4o for high-quality responses
- Memory Setup – We configure persistent memory using LibSQL for conversation context
- Instructions – We give the agent basic guidelines for helpful conversation
Step 4 – Create the CLI interface
Now let’s create the interactive chat interface. Createsrc/index.ts
:
What’s happening in the CLI interface?
- Readline Interface – We create an interactive prompt for user input
- Message Management – We add each user input to the agent’s conversation history
- Event Handling – We listen to AG-UI events to provide real-time feedback
- Streaming Display – We show the agent’s response as it’s being generated
Step 5 – Test your assistant
Let’s run your new AG-UI client:- “Hello! How are you?”
- “What can you help me with?”
- “Tell me a joke”
- “Explain quantum computing in simple terms”
Step 6 – Understanding the AG-UI event flow
Let’s break down what happens when you send a message:- User Input – You type a question and press Enter
- Message Added – Your input is added to the conversation history
- Agent Processing – The agent analyzes your request and formulates a response
- Response Generation – The agent streams its response back
- Streaming Output – You see the response appear word by word
Event types you’re handling:
onTextMessageStartEvent
– Agent starts respondingonTextMessageContentEvent
– Each chunk of the responseonTextMessageEndEvent
– Response is complete
Step 7 – Add tool functionality
Now that you have a working chat interface, let’s add some real-world capabilities by creating tools. We’ll start with a weather tool.Create your first tool
Let’s create a weather tool that your agent can use. Create the directory structure:src/tools/weather.tool.ts
:
What’s happening in the weather tool?
- Tool Definition – We use
createTool
from Mastra to define the tool’s interface - Input Schema – We specify that the tool accepts a location string
- Output Schema – We define the structure of the weather data returned
- API Integration – We fetch data from Open-Meteo’s free weather API
- Data Processing – We convert weather codes to human-readable conditions
Update your agent
Now let’s update our agent to use the weather tool. Updatesrc/agent.ts
:
Update your CLI to handle tools
Update your CLI interface insrc/index.ts
to handle tool events:
Test your weather tool
Now restart your application and try asking about weather:- “What’s the weather like in London?”
- “How’s the weather in Tokyo today?”
- “Is it raining in Seattle?”
Step 8 – Add more functionality
Create a browser tool
Let’s add a web browsing capability. First install theopen
package:
src/tools/browser.tool.ts
:
Update your agent with both tools
Updatesrc/agent.ts
to include both tools:
Step 9 – Deploy your client
Building your client
Create a production build:Create a startup script
Add to yourpackage.json
:
dist/index.js
:
Link globally
Install your CLI globally:weather-assistant
from anywhere!
Extending your client
Your AG-UI client is now a solid foundation. Here are some ideas for enhancement:Add more tools
- Calculator tool – For mathematical operations
- File system tool – For reading/writing files
- API tools – For connecting to other services
- Database tools – For querying data
Improve the interface
- Rich formatting – Use libraries like
chalk
for colored output - Progress indicators – Show loading states for long operations
- Configuration files – Allow users to customize settings
- Command-line arguments – Support different modes and options
Add persistence
- Conversation history – Save and restore chat sessions
- User preferences – Remember user settings
- Tool results caching – Cache expensive API calls
Share your client
Built something useful? Consider sharing it with the community:- Open source it – Publish your code on GitHub
- Publish to npm – Make it installable via
npm install
- Create documentation – Help others understand and extend your work
- Join discussions – Share your experience in the AG-UI GitHub Discussions
Conclusion
You’ve built a complete AG-UI client from scratch! Your weather assistant demonstrates the core concepts:- Event-driven architecture with real-time streaming
- Tool integration for real-world functionality
- Conversation memory for context retention
- Interactive CLI interface for user engagement