Practical MCP with FastMCP & LangChain — book cover

Practical MCP with FastMCP & LangChain

Engineering the Agentic Experience

Available as: Online course

What You'll Learn

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

  1. Practical MCP with FastMCP and LangChain
    • What You'll Learn
    • About the Author
    • Join the Community
    • Your Feedback Matters
  2. How to Use This Guide
    • The Companion Kit
    • Heredoc
    • Long Commands
  3. 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
  4. 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
  5. 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
  6. 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
  7. How MCP Talks: JSON-RPC, Transports, and Message Types
    • The Standard MCP Communication Protocol
    • The JSON-RPC Message Structure
  8. 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
  9. 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
  10. Your First MCP Server: A Working Calculator in Python
    • Installing the Official MCP SDK
    • Prototyping an MCP Server
    • Testing the Server
    • The MCP Inspector
  11. 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
  12. 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
  13. 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
  14. FastMCP in Depth: Building "The Puppy Guide" MCP
    • Preparing the Development Environment
    • The FastMCP Class
  15. 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
  16. Async vs Sync Tools: When Each One Wins
    • Async vs Sync Tools: When Each One Wins
  17. 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
  18. The Context Object: Making Your Tools Session-Aware
    • What is the MCP Context?
    • How Do You Get a Context Object?
  19. 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?
  20. 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
  21. 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
  22. Keeping Clients Informed: Server-Side Progress Reporting
    • How to Report Progress
  23. Borrowing the Client's LLM: Model Sampling in FastMCP
    • How FastMCP Implements Sampling
    • Implementing Sampling with FastMCP
  24. 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
  25. Setup Once, Share Everywhere: The Server Lifespan
    • The Problem with Per-Request Setup
    • What a Lifespan Does
    • Defining a Lifespan
    • Lifespan vs Session State
  26. 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
  27. FastMCP's Layered Architecture: From Wire to Tool
    • The Big Picture
    • Why This Matters
  28. Building an Advanced Netflix MCP: Introduction and Setup
    • FastMCP Netflix Server-Client Architecture
    • Project Data Stores
    • Coding Requirements
  29. 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
  30. Building an Advanced Netflix MCP: Client Implementation Guide
    • Main Client: `client/main.py`
    • Handlers
  31. 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
  32. Integrating Agents with MCP: Introduction to LangChain
    • What is LangChain?
    • Building a LangChain Agent
  33. Integrating Agents with MCP: Function Calling Agents
    • Building a Function Calling Agent
  34. 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
  35. 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
  36. Deploying FastMCP in Production
    • Gateway and Proxy Architectures
    • Running FastMCP in Production
    • Infra-Level Orchestration & Scalability
  37. 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.

More by Aymen El Amri