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
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
Create a virtual environment.
python -m venv .venv
Activate it.
Windows
.venv\Scripts\activate
macOS/Linux
source .venv/bin/activate
Install the MCP SDK.
pip install mcp
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()
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")- Creates an MCP server.@mcp.tool()- Registers a Python function as an MCP tool.def get_weather(city: str):- Defines the tool that Claude can call.mcp.run()- 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"]
}
}
}
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
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
With MCP:
Question
β
LLM calls tool
β
Gets real data
β
Returns reliable answer
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) - File Reader -
read_file(filename) - SQL Database -
query_database(sql_query) - Email Sender -
send_email() - Document Search -
search_documents(question)
The AI chooses which tool to call based on the user's request.
Common Beginner Mistakes
- Expecting every prompt to use a tool - AI models only call tools when they determine a tool is necessary.
- Returning unstructured text - Whenever possible, return structured data such as JSON. Structured responses are easier for language models to understand.
- 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.
- 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!
Comments
No comments yet. Start the discussion.