📘Introduction

Welcome to this hands-on FastMCP tutorial! In this guide, we'll walk you step-by-step through creating a FastMCP Client to interact with a FastMCP Server. By the end, you’ll understand how to connect a client, call server tools, and handle responses asynchronously.

✅ Prerequisites

Before you begin, make sure you have the following:

🐍☑️ Installed Python
📦☑️ Installed Pip
🌐☑️ Created and activated a virtual environment (venv)
⚡☑️ FastMCP installed inside your venv
⚙️☑️ Created a FastMCP Server instance
🛠️☑️ At least one tool added to your FastMCP Server

💡What is a Client in FastMCP?

The Client class in FastMCP lets you connect to a running MCP Server and call its registered tools asynchronously. It handles communication with the server, sends input parameters to tools, and returns the results. This makes it easy to interact programmatically with server-side functions from your Python code.

🔌 Example FastMCP Server

We've already created a FastMCP Server instance called Deep Learning Nerds MCP Server 📘 and added a tool greet_user() to the MCP Server. Our FastMCP Server should be able to greet a user by name and return a personalized welcome message from the Deep Learning Nerds Academy.

📥1️⃣ Import libraries

Start by importing the required Python modules:

import asyncio
from fastmcp import Client

⚙️2️⃣ Create FastMCP Client

Initialize a client instance. The file mcp_server.py is the target server file the client will connect to:

client = Client("mcp_server.py")

🛠️3️⃣ Define an async function to call a tool

Define an async function to call the greet_user tool:

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In