Sample-MCP-Server

sanjeet9271/Sample-MCP-Server

3.2

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

  1. Clone or download this project

    git clone <your-repo-url>
    cd scientific-calculator-mcp
    
  2. Install dependencies

    pip install -r requirements.txt
    
  3. Run the server

    python mcp_server.py
    
  4. 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:

  1. Perform calculations using the mathematical tools
  2. Access greetings using the greeting resource
  3. 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:

  1. 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
    
  2. Add error handling as needed

  3. 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:

  1. Running the server (python mcp_server.py)
  2. Using an MCP client to connect and test operations
  3. 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! ๐Ÿงฎโœจ