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! ๐งฎโจ