Skip to content

Instantly share code, notes, and snippets.

@vguhesan
Created February 16, 2026 22:26
Show Gist options
  • Select an option

  • Save vguhesan/1842e25e1a78a415d7118d38f7e3e21e to your computer and use it in GitHub Desktop.

Select an option

Save vguhesan/1842e25e1a78a415d7118d38f7e3e21e to your computer and use it in GitHub Desktop.
MCP Server & Client Python Example Using fastMCP Framework

MCP Server & Client Python Example Using fastMCP Framework

In this example, we will be creating an example of a MCP Server & Client Python Example Using fastMCP Framework

fastMCP is a popular Python framework for building MCP servers and clients. It abstracts protocol details, allowing focus on logic via decorators like @tool and @resource. Below are step-by-step examples with full, working code.

MCP Server in Python Using fastMCP

This example creates a simple MCP server with tools for basic math operations and a resource for version info.

  1. Install fastMCP: Run pip install fastmcp (or uv pip install fastmcp for better dependency management).
  2. Create the Server File: Save as mcp_server.py. (see file)
  3. Run the Server: Execute python mcp_server.py. The server will be available at http://127.0.0.1:8000/mcp.
  4. Test: Clients can now discover tools (e.g., add, multiply) and resources (e.g., config://version).

MCP Server in Python Using fastMCP

This example creates a client that connects to the above server, lists tools, and calls one.

  1. Create the Client File: Save as mcp_client.py. (see file)
  2. Run the Client: Ensure the server is running, then execute python mcp_client.py. Output will show tools, the addition result (8), and the version.
# Filename: mcp_client.py
import asyncio
from fastmcp import Client
async def main():
# Connect to the server over HTTP
async with Client("http://127.0.0.1:8000/mcp") as client:
# List available tools
tools = await client.list_tools()
print("Available Tools:", tools)
# Call the 'add' tool
add_result = await client.call_tool("add", {"a": 5, "b": 3})
print("Add Result:", add_result.content[0].text)
# Read a resource
version = await client.read_resource("config://version")
print("Server Version:", version.content)
# Run the async main function
asyncio.run(main())
# Filename: mcp_server.py
from fastmcp import FastMCP
# Initialize the MCP server
mcp = FastMCP(name="Math MCP Server", description="A server for basic math operations")
# Define a tool for addition
@mcp.tool
def add(a: int, b: int) -> int:
"""Add two integers and return the result."""
return a + b
# Define a tool for multiplication
@mcp.tool
def multiply(a: float, b: float) -> float:
"""Multiply two floats and return the result."""
return a * b
# Define a static resource for server version
@mcp.resource("config://version")
def get_version():
"""Return the server version."""
return "1.0.0"
if __name__ == "__main__":
# Run the server over HTTP for remote access
mcp.run(transport="http", host="127.0.0.1", port=8000, path="/mcp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment