Practical MCP with FastMCP & LangChain
Engineering the Agentic Experience
Available as: Online course
What You'll Learn
- Trace a request from prompt to answer, including how the model picks a tool
- Build an MCP server and client from scratch, then connect them to Claude, Cursor or GitHub Copilot
- Design tools a model can read: docstrings, type hints, Pydantic validation, structured output
- Write error messages a model can recover from, with internal details masked
- Handle approval flows, progress reporting and session state across tool calls
- Deploy stateful or stateless, on one container or on Kubernetes with Redis-backed state
About This Book
The Model Context Protocol replaces the M-by-N integration problem — every model wired to every tool by hand — with a single protocol. This course starts at the wire level with JSON-RPC messages, transports and capability negotiation, then builds MCP servers that expose real capabilities and clients that run full multi-turn conversations against them.
It untangles the four things teams routinely confuse: RAG, function calling, MCP tools and MCP resources. From there it works through the interaction patterns the protocol supports — human-in-the-loop approval, progress updates from long-running tools, server-side sampling, structured logging and session state that survives across tool calls — then connects servers to LangChain agents and deploys them stateful or stateless, on a single container or on Kubernetes with Redis-backed state. The capstone is a production-grade analytics system backed by PostgreSQL and Redis.
Table of Contents
-
Practical MCP with FastMCP and LangChain
- What You'll Learn
- About the Author
- Join the Community
- Your Feedback Matters
-
How to Use This Guide
- The Companion Kit
- Heredoc
- Long Commands
-
Introduction to MCP: One Protocol Instead of a Hundred Integrations
- The USB-C of AI Integrations
- Why MCP Matters
- Setting Up an MCP Environment for Learning
-
MCP Architecture and Workflow: How Host, Client, and Server Fit Together
- The MCP Host
- The MCP Client
- The MCP Server
- Understanding How Everything Fits Together
-
Primitives, Capabilities, and Utilities: The Full MCP Feature Set and When to Use Each
- Tools (Server Side)
- Resources (Server Side)
- Prompts (Server Side)
- Logging (Server Side)
- Pagination (Server Side)
- Completions (Server Side)
- Sampling (Client Side)
- Elicitation (Client Side)
- Roots (Client Side)
- Cancellation (Client & Server Side)
- Ping (Client & Server Side)
- Progress (Client & Server Side)
- Tasks (Client & Server Side)
- Primitives vs Capabilities vs Utilities
-
Four Concepts People Confuse: RAG, Function Calling, MCP Tools, and MCP Resources
- Function Calling vs MCP Tools
- Retrieval Augmented Generation vs Model Context Protocol Resources
-
How MCP Talks: JSON-RPC, Transports, and Message Types
- The Standard MCP Communication Protocol
- The JSON-RPC Message Structure
-
Following One MCP Request From Prompt to Answer
- Step 1 - Startup, Configuration and Preparation
- Step 2 - Initialization, Handshake and Capability Negotiation
- Step 3 - Discovery and Introspection
- Step 4 - Reasoning, Decision-Making and Planning
- Step 5 - Tool Calling, Execution and Structured Output
- Step 6 - Integration: Composing the Final Answer
- Step 7 - The Feedback Loop
- Post-Processing & Application Logic
- How The AI Agent Selects and Uses MCP Tools
-
Prerequisites & Setup for the Development Environment
- The Operating System
- Python: The De Facto Language for AI/ML
- UV: The Fast Python Packaging Tool
- Pyproject: The Modern Python Project
- NPM and Node.js
-
Your First MCP Server: A Working Calculator in Python
- Installing the Official MCP SDK
- Prototyping an MCP Server
- Testing the Server
- The MCP Inspector
-
Build an MCP Client: Wiring an LLM to Your Server
- Overview of Function Calling
- Prototyping an MCP Client
- Understanding How the MCP Client Works
- Testing the MCP Client
- OpenAI as MCP Orchestrator
- Connecting Other Applications to our MCP Server
- Connecting GitHub Copilot to our MCP Server
- Connecting Claude Code to our MCP Server
- What's Next
-
MCP vs FastMCP: Untangling the Names That Confuse Everyone
- MCP vs FastMCP - What Is the Difference?
- What Is FastMCP Then?
- What Changed With FastMCP?
- Why We Are Moving to FastMCP
-
Getting Started with FastMCP: From Install to Your First Server
- Installation
- Creating Your First FastMCP Server
- Running the Development Server
- FastMCP JSON Configurations
- FastMCP with MCP Inspector
- Testing the Server with a FastMCP Client
-
FastMCP in Depth: Building "The Puppy Guide" MCP
- Preparing the Development Environment
- The FastMCP Class
-
Shaping Tools the LLM Understands: Types, Docstrings, and Validation
- The Importance of Docstrings
- Parameter Types and Return Types
- Additional Customization Parameters
- FastMCP Annotations
- Type Coercion and Validation
- Better Control with Pydantic
- Hiding Parameters from the LLM
- Putting Everything Together
-
Async vs Sync Tools: When Each One Wins
- Async vs Sync Tools: When Each One Wins
-
Error Handling: Writing Messages the LLM Can Actually Use
- Exception Types
- What actually happens at the protocol level
- MCP Clients vs LLMs: Who sees the error?
- So what does the LLM actually understand?
- Masking Internal Error Details
- Resource and Prompt Error Handling
- Putting Everything Together
-
The Context Object: Making Your Tools Session-Aware
- What is the MCP Context?
- How Do You Get a Context Object?
-
Building a Custom MCP Client: An LLM-Powered REPL
- Prepare the Environment
- The Client Code
- Elicitation Handler
- Logging Handler
- Progress Handler
- Sampling Handler
- What's Next?
-
Server-to-Client Logging: Talking to the User While a Tool Runs
- Why MCP Has Its Own Logging
- The Four Log Levels
- Structured Data with the Extra Parameter
- How Clients Handle Log Messages
- Putting It All Together
-
Pausing a Tool to Ask the User: Elicitation in MCP
- The Problem with One-Way Communication
- Why Elicitation Matters
- The Flow of an Elicitation
- The Three Possible Outcomes
- Requirements and Limitations
- Implementing Elicitation
-
Keeping Clients Informed: Server-Side Progress Reporting
- How to Report Progress
-
Borrowing the Client's LLM: Model Sampling in FastMCP
- How FastMCP Implements Sampling
- Implementing Sampling with FastMCP
-
Making Your Server Remember: Session State
- Storing and Retrieving Values
- Session Isolation
- Custom Storage Backends
- How Long State Lasts
- What Values Can Be Stored
- Implementing Session State Using FastMCP
-
Setup Once, Share Everywhere: The Server Lifespan
- The Problem with Per-Request Setup
- What a Lifespan Does
- Defining a Lifespan
- Lifespan vs Session State
-
FastMCP Middleware: Gates Around Every Request
- The Basic Idea
- How to Write One
- Hooks: Choosing the Right Level
- Ordering Matters
- What Middleware Is Good For
-
FastMCP's Layered Architecture: From Wire to Tool
- The Big Picture
- Why This Matters
-
Building an Advanced Netflix MCP: Introduction and Setup
- FastMCP Netflix Server-Client Architecture
- Project Data Stores
- Coding Requirements
-
Building an Advanced Netflix MCP: Server Implementation Guide
- Imports
- Loading Configuration
- Debugging Middleware
- Creating the FastMCP Server
- Attaching Middleware and Registering Components
- Adding Transforms
- Exposing the ASGI Application
- Component Implementation: Tools
- Component Implementation: Resources
- Component Implementation: Prompts
- Database Setup
-
Building an Advanced Netflix MCP: Client Implementation Guide
- Main Client: `client/main.py`
- Handlers
-
Testing the Advanced Netflix MCP: Running the Full Stack End to End
- Running the Server
- Running the Client REPL
- Testing the Workflow
- Limitations of Using "Raw" OpenAI
-
Integrating Agents with MCP: Introduction to LangChain
- What is LangChain?
- Building a LangChain Agent
-
Integrating Agents with MCP: Function Calling Agents
- Building a Function Calling Agent
-
Integrating Agents with MCP: MCP for LangChain Agents
- Connecting MCP to LangChain
- Integrating An Existing MCP With LangChain
- Calling MCP Tools vs. LangChain Functions
- What's next
-
RAG Meets MCP: Building a Question-Answering MCP Agent
- Embeddings
- Cosine Similarity
- Chunking
- Vector Stores
- Top-k Retrieval
- Retrieval-Augmented Generation (RAG)
- Building a PDF Question Answering RAG Agent
- What's next
-
Deploying FastMCP in Production
- Gateway and Proxy Architectures
- Running FastMCP in Production
- Infra-Level Orchestration & Scalability
-
Afterword
- Let's Stay Connected
- Your Feedback Matters
Who This Is For
Python engineers building agents who are done with demos and need the protocol to hold up in production.
About the Author
Aymen El Amri is a software engineer, author and founder. He runs eralabs, built FAUN.dev, and writes about cloud native systems, AI engineering and developer tooling. More about Aymen.