DEV Community

Cover image for Building Your First AI Agent with MCP: A Step-by-Step Guide
Sushyam Nagallapati
Sushyam Nagallapati

Posted on

Building Your First AI Agent with MCP: A Step-by-Step Guide

If you've heard about the Model Context Protocol (MCP) but aren't sure how to build something with it, this guide is for you.

In my previous article, I explained what MCP is and why it matters. If you haven't read it yet, I recommend starting there first.

Understanding the concepts is one thing. Building an AI agent that actually uses MCP is another.

In this guide, you'll build a simple AI agent that communicates with an MCP server, uses external tools, and returns useful responses. More importantly, you'll understand why each component exists and how they work together.

By the end, you'll have a solid foundation that you can extend into more advanced AI applications.


What You'll Build

Imagine asking an AI assistant:

"What's the weather in Toronto today?"

Instead of guessing the answer, the AI contacts a weather tool, retrieves real information, and responds naturally.

That entire interaction is made possible through the Model Context Protocol.

Our simple AI agent will:

  • Receive a user's question
  • Decide whether a tool is required
  • Call an MCP server
  • Receive structured data
  • Generate a final response

Although we'll use a weather example, this same architecture is used for:

  • AI coding assistants
  • Customer support agents
  • Document search applications
  • Database assistants
  • Internal company chatbots

Prerequisites

Before getting started, make sure you have:

  • Python 3.11 or later
  • Claude Desktop
  • Visual Studio Code (recommended)
  • Basic Python knowledge

We'll also use the official MCP Python SDK.


Understanding the Architecture

Before writing code, it's helpful to understand how requests flow through an MCP application.

┌──────────┐
│   User   │
└────┬─────┘
     │
     ▼
┌───────────────┐
│ Claude Desktop│
└────┬──────────┘
     │
     ▼
┌───────────────┐
│  MCP Client   │
└────┬──────────┘
     │
     ▼
┌───────────────┐
│  MCP Server   │
└────┬──────────┘
     │
     ▼
┌───────────────┐
│ Custom Tool   │
└────┬──────────┘
     │
     ▼
 Structured Data
     │
     ▼
 Claude generates
 natural response
     │
     ▼
     User
Enter fullscreen mode Exit fullscreen mode

Each component has a specific responsibility.

Component Responsibility
User Asks a question
Claude Understands the request
MCP Client Sends tool requests
MCP Server Exposes available tools
Tool Performs the requested task
Claude Generates the final response

Step 1: Create a Project

Create a new project folder.

mkdir weather-agent
cd weather-agent
Enter fullscreen mode Exit fullscreen mode

Create a virtual environment.

python -m venv .venv
Enter fullscreen mode Exit fullscreen mode

Activate it.

Windows

.venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

macOS/Linux

source .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Install the MCP SDK.

pip install mcp
Enter fullscreen mode Exit fullscreen mode

Step 2: Build Your First MCP Server

Every MCP server exposes one or more tools.

A tool is simply a function that AI models can call whenever they need information or need to perform an action.

Examples include:

  • Weather lookup
  • Calculator
  • File reader
  • SQL database query
  • Email sender

Let's build a weather tool.

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Weather Server")

@mcp.tool()
def get_weather(city: str):
    return f"The weather in {city} is sunny and 24°C."

if __name__ == "__main__":
    mcp.run()
Enter fullscreen mode Exit fullscreen mode

Although this example returns hardcoded data, the same structure works with real APIs.


Step 3: Understanding the Code

Let's break down what happened.

mcp = FastMCP("Weather Server")
Enter fullscreen mode Exit fullscreen mode

Creates an MCP server.

@mcp.tool()
Enter fullscreen mode Exit fullscreen mode

Registers a Python function as an MCP tool.

def get_weather(city: str):
Enter fullscreen mode Exit fullscreen mode

Defines the tool that Claude can call.

mcp.run()
Enter fullscreen mode Exit fullscreen mode

Starts the MCP server.

Once the server is running, Claude automatically discovers every registered tool.

You never explicitly tell Claude when to use the tool.

Claude decides that based on the user's request.


Step 4: Connect Claude Desktop

Claude Desktop needs to know where your MCP server is running.

Update your MCP configuration.

{
  "mcpServers": {
    "weather": {
      "command": "python",
      "args": [
        "/path/to/weather_server.py"
      ]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Restart Claude Desktop.

If everything is configured correctly, Claude will automatically discover your new weather tool.


Step 5: Test Your Agent

Now ask Claude:

What's the weather in Toronto today?

Behind the scenes, this workflow takes place.

User asks question
        │
        ▼
Claude understands request
        │
        ▼
Needs external data?
        │
       Yes
        │
        ▼
Calls Weather Tool
        │
        ▼
Receives result
        │
        ▼
Writes natural response
        │
        ▼
Returns answer
Enter fullscreen mode Exit fullscreen mode

Notice something important.

Claude isn't writing Python code.

It's deciding when a tool should be used.

That decision-making process is what makes AI agents so powerful.


Why MCP Matters

Without MCP:

Question

↓

LLM guesses

↓

Possible hallucination
Enter fullscreen mode Exit fullscreen mode

With MCP:

Question

↓

LLM calls tool

↓

Gets real data

↓

Returns reliable answer
Enter fullscreen mode Exit fullscreen mode

Instead of relying only on its training data, the model can interact with external systems whenever necessary.


Expanding Your Agent

Once you've built one tool, adding more is straightforward.

For example:

Calculator

calculate(expression)
Enter fullscreen mode Exit fullscreen mode

File Reader

read_file(filename)
Enter fullscreen mode Exit fullscreen mode

SQL Database

query_database(sql_query)
Enter fullscreen mode Exit fullscreen mode

Email Sender

send_email()
Enter fullscreen mode Exit fullscreen mode

Document Search

search_documents(question)
Enter fullscreen mode Exit fullscreen mode

The AI chooses which tool to call based on the user's request.


Common Beginner Mistakes

1. Expecting every prompt to use a tool

AI models only call tools when they determine a tool is necessary.


2. Returning unstructured text

Whenever possible, return structured data such as JSON.

Structured responses are easier for language models to understand.


3. Building large tools

A single tool should perform one clear task.

Smaller tools are easier to maintain and easier for AI models to use correctly.


4. Ignoring error handling

Validate inputs and return meaningful error messages.

Reliable tools lead to reliable AI applications.


Next Steps

Now that you've built a simple MCP server, try extending it with real-world integrations.

Some ideas include:

  • Connect a real weather API
  • Search local documents
  • Query a PostgreSQL database
  • Build a GitHub assistant
  • Connect Google Calendar
  • Build a file management assistant
  • Create a multi-agent system with LangGraph

Each project builds on the same MCP foundation you've learned here.


Final Thoughts

Building your first MCP server is more than just another Python project.

It introduces a practical pattern for connecting language models with real tools and real data.

Instead of expecting an AI model to know everything, you allow it to discover and use specialized tools whenever they're needed.

As AI applications continue to evolve, protocols like MCP will become an important part of modern software development. Learning these concepts now will prepare you to build assistants that can search documents, interact with APIs, query databases, automate workflows, and solve real-world problems.

Start with one tool.

Then add another.

Before long, you'll have an AI agent capable of handling tasks that go far beyond simple conversation.


Thanks for Reading

If you found this guide helpful, consider following me for more articles on AI Engineering, MCP, LangGraph, RAG, FastAPI, and Full Stack development.

🔗 LinkedIn: https://www.linkedin.com/in/sushyamnagallapati/

Happy building!

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.