Provides the underlying web framework for exposing math tools and prompt templates via Server-Sent Events (SSE)
Enables the client to connect to the server and use LangChain adapters to discover and invoke the available math tools
Used to create a ReAct agent that can interact with the math tools exposed by the server
Provides the language model (GPT-4.1) used by the client to interpret user requests and invoke the appropriate math tools
Math MCP Example: Server & Client
#############################################
Directory Structure & Environment Setup
#############################################
Recommended project structure:
Setting up your Python environment
- Create a virtual environment (recommended):
- Install required dependencies:
(Make sure you are in the root directory or where your requirements are listed)
- Set your OpenAI API key:
Overview
- Server: Exposes math tools (add, sub, multiply) and prompt templates using FastMCP and SSE (Server-Sent Events).
- Client: Connects to the server, discovers available tools, and uses an LLM agent to invoke those tools.
Learning Objectives
- Understand how to register and expose tools in a Python server.
- Learn how to connect to a tool server and discover available tools.
- See how an LLM agent can use external tools to answer questions.
- Practice async programming and client-server communication.
#############################################
Server: math_server.py
#############################################
What does it do?
- Registers three math tools:
add
,sub
,multiply
. - Registers three prompt templates for natural language generation.
- Exposes an ASGI app for Uvicorn to serve via SSE.
- Logs every tool and prompt call for transparency.
Key code sections
>>>>>>>>>>>>>>>>>>>>>>>> How to run the server===================================================================================
From the iceberg_mcp/math
directory:
Or from the project root:
#############################################
Client: math_clinet.py
#############################################
What does it do?
- Connects to the math server using SSE.
- Discovers available tools.
- Uses a LangChain agent to ask the server to add 3 and 5.
- Prints only the final answer from the agent's response.
Key code sections
OpenAI API Key Setup
To use the LLM agent (e.g., GPT-4), you need an OpenAI API key. This is required for the client to access OpenAI's language models.
How to set your OpenAI API key:
- The recommended way is to set the
OPENAI_API_KEY
environment variable in your shell:
- Alternatively, you can set it in your Python code (not recommended for production):
You must set the API key before running the client, or you will get authentication errors.
>>>>>>>>>>>>>>>>>>> How to run the client################################################################
From the iceberg_mcp/math
directory:
Experiment & Learn
- Try changing the numbers in the client prompt.
- Add new tools (e.g., division) to the server and see if the client discovers them.
- Add more prompts or logging to see how the server responds.
- Explore how async programming enables real-time tool discovery and invocation.
Troubleshooting
- If the client prints
[]
for tools, check server logs and package versions. - Make sure both server and client use compatible MCP and adapter versions.
- Ensure the server is running before starting the client.
Summary
This example demonstrates how to:
- Build a tool-augmented AI server in Python
- Connect and interact with it using a modern LLM agent
- Use async programming for efficient, real-time communication
Happy learning! # mcp
This server cannot be installed
A tool-augmented AI server that exposes basic math operations (add, subtract, multiply) via FastMCP and Server-Sent Events, allowing LLM agents to discover and use these mathematical tools.
Related MCP Servers
- AsecurityAlicenseAqualityA Model Context Protocol server that provides basic mathematical and statistical functions to LLMs, enabling them to perform accurate numerical calculations through a simple API.Last updated -1313TypeScriptMIT License
- AsecurityFlicenseAqualityA server that provides tools for performing basic arithmetic operations (addition, subtraction, multiplication, division) via the Model Context Protocol.Last updated -4JavaScript
- AsecurityFlicenseAqualityA simple server that provides basic arithmetic operations (addition, subtraction, multiplication, division) as tools that can be called by Large Language Models through the Model Context Protocol.Last updated -4JavaScript
- AsecurityFlicenseAqualityA simple Model Context Protocol server that provides basic arithmetic operations (addition, subtraction, multiplication, division) as tools that can be called by Large Language Models.Last updated -4JavaScript