mcp-client-x
by: RGGH
Python MCP client + server example
πOverview
Purpose: To demonstrate a simple client-server implementation using the Model Context Protocol (MCP) for connecting large language models with tools and data.
Overview: This project serves as a practical example of creating an MCP server to host custom tools, connecting an MCP client to the server, and invoking tools to obtain responses. It showcases essential functionalities through a structured approach.
Key Features:
-
MCP Server with Custom Tools: Implements two tools,
calculate_bmi
for computing Body Mass Index andfetch_weather
for retrieving weather data, allowing users to leverage these functionalities via the server. -
MCP Client Connection: Facilitates a streamlined process for the client to connect to the server, initialize a session, and interact with server tools efficiently.
MCP Client Example βοΈ
This project demonstrates a simple client-server implementation using the Model Context Protocol (MCP), a standardized way to connect large language models with tools and data.
Overview
This example shows how to:
- Create an MCP server with custom tools
- Connect to the server using an MCP client
- Call tools and get responses from the server
Project Structure
.
βββ pyproject.toml
βββ README.md
βββ src
β βββ client
β β βββ mcp_client.py # MCP client implementation
β βββ server
β βββ example_server.py # MCP server with tools
βββ uv.lock
Server Implementation
The server exposes two tools:
calculate_bmi
- A simple calculator that computes Body Mass Indexfetch_weather
- An async tool that retrieves weather data from an external API
Client Implementation
The client connects to the server via stdio, initializes a session, and calls the server's tools.
Getting Started
Prerequisites
- Python 3.9+
- uv (Python package manager)
Installation
# Install dependencies
uv install -e .
Running the Example
Start the client (which will automatically start the server):
uv run src/client/mcp_client.py
Usage
The client will:
- Connect to the server
- List available tools
- Call the BMI calculator with sample data
- Call the weather tool with sample coordinates
Example Response
Available tools: meta=None nextCursor=None tools=[...]
BMI calculation result: 22.857142857142858
Weather data: {"current_weather":{"temperature":14.2,"windspeed":12.6, ...}}
Test with MCP Inspector
Run the command below and then visit http://localhost:5173
β― mcp dev src/server/example_server.py
Starting MCP inspector...
Proxy server listening on port 3000
π MCP Inspector is up and running at http://localhost:5173 π
New SSE connection
Query parameters: {
transportType: 'stdio',
command: 'uv',
args: 'run --with mcp mcp run src/server/example_server.py',
}
Resources
This project uses:
License
This project is licensed under the MIT License - see the LICENSE file for details.