How to Build Your First AI Agent with MCP Tools: A Hands-On Tutorial

What You Will Build

In this tutorial, you will build a working AI agent with MCP tools that can read data from an API, process it, and take actions. By the end, you will have a Node.js-based agent that connects to external services through the Model Context Protocol — the same architecture powering production AI systems in 2026.

No machine learning experience required. If you can write JavaScript, you can build an AI agent.

Prerequisites

  • Node.js 18+ installed
  • Basic JavaScript/TypeScript knowledge
  • An API key from Anthropic (Claude), OpenAI, or Google (Gemini)
  • 15-30 minutes of focused time

Step 1: Understand the Architecture

An MCP-based AI agent has three components:

  • The LLM — The brain that decides what to do (Claude, GPT, Gemini)
  • MCP Servers — Tools the agent can use (APIs, databases, file systems)
  • The Agent Loop — Your code that connects the LLM to the tools and manages the conversation

The flow is simple: User asks a question → LLM decides which tool to call → Your code executes the tool → Result goes back to the LLM → LLM formulates the response.

Step 2: Create Your MCP Server

Start by building a simple MCP server that provides weather data. Install the SDK:

  • Run npm init -y and npm install @modelcontextprotocol/sdk zod
  • Create a file called weather-server.js
  • Import McpServer from the SDK and z from Zod for input validation
  • Define a tool called get_weather that takes a city name and returns weather data
  • Connect the server using StdioServerTransport

The tool handler is where your business logic lives. It can call any API, query any database, or perform any computation. The MCP SDK handles all the protocol details.

Step 3: Build the Agent Loop

The agent loop is the core of your application. Here is the logic:

  • Send the user message to the LLM along with descriptions of available tools
  • Check if the LLM wants to call a tool — if yes, execute it and send the result back
  • Repeat until the LLM has a final response for the user
  • Handle errors gracefully — tool calls can fail, and your agent should recover

This loop is what makes it an “agent” rather than a simple chatbot. The LLM can chain multiple tool calls, use the output of one tool as input to another, and reason about the results before responding.

Step 4: Add More Tools

The power of MCP is composability. Add more servers to give your agent new capabilities:

  • Database server — Query MongoDB or PostgreSQL from natural language
  • Email server — Send emails and read inbox
  • GitHub server — Read repositories, create issues, review pull requests
  • Slack server — Post messages and read channels

Each server is independent. You can mix and match them, share them with other developers, and use community-built servers from the growing MCP ecosystem.

Step 5: Add Memory

A stateless agent forgets everything between conversations. To make your agent truly useful, add memory:

  • Short-term memory — Keep the conversation history in an array and send it with each LLM call
  • Long-term memory — Store important facts in a vector database (like Pinecone or ChromaDB) and retrieve relevant context for each conversation
  • Preference memory — Track user preferences and adjust behavior over time

Common Mistakes to Avoid

  • Too many tools — Start with 3-5 tools. Too many confuse the LLM and increase latency
  • Vague tool descriptions — The LLM decides which tool to call based on descriptions. Be specific and include examples
  • No error handling — Tools fail. APIs go down. Always handle errors and give the LLM a chance to retry or try a different approach
  • Ignoring cost — Each LLM call costs money. Cache results, minimize unnecessary tool calls, and use cheaper models for simple decisions

Take It Further at a Hackathon

You now have the foundation to build any AI agent. The next step is to build something real — and hackathons are the best forcing function. The StepOne AI Engine Buildathon on Reskilll is live right now, and MCP-based agents are exactly the kind of project that wins.

With 7M+ innovators across 2,000+ hackathons, Reskilll is where developers turn tutorials into real projects. Get matched with teammates, find mentors on MentorVerse, and explore AI workshops on Reskilll Events.

You have the tutorial. Now build the agent. Start on Reskilll today.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top