sanjeet9271/Sample-MCP-Server
If you are the rightful owner of Sample-MCP-Server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This is a Model Context Protocol (MCP) Server providing a scientific calculator with various mathematical operations.
Scientific Calculator MCP Server Tutorial
🧮 What is this?
This is a Model Context Protocol (MCP) Server that provides a scientific calculator with various mathematical operations. It demonstrates how to build and deploy an MCP server using FastMCP.
🔍 What is MCP?
Model Context Protocol (MCP) is an open protocol that standardizes how AI applications connect to external data sources and tools. Think of it as a universal adapter that lets AI assistants like Claude, ChatGPT, or other language models seamlessly access and interact with external resources.
Key Benefits:
- Standardized Interface: One protocol for all integrations
- Secure: Built-in security and permission controls
- Extensible: Easy to add new tools and resources
- Interoperable: Works with any MCP-compatible AI application
🛠️ What This Server Provides
This MCP server offers a comprehensive scientific calculator with the following capabilities:
Basic Arithmetic
- ➕ Addition: Add two numbers
- ➖ Subtraction: Subtract two numbers
- ✖️ Multiplication: Multiply two numbers
- ➗ Division: Divide two numbers (with zero-division protection)
Advanced Operations
- 🔢 Power: Calculate exponential powers
- √ Square Root: Calculate square roots (positive numbers only)
- ∛ Cube Root: Calculate cube roots (handles negative numbers)
- ❗ Factorial: Calculate factorials (non-negative integers only)
- 📊 Logarithm: Calculate natural logarithms (positive numbers only)
- 🔄 Remainder: Calculate division remainders
Trigonometric Functions
- 📐 Sine: Calculate sine (input in degrees)
- 📐 Cosine: Calculate cosine (input in degrees)
- 📐 Tangent: Calculate tangent (input in degrees, with undefined angle protection)
Resources
- 👋 Greeting: Get personalized greetings with dynamic names
🚀 Quick Start
Prerequisites
- Python 3.7 or higher
- pip (Python package manager)
Installation
-
Clone or download this project
git clone <your-repo-url> cd scientific-calculator-mcp -
Install dependencies
pip install -r requirements.txt -
Run the server
python mcp_server.py -
Verify it's working You should see:
Scientific Calculator MCP Server is starting up... Server will be available on port 3001
🔧 Configuration
The server runs on port 3001 by default and uses SSE (Server-Sent Events) transport. You can modify these settings in mcp_server.py:
mcp = FastMCP(
"Scientific Calculator",
description="A scientific calculator providing mathematical operations",
version="1.0.0",
transport="sse",
port=3001 # Change this to use a different port
)
📁 Project Structure
scientific-calculator-mcp/
├── mcp_server.py # Main server implementation
├── requirements.txt # Python dependencies
└── README.md # This tutorial file
💡 How to Use
Once the server is running, it can be connected to any MCP-compatible AI application. The AI will be able to:
- Perform calculations using the mathematical tools
- Access greetings using the greeting resource
- Handle errors gracefully with built-in validation
Example Usage Scenarios
Basic Math:
- "Add 15 and 27"
- "What's 144 divided by 12?"
- "Calculate 2 to the power of 8"
Advanced Operations:
- "Find the square root of 225"
- "What's the factorial of 5?"
- "Calculate the natural log of 100"
Trigonometry:
- "What's the sine of 30 degrees?"
- "Find the cosine of 45 degrees"
- "Calculate the tangent of 60 degrees"
🛡️ Error Handling
The server includes comprehensive error handling:
- Division by zero: Prevents crashes when dividing by zero
- Invalid inputs: Validates data types and ranges
- Mathematical limits: Handles overflow and undefined operations
- Negative roots: Prevents square roots of negative numbers
- Trigonometric edge cases: Handles undefined tangent values
🔧 Customization
Adding New Tools
To add a new mathematical operation:
-
Create a new tool function:
@mcp.tool() def my_operation(a: float, b: float) -> float: """Description of my operation""" return float(a + b) # Your calculation here -
Add error handling as needed
-
Restart the server to apply changes
Adding New Resources
To add a new resource:
@mcp.resource("my-resource://{parameter}")
def get_my_resource(parameter: str) -> str:
"""Description of my resource"""
return f"Result for {parameter}"
🧪 Testing
You can test the server functionality by:
- Running the server (
python mcp_server.py) - Using an MCP client to connect and test operations
- Checking the logs for any errors or issues
📚 Learning More
MCP Resources
Python Math Resources
🤝 Contributing
This is a tutorial project, but you can extend it by:
- Adding more mathematical operations
- Implementing complex number support
- Adding unit conversion capabilities
- Creating visualization tools
- Adding statistical functions
📄 License
This project is created for educational purposes. Feel free to use and modify it for your learning and development needs.
Happy calculating! 🧮✨