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.
This example creates a simple MCP server with tools for basic math operations and a resource for version info.
- Install fastMCP: Run
pip install fastmcp(oruv pip install fastmcpfor better dependency management). - Create the Server File: Save as
mcp_server.py. (see file) - Run the Server: Execute
python mcp_server.py. The server will be available athttp://127.0.0.1:8000/mcp. - Test: Clients can now discover tools (e.g.,
add,multiply) and resources (e.g.,config://version).
This example creates a client that connects to the above server, lists tools, and calls one.
- Create the Client File: Save as
mcp_client.py. (see file) - 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.