> ## Documentation Index
> Fetch the complete documentation index at: https://docs.atxp.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a simple CLI client

> Follow a step-by-step tutorial to build your first ATXP client from scratch.

## Overview

In this tutorial, you'll build a simple command-line client that uses ATXP to search the web. Starting from an empty directory, you'll learn how to set up a project, install dependencies, configure authentication, and make your first MCP server call.

By the end of this tutorial, you'll have a working CLI that can search the web using the [Search MCP server](/tools/search).

## Prerequisites

* Node.js installed (version 18 or higher recommended)
* An ATXP account with a connection string (learn how to [create an account](/developers/build-agents/create-account))
* A terminal or command prompt

## Step 1: Create your project

First, let's create a new directory for your project and navigate into it:

```bash theme={null}
mkdir my-atxp-cli
cd my-atxp-cli
```

Now initialize a new Node.js project:

```bash theme={null}
npm init -y
```

This creates a `package.json` file with default values.

## Step 2: Install dependencies

Install the ATXP client SDK and dotenv for managing environment variables:

```bash theme={null}
npm install @atxp/client dotenv
```

The `@atxp/client` package provides everything you need to connect to MCP servers and make tool calls. The `dotenv` package helps you securely manage your connection string.

## Step 3: Set up your environment variables

Create a `.env` file in your project directory to store your ATXP connection string:

```bash theme={null}
touch .env
```

Open the `.env` file in your text editor and add your ATXP connection string:

```bash .env theme={null}
ATXP_CONNECTION=https://accounts.atxp.ai?connection_token=<your_token_here>
```

<Info>
  If you don't have an ATXP connection string yet, follow the [account creation guide](/developers/build-agents/create-account) to get one.
</Info>

<Warning>
  Never commit your `.env` file to version control. Add it to your `.gitignore` file:

  ```bash theme={null}
  echo .env >> .gitignore
  ```
</Warning>

## Step 4: Write your CLI script

Create a new file called `index.js`:

```bash theme={null}
touch index.js
```

Now let's build the script step by step. Open `index.js` in your text editor.

### Step 4a: Load dependencies and create an account

Add the imports and create an ATXP account from your connection string:

```javascript index.js theme={null}
const { atxpClient, ATXPAccount } = require('@atxp/client');
require('dotenv/config');

const account = new ATXPAccount(process.env.ATXP_CONNECTION);
```

### Step 4b: Connect to the Search MCP server

Create a client that connects to the Search MCP server:

```javascript index.js theme={null}
const client = await atxpClient({
  mcpServer: 'https://search.mcp.atxp.ai',
  account: account,
});
```

### Step 4c: Call the search tool and display results

Call the search tool and output the results:

```javascript index.js theme={null}
const result = await client.callTool({
  name: 'search_search',
  arguments: { query: 'latest news on artificial intelligence' }
});

const searchResult = JSON.parse(result.content[0].text);
searchResult.results.forEach((item, index) => {
  console.log(`${index + 1}. ${item.title}`);
  console.log(`   ${item.url}\n`);
});
```

### Complete code

Your full `index.js` file should look like this:

```javascript index.js theme={null}
const { atxpClient, ATXPAccount } = require('@atxp/client');
require('dotenv/config');

async function main() {
  const account = new ATXPAccount(process.env.ATXP_CONNECTION);

  const client = await atxpClient({
    mcpServer: 'https://search.mcp.atxp.ai',
    account: account,
  });

  const result = await client.callTool({
    name: 'search_search',
    arguments: { query: 'latest news on artificial intelligence' }
  });

  const searchResult = JSON.parse(result.content[0].text);
  searchResult.results.forEach((item, index) => {
    console.log(`${index + 1}. ${item.title}`);
    console.log(`   ${item.url}\n`);
  });
}

main();
```

## Step 5: Run your CLI

Now you're ready to run your CLI! Execute the following command:

```bash theme={null}
node index.js
```

You should see output similar to this:

```
Creating ATXP client...
Client created successfully!
Searching the web...

Found 10 results:

1. AI Breakthrough: New Language Model Achieves...
   URL: https://example.com/ai-news
   Recent developments in artificial intelligence have led to significant breakthroughs in natural language processing...
   Published: 2 days ago

2. Tech Giants Invest Billions in AI Research
   URL: https://example.com/tech-investment
   Major technology companies announced record investments in artificial intelligence research and development...
   Published: 3 days ago

...
```

<Check>
  Congratulations! You've successfully built a working ATXP client that can search the web using MCP servers.
</Check>

## Understanding the code

Let's break down what your CLI does:

1. **Import dependencies**: You import the ATXP client SDK and load environment variables
2. **Create account**: You create an `ATXPAccount` instance using your connection string
3. **Create client**: You use `atxpClient()` to create a client connected to the Search MCP server
4. **Call tool**: You use `client.callTool()` to invoke the `search_search` tool with a query
5. **Parse results**: You parse the JSON response and display the search results

## Customizing your CLI

Now that you have a working CLI, try customizing it:

### Change the search query

Modify the `query` argument in the `callTool()` call:

```javascript theme={null}
arguments: { query: 'your custom search query here' }
```

### Accept command-line arguments

Make your CLI accept a search query as a command-line argument:

```javascript theme={null}
// Get query from command line or use default
const query = process.argv[2] || 'latest news on artificial intelligence';

const result = await client.callTool({
  name: 'search_search',
  arguments: { query: query }
});
```

Then run it with:

```bash theme={null}
node index.js "your search query"
```

### Try different MCP servers

Explore other MCP servers by changing the `mcpServer` URL. Check out the [MCP Servers documentation](/tools) to see what's available.

## Next steps

<CardGroup cols={2}>
  <Card title="Explore MCP Servers" icon="server" href="/tools">
    Discover other MCP servers you can use in your applications, from image generation to file storage.
  </Card>

  <Card title="Build a paid MCP server" icon="messages-dollar" href="/developers/monetize/tutorial">
    Learn how to build and monetize your own MCP server with ATXP integration.
  </Card>

  <Card title="Client SDK Reference" icon="code" href="/developers/api-reference/client">
    Dive deeper into the ATXP Client SDK documentation and learn about advanced features.
  </Card>
</CardGroup>
