- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- AI
Guided: Build a Simple MCP Server
In this Code Lab, you will build a simple but compliant Model Context Protocol (MCP) server from the ground up using Python and the FastAPI framework. You will learn how to structure MCP messages, handle discovery and execution requests, and securely expose custom tools to AI agents.
Lab Info
Table of Contents
-
Challenge
Introduction to the Model Context Protocol
Welcome to this Code Lab on building a Model Context Protocol (MCP) server!
MCP is a standardized specification that allows AI models and agents to interact with external tools and services in a secure and predictable way.
Imagine an AI chatbot that needs to get the current weather. Instead of having weather-checking logic built into the AI model itself, it can use MCP to ask an external server:
"Do you have a tool for weather? (Discovery)
If the server says yes, the AI can then make a second request:
"Please execute your weather tool for the location 'San Francisco'" (Execution).
In this lab, you will build the server side of this interaction. You'll create a server that can advertise its tools and execute them when requested. info > This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.
-
Challenge
Step 2: Setting Up the Basic Server
First, you need a basic web server to listen for incoming requests.
You will use FastAPI, a modern, high-performance Python web framework. Your server will expose a single endpoint,
/mcp, which will handle all protocol communications via HTTPPOSTrequests. -
Challenge
Step 3: Defining MCP Data Structures
MCP relies on a clear and consistent message structure. You will use Pydantic, a data validation library, to define Python classes that map directly to the JSON structure of MCP requests and responses.
This ensures that any data sent to your server is valid, and any data you send back is correctly formatted. This is the foundation of the MCP Message Structures concept in action.
-
Challenge
Step 4: Implementing Discovery
The first stage of the MCP lifecycle is Tool Registration and Discovery.
In this phase, an AI agent ask your server what tools it can use. You'll implement a handler for the
discoveryverb that responds with a list of all available tools — including their names, descriptions, and required parameters.This allows the agent to learn how to use your tools dynamically.
-
Challenge
Step 5: Implementing Tool Execution
Once an agent has discovered a tool, it can ask your server to run it. This is the Execution phase of the Request/Response Lifecycle.
In this step, you will implement the logic to handle the
executeverb. This involves mapping the requested tool name to the actual Python function, calling it with the arguments provided by the agent, and returning the result in the response. -
Challenge
Step 6: Adding Error Handling
Finally, a production-ready server needs to be robust. You'll add simple error handling to provide a clear response when an agent requests a tool that doesn't exist.
This is a key Server-Side Implementation Pattern that ensures predictable behavior and helps developers debug interactions with the AI agent.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.