MCP FastAPI Tutorial Server

MCP FastAPI Tutorial Server

A basic MCP server demonstrating tool registration and SSE transport, enabling AI clients to call greeting, arithmetic, and time tools.

Category
Visit Server

README

MCP Server with FastAPI (Python)

A hands-on implementation of a Model Context Protocol (MCP) server built with Python using the official MCP SDK and FastAPI/Starlette.

What is MCP?

Model Context Protocol (MCP) is an open standard that enables AI assistants (like Claude, Kiro, Cursor) to connect with external tools, data sources, and APIs through a unified interface — similar to how USB-C standardized device connectivity.

┌─────────────┐         ┌─────────────┐         ┌──────────────┐
│  AI Client  │  ←───→  │  MCP Server │  ←───→  │  Your Tools/ │
│(Claude/Kiro)│  JSON   │  (This Repo)│         │  Data/APIs   │
└─────────────┘         └─────────────┘         └──────────────┘

Features

  • Tools: Functions that AI clients can discover and call
    • greet — Returns a greeting for a given name
    • add_numbers — Adds two numbers and returns the result
    • get_current_time — Returns the current date and time
  • SSE Transport: Server-Sent Events based communication (HTTP)
  • Test Client: A Python client that connects to the server and calls all tools

Tech Stack

  • Python 3.13+
  • MCP Python SDK (v1.9)
  • FastAPI / Starlette
  • Uvicorn (ASGI server)
  • SSE (Server-Sent Events) transport

Getting Started

Prerequisites

  • Python 3.10+
  • pip

Installation

git clone https://github.com/MuhammadIshaqSkd/mcp-fastapi-tutorial.git
cd mcp-fastapi-tutorial

# Create virtual environment
python -m venv venv

# Activate (Windows)
.\venv\Scripts\activate

# Activate (macOS/Linux)
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

Running the Server

python server.py

Server starts on http://127.0.0.1:8080 with SSE endpoint at /sse.

Testing with the Client

Open a second terminal (keep the server running):

python test_client.py

Expected output:

==================================================
MCP Client - Connecting to server...
==================================================

✅ Connection successful!

📋 Available Tools:
------------------------------
  🔧 greet: Greet someone by name and return a greeting message.
  🔧 add_numbers: Return the sum of two numbers.
  🔧 get_current_time: Return the current date and time.

🎯 Calling 'greet' tool...
  Result: Assalam-o-Alaikum, Mishaq! MCP Server se aapko salam!

🎯 Calling 'add_numbers' tool...
  Result: 15 + 27 = 42

🎯 Calling 'get_current_time' tool...
  Result: Current time: 2026-06-22 18:15:09

==================================================
🎉 All tools called successfully!
==================================================

Project Structure

mcp-fastapi-tutorial/
├── server.py           # MCP Server — registers and exposes tools via SSE
├── test_client.py      # MCP Client — connects to server, lists & calls tools
├── requirements.txt    # Python dependencies
└── README.md

How It Works

  1. Server registers tools using @mcp.tool() decorator
  2. Client connects via SSE to /sse endpoint
  3. Client calls list_tools() to discover available tools
  4. Client calls call_tool("tool_name", {args}) to execute a tool
  5. Server executes the function and returns the result

Key Concepts Demonstrated

Concept Description
Tool Registration Using @mcp.tool() to expose Python functions as MCP tools
SSE Transport HTTP-based bidirectional communication via Server-Sent Events
Client Session Initializing and managing MCP client-server sessions
Tool Discovery Clients dynamically discover available tools at runtime
Tool Execution Remote procedure call pattern over the MCP protocol

Roadmap

  • [x] Basic MCP server with tools
  • [ ] Add Resources (context/data providers for AI)
  • [ ] Add Prompts (reusable prompt templates)
  • [ ] Streamable HTTP transport
  • [ ] Authentication & authorization
  • [ ] Deploy to cloud

Resources

License

MIT

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured