1
1
---
2
- title : " Client "
2
+ title : " Build clients "
3
3
description :
4
4
" Build a conversational CLI agent from scratch using AG-UI and Mastra"
5
5
---
@@ -127,7 +127,7 @@ pnpm add @ag-ui/client @ag-ui/core @ag-ui/mastra
127
127
pnpm add @mastra/core @mastra/memory @mastra/libsql
128
128
129
129
# AI SDK and utilities
130
- pnpm add @ai-sdk/openai
130
+ pnpm add @ai-sdk/openai zod@^3.25
131
131
```
132
132
133
133
## Step 3 – Create your agent
@@ -304,14 +304,6 @@ Let's break down what happens when you send a message:
304
304
Now that you have a working chat interface, let's add some real-world
305
305
capabilities by creating tools. We'll start with a weather tool.
306
306
307
- ### Install additional dependencies
308
-
309
- First, install the packages we need for tools:
310
-
311
- ``` bash
312
- pnpm add zod
313
- ```
314
-
315
307
### Create your first tool
316
308
317
309
Let's create a weather tool that your agent can use. Create the directory
@@ -445,36 +437,15 @@ function getWeatherCondition(code: number): string {
445
437
Now let's update our agent to use the weather tool. Update ` src/agent.ts ` :
446
438
447
439
``` typescript
448
- import { openai } from " @ai-sdk/openai"
449
- import { Agent } from " @mastra/core/agent"
450
- import { MastraAgent } from " @ag-ui/mastra"
451
- import { Memory } from " @mastra/memory"
452
- import { LibSQLStore } from " @mastra/libsql"
453
- import { weatherTool } from " ./tools/weather.tool"
440
+ import { weatherTool } from " ./tools/weather.tool" // <--- Import the tool
454
441
455
442
export const agent = new MastraAgent ({
456
443
agent: new Agent ({
457
- name: " AG-UI Assistant" ,
458
- instructions: `
459
- You are a helpful AI assistant with weather capabilities.
444
+ // ...
460
445
461
- When helping users get weather details:
462
- - Always ask for a location if none is provided
463
- - If the location name isn't in English, translate it to English
464
- - For locations with multiple parts (e.g. "New York, NY"), use the most specific part
465
- - Include relevant details like temperature, humidity, wind conditions
466
- - Keep responses conversational and informative
467
- - Always use the weatherTool to fetch current weather data
446
+ tools: { weatherTool }, // <--- Add the tool to the agent
468
447
469
- For other questions, be friendly and helpful!
470
- ` ,
471
- model: openai (" gpt-4o" ),
472
- tools: { weatherTool },
473
- memory: new Memory ({
474
- storage: new LibSQLStore ({
475
- url: " file:./assistant.db" ,
476
- }),
477
- }),
448
+ // ...
478
449
}),
479
450
threadId: " main-conversation" ,
480
451
})
@@ -486,21 +457,27 @@ Update your CLI interface in `src/index.ts` to handle tool events:
486
457
487
458
``` typescript
488
459
// Add these new event handlers to your agent.runAgent call:
489
- onToolCallStartEvent ({ event }) {
490
- console .log (` 🔧 Using ${event .toolCallName }... ` )
491
- },
492
- onToolCallArgsEvent ({ event }) {
493
- // Show tool arguments being built (optional)
494
- process .stdout .write (event .delta )
495
- },
496
- onToolCallEndEvent () {
497
- console .log (" " )
498
- },
499
- onToolCallResultEvent ({ event }) {
500
- if (event .content ) {
501
- console .log (" ✅ Tool completed successfully" )
502
- }
503
- },
460
+ await agent .runAgent (
461
+ {}, // No additional configuration needed
462
+ {
463
+ // ... existing event handlers ...
464
+
465
+ onToolCallStartEvent({ event }) {
466
+ console .log (" 🔧 Tool call:" , event .toolCallName )
467
+ },
468
+ onToolCallArgsEvent({ event }) {
469
+ process .stdout .write (event .delta )
470
+ },
471
+ onToolCallEndEvent() {
472
+ console .log (" " )
473
+ },
474
+ onToolCallResultEvent({ event }) {
475
+ if (event .content ) {
476
+ console .log (" 🔍 Tool call result:" , event .content )
477
+ }
478
+ },
479
+ }
480
+ )
504
481
```
505
482
506
483
### Test your weather tool
@@ -610,7 +587,7 @@ me the weather website".
610
587
611
588
## Step 9 – Deploy your client
612
589
613
- ### Build for production
590
+ ### Building your client
614
591
615
592
Create a production build:
616
593
0 commit comments