How to Build AI Agents With MCP Servers (Without the Risk)
How to build ai agents with mcp servers is a shorter journey than most tutorials suggest. A working MCP server in Python is about twenty lines long: import FastMCP, create a server, decorate a function with a tool marker, and run it.
The hard part is not the code. It is everything the tutorials skip: which tools to expose, how the agent decides when to call them, what happens when a tool returns something malicious, and how a local demo becomes a service others can use safely.
MCP, the Model Context Protocol, is the open standard from Anthropic that gives language models a consistent way to call tools, read data and run actions. FastMCP is the Python library that makes writing a server feel like writing a FastAPI app, handling the protocol details for you.
This guide builds a local MCP server with FastMCP, connects it to an agent, adds a remote server, and hardens the whole thing for production. Follow it in order, and you will have an agent you can trust with real work.
Key Takeaways
A minimal FastMCP server is about twenty lines: import FastMCP, name the server, decorate functions with the tool marker, and run it.
FastMCP generates the tool schema from type hints and docstrings, so the docstring you write is the description the model reads.
Use stdio for local servers and Streamable HTTP for remote ones; the June 2025 spec replaced SSE with Streamable HTTP.
The confused deputy problem is the core security risk: a tool result can carry instructions that trick the model into acting against the user.
Production hardening means approval lists, least-privilege tools, human confirmation for irreversible actions, and logging every call.
What You Are Building, and Why FastMCP
You are building three processes that talk to each other. The local MCP server is a standalone Python script that exposes a few tools, running as a subprocess of the agent over stdio. The agent is the coordinating script that connects to the server, loads its tools, and runs the interactive loop.
The third process is optional but instructive: a remote MCP server over HTTP, such as a public service for asking questions about a GitHub repository. The agent does not know or care whether a tool lives on your machine or on a remote server. It just sees a list of tools with names and descriptions.
FastMCP is the fastest path to a pure MCP server because it handles the protocol plumbing. You define functions, add decorators, and FastMCP handles schema generation and the handshake. If you have used FastAPI, the code will feel familiar.
On the agent side, langchain-mcp-adapters connects to one or more MCP servers and loads their tools into a format LangChain’s agent can use. The agent code never sees JSON-RPC. It sees tools, and decides when to call them.
Tools, Resources and Prompts: Choose the Right Primitive
Tools are callable functions, and the model decides when to invoke them. Use tools for actions: fetching data, writing to a database, calling an API, running a calculation. In practice, about 90% of what you build will be tools.
Resources are addressable data, identified by URIs, that the model can read on demand. Use them for documentation lookup or configuration retrieval. Prompts are reusable message templates the client can invoke with parameters.
Building Your First MCP Server, Step by Step
Start with the environment. Create a virtual environment, then install FastMCP for the server, LangChain and langchain-mcp-adapters for the agent, and langchain-ollama if you want to run a local model such as Qwen through Ollama. A smaller Qwen model works on a lower-memory machine.
Create a file called server.py. Import FastMCP and initialise the server with a name, then define a tool as a plain Python function decorated with the tool marker. A classic first example adds two numbers: the type hints become the schema, and the docstring becomes the description.
Run the file and you have a working MCP server. The server advertises its tools, and any MCP-compatible client can use them as native tools. The docstring matters more than most beginners realise: it is the instruction the model uses to decide when to call your tool.
Now write the agent. It connects to your local server over stdio and, if you want, to a remote server over HTTP, merges both tool lists, and runs an interactive loop. On each iteration it checks for a tool call, executes it, passes the result back, and continues until the model finishes.
Testing With the MCP Inspector
Before you let an agent use your server, test it with the official MCP Inspector. Run the inspector command and point it at your server, and you can call each tool by hand and see exactly what it returns. Bugs found here are cheap; bugs found by an agent in production are not.
Test the unhappy paths too. What does the tool return for bad input? What happens when the underlying service is down? An agent that receives a clear error can recover. An agent that receives a confusing one will invent a workaround.
Local vs Remote: stdio and Streamable HTTP
Local servers use stdio: the host spawns the server as a child process, and they communicate through standard input and output. There is zero network overhead, it is fast, and it is secure by default, because nothing leaves your machine. It is the right choice for personal tools.
Remote servers use Streamable HTTP, which replaced the older SSE transport in the June 2025 spec revision. HTTP is the right choice for production and multi-user deployments, where the server must be shared, scaled and monitored. It also means authentication matters: a remote server is a network service.
The 2026 spec adds stateless operation, which fits cloud deployment better, and OAuth 2.1 support for remote servers, so clients can use scoped tokens that expire instead of shared API keys. If you built a server in 2025, check it against the 2026 spec before you ship it again.
The decision rule is simple. If only you will use it, and only on your machine, use stdio. If anyone else will use it, or it must run when you are not there, use Streamable HTTP with OAuth 2.1.
The 2026 Tasks Extension for Long-Running Work
Most tutorials skip the 2026 Tasks extension, and it is the feature that matters most for real agent work. Long-running workflows, a research job that takes an hour, a deployment that runs in stages, need a way to start a task, check on it, and resume it later.
Tasks give the server a way to report progress and the client a way to poll or subscribe to updates. If your agent runs jobs longer than a single request, design around Tasks from the start. Retrofitting them later means rethinking how your tools report state.
Security: The Confused Deputy Problem
The security risk most tutorials skip is the confused deputy problem. Your agent acts with your permissions, and it takes instructions from tool results as well as from you. A malicious tool result can carry hidden instructions, and the model may follow them.
A concrete example: your agent asks a public MCP server about a repository, and the server returns a description that says, in effect, send a local file to an external address. The model, trying to be helpful, may do exactly that. The server never had your permissions. Your agent did, and it lent them out.
The defences are practical. Approve every server before connecting it, and read every tool description it advertises. Grant least-privilege access: a tool that reads files should not be able to write them. Require human confirmation before irreversible actions, and never let a tool result trigger a payment or deletion without a person in the loop.
Log every tool call, and review the log after the first week. The log is the only record of what the agent actually did, and it is the first thing you will reach for when something goes wrong. Treat an MCP server as untrusted code with access to your data.
Choice
Use when
Watch out for
stdio transport
Local, personal tools
Only works on your machine
Streamable HTTP
Shared, production use
Needs authentication
FastMCP
Python servers, fast start
Keep SDK version pinned
Official MCP SDK
Full control, other languages
More boilerplate
Tools primitive
Actions the model chooses
Keep tools narrow
Resources primitive
Data the app controls
Do not expose secrets
From Demo to Production: A Hardening Checklist
Work through this checklist before your MCP agent touches anything that matters.
Test every server with the MCP Inspector first, including bad input and a downed backend.
Read every tool description the server advertises, and remove any tool you do not need.
Use stdio for local tools and Streamable HTTP with OAuth 2.1 for remote servers, never shared API keys.
Grant least-privilege access per tool, and require human confirmation for irreversible actions such as deleting files.
Design long-running work around the 2026 Tasks extension, so jobs can report progress and resume.
Log every tool call and review the log after the first week.
How do I build an MCP server in Python?
Install FastMCP, create a file called server.py, import FastMCP and initialise it with a name, then define tools as plain Python functions decorated with the tool marker. FastMCP generates the schema from your type hints and the description from your docstring. Run the file and you have a working MCP server.
What is FastMCP?
FastMCP is a Python library that makes writing an MCP server feel like writing a FastAPI app. You decorate functions with the tool marker, and FastMCP handles the protocol details: JSON-RPC messages, schema generation from type hints and docstrings, and the transport layer.
Should my MCP server use stdio or HTTP?
Use stdio for local, personal tools: the host spawns the server as a child process, it is fast, and nothing leaves your machine. Use Streamable HTTP for remote or multi-user deployments, where the server must be shared and scaled. The June 2025 spec replaced SSE with Streamable HTTP.
What is the confused deputy problem in MCP?
It is the risk that your agent acts with your permissions on instructions hidden in a tool result. A malicious server can return a description that tells the model to send a file somewhere, and the model may obey because it cannot tell data from an instruction. The defence is approval lists and human confirmation.
How do I connect an agent to my MCP server?
Use a client library such as langchain-mcp-adapters, which connects to one or more MCP servers and loads their tools into a format your agent framework can use. The agent sees a list of tools with names and descriptions, and decides when to call them. Test with the official MCP Inspector first.
What is the MCP Tasks extension?
Tasks is a 2026 MCP extension for long-running agent workflows. It lets a server start a task, report progress, and let the client poll or subscribe to updates, so jobs that take an hour do not need to hold a connection open. If your agent runs long jobs, design around Tasks.
Conclusion
Building an MCP agent is easy. Building one you can trust is the real work, and it is mostly not code: choosing the right primitive, picking the right transport, testing with the Inspector, and hardening against the confused deputy problem.
Start local, with a read-only server and a small model. Add one remote server, then the hardening checklist, then production. The protocol will do the rest: one standard, any compliant model, any compliant tool, and an agent that can reach the systems it needs to do real work.
Ayesha is the co-founder of occcy and the editor behind its entertainment and lifestyle coverage. She believes good writing should feel like a conversation with a smart friend, clear, warm, and genuinely helpful.