Tutorial: Build a MCP Tool to Empower AI Agents with Dynamic Data
Luciano Ayres
Global Software Engineering Manager @ AB InBev | Creator of Chatty AI | Author of Agentic Software Engineering for Leaders Handbook | AWS & Azure Certified
Imagine supercharging your AI agent by equipping it with a custom tool that fetches real-time data—like a crypto ticker that shows you the latest prices. In this guide, we’ll walk you through creating an MCP tool using a crypto ticker as our example. We’ll explain the code step-by-step, cover installation and setup instructions, and show you how to integrate the tool with Cursor AI so that your AI agent can access dynamic data effortlessly.
For more details on MCP, check out the Model Context Protocol documentation.
You can also explore the complete project on GitHub.
What Is MCP and Why It Matters for AI Agents
Model Context Protocol (MCP) is a framework that allows you to integrate custom tools directly into AI-powered development environments—such as Cursor AI. With MCP, your AI agent can be extended with capabilities like live data retrieval, code analysis, and much more—all activated by simple natural language commands. Our crypto ticker tool demonstrates how to load dynamic data (like real-time cryptocurrency prices) and serves as a springboard for building your own tools.
The Example: A Dynamic Crypto Ticker Tool
Our tool uses the Coinpaprika API to fetch live cryptocurrency data. When you ask your AI agent, “get the bitcoin price,” the tool kicks into action to retrieve the current USD price and the 24-hour change for Bitcoin. Although this example uses crypto data, the same approach can be adapted to any external data source you wish to integrate.
Installation and Setup
Prerequisites
Step 1: Clone the Repository
Clone the project repository to your local machine:
git clone <repository_url>
cd <repository_directory>
Step 2: Install Dependencies
Install the necessary dependencies by running:
npm install @modelcontextprotocol/sdk @coinpaprika/api-nodejs-client zod
Step 3: Enable ES Modules
Ensure your project supports ES modules by editing your package.json to include:
{
"type": "module"
}
Code Walkthrough: Understanding the MCP Crypto Ticker Tool
Let’s break down the code that builds our MCP tool. The following sections explain each part of the tool’s implementation.
1. Importing Required Modules
At the top of our JavaScript file (e.g., mcp-crypto-ticker.js), we import the necessary modules:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import CoinpaprikaAPI from "@coinpaprika/api-nodejs-client";
2. Initializing the MCP Server
Create an instance of McpServer to initialize your tool:
const server = new McpServer({
name: "MCP Crypto Ticker Tool",
version: "1.0.0",
});
This setup assigns a name and version to your tool, making it easily identifiable when integrated with environments like Cursor AI.
3. Creating the API Client
Instantiate the Coinpaprika API client:
const client = new CoinpaprikaAPI();
This client is used later to fetch coin and ticker data from the API.
4. Defining the getTicker Tool
The core of our MCP tool is the getTicker function. It accepts a coin name, validates it, fetches the required data, and returns the formatted output:
server.tool(
"getTicker",
"Get the ticker for a given coin by its full name (e.g. 'bitcoin')",
{
coin: z.string().describe("The full name of the coin").default("bitcoin"),
},
async ({ coin }) => {
try {
// Fetch the list of coins from the Coinpaprika API
const coins = await client.getCoins();
const coinLower = coin.toLowerCase();
// Find the coin matching the provided name (case-insensitive)
const matched = coins.find((c) => c.name.toLowerCase() === coinLower);
if (!matched) {
return {
content: [
{
type: "text",
text: `Coin "${coin}" not found.`,
},
],
};
}
// Retrieve ticker data for the matched coin using USD as the quote currency
const tickers = await client.getAllTickers({
coinId: matched.id,
quotes: ["USD"],
});
const ticker = Array.isArray(tickers) ? tickers[0] : tickers;
if (!ticker || !ticker.quotes?.USD) {
return {
content: [
{
type: "text",
text: `No ticker data returned for coin "${coin}" (${matched.id}).`,
},
],
};
}
// Extract price and 24-hour percentage change
const price = ticker.quotes.USD.price;
const change = ticker.quotes.USD.percent_change_24h;
// Format the output message
const output = `Ticker for ${ticker.name} (${ticker.symbol}):
Price: $${price}
24h Change: ${change}%`;
return {
content: [
{
type: "text",
text: output,
},
],
};
} catch (error) {
// Handle errors gracefully by returning an error message
return {
content: [
{
type: "text",
text: `Error fetching ticker: ${error.message}`,
},
],
};
}
}
);
Explanation of Key Parts:
5. Setting Up Standard I/O Communication
Finally, establish a connection between the MCP server and your environment:
const transport = new StdioServerTransport();
await server.connect(transport);
This connects your tool to Cursor AI (or any other compatible environment) so it can receive commands and send back responses.
Integrating Your MCP Tool with Cursor AI
This project includes a ./cursor subdirectory that contains an mcp.json file for configuring the MCP server. Cursor AI uses this file to automatically discover and launch your MCP server. Open the file and update the fields as follows:
{
"mcpServers": {
"MCP Crypto Ticker Server": {
"command": "/path/tonode",
"args": ["/path/to/mcp-crypto-ticker/mcp-crypto-ticker.js"]
}
}
}
An object mapping server names to their configuration.
This is the key for your server configuration. You can name it as you like.
Specifies the absolute path to your Node.js executable. For example:
/home/john/.nvm/versions/node/v20.13.1/bin/node
An array containing the absolute path to your MCP server file. For example:["/home/john/mcp-server-boilerplate/index.js"]
Optional: Global Configuration
If desired, you can move the mcp.json file from the ./cursor subdirectory to your global Cursor AI configuration directory located at ~/.cursor. This allows Cursor AI to recognize your MCP server configuration globally.
Using the MCP Tool in Cursor Composer (Agent Mode)
With your MCP server integrated into Cursor AI and Agent mode enabled in Cursor Composer, you can now invoke the tool using natural language prompts. For example:
get the bitcoin price
The AI agent will detect the available getTicker tool from your MCP server and execute it to retrieve the current ticker data for Bitcoin.
More MCP Tool Ideas for Enhancing Software Development
The crypto ticker tool is just one example of what you can build with MCP. Here are some additional ideas to spark your creativity:
For additional inspiration and detailed examples, check out other MCP projects and resources available online.
Final Thoughts
This guide has walked you through building an MCP tool using a crypto ticker as an example. We explained the code in detail, covered installation and setup, and demonstrated how to integrate the tool with Cursor AI. With MCP, you can extend your AI agent’s capabilities to access and process live data, making your development environment smarter and more responsive.
Happy coding, and enjoy building tools that bring your AI agents to life!