Skip to main content

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:
  1. Uses the MastraAgent from @ag-ui/mastra
  2. Connects to OpenAI’s GPT-4o model
  3. Implements a weather tool for real-world functionality
  4. Provides an interactive chat interface in the terminal
Let’s get started!

Prerequisites

Before we begin, make sure you have:
  • Node.js 22.13.0 or later
  • An OpenAI API key
  • pnpm package manager

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:
Initialize a new Node.js project:

Set up TypeScript and basic configuration

Install TypeScript and essential development dependencies:
Create a tsconfig.json file:
Update your 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. Create src/agent.ts:

What’s happening in the agent?

  1. MastraAgent – We wrap a Mastra Agent with the AG-UI protocol adapter
  2. Model Configuration – We use OpenAI’s GPT-4o for high-quality responses
  3. Memory Setup – We configure persistent memory using LibSQL for conversation context
  4. Instructions – We give the agent basic guidelines for helpful conversation

Step 4 – Create the CLI interface

Now let’s create the interactive chat interface. Create src/index.ts:

What’s happening in the CLI interface?

  1. Readline Interface – We create an interactive prompt for user input
  2. Message Management – We add each user input to the agent’s conversation history
  3. Event Handling – We listen to AG-UI events to provide real-time feedback
  4. 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:
You should see:
Try asking questions like:
  • “Hello! How are you?”
  • “What can you help me with?”
  • “Tell me a joke”
  • “Explain quantum computing in simple terms”
You’ll see the agent respond with streaming text in real-time!

Step 6 – Understanding the AG-UI event flow

Let’s break down what happens when you send a message:
  1. User Input – You type a question and press Enter
  2. Message Added – Your input is added to the conversation history
  3. Agent Processing – The agent analyzes your request and formulates a response
  4. Response Generation – The agent streams its response back
  5. Streaming Output – You see the response appear word by word

Event types you’re handling:

  • onTextMessageStartEvent – Agent starts responding
  • onTextMessageContentEvent – Each chunk of the response
  • onTextMessageEndEvent – 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:
Create src/tools/weather.tool.ts:

What’s happening in the weather tool?

  1. Tool Definition – We use createTool from Mastra to define the tool’s interface
  2. Input Schema – We specify that the tool accepts a location string
  3. Output Schema – We define the structure of the weather data returned
  4. API Integration – We fetch data from Open-Meteo’s free weather API
  5. Data Processing – We convert weather codes to human-readable conditions

Update your agent

Now let’s update our agent to use the weather tool. Update src/agent.ts:

Update your CLI to handle tools

Update your CLI interface in src/index.ts to handle tool events:

Test your weather tool

Now restart your application and try asking about weather:
Try questions like:
  • “What’s the weather like in London?”
  • “How’s the weather in Tokyo today?”
  • “Is it raining in Seattle?”
You’ll see the agent use the weather tool to fetch real data and provide detailed responses!

Step 8 – Add more functionality

Create a browser tool

Let’s add a web browsing capability. First install the open package:
Create src/tools/browser.tool.ts:

Update your agent with both tools

Update src/agent.ts to include both tools:
Now you can ask your assistant to open websites: “Open Google for me” or “Show me the weather website”.

Step 9 – Deploy your client

Building your client

Create a production build:

Create a startup script

Add to your package.json:
Add a shebang to your built dist/index.js:
Make it executable:
Install your CLI globally:
Now you can run 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:
  1. Open source it – Publish your code on GitHub
  2. Publish to npm – Make it installable via npm install
  3. Create documentation – Help others understand and extend your work
  4. 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
From here, you can extend your client to support any use case – from simple CLI tools to complex conversational applications. The AG-UI protocol provides the foundation, and your creativity provides the possibilities. Happy building! 🚀