chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
# MCP Calculator Server (Python)
|
||||
|
||||
|
||||
|
||||
A simple Model Context Protocol (MCP) server implementation in Python that provides basic calculator functionality.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
Install the required dependencies:
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
Or install the MCP Python SDK directly:
|
||||
|
||||
```bash
|
||||
pip install mcp>=1.18.0
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Running the Server
|
||||
|
||||
The server is designed to be used by MCP clients (like Claude Desktop). To start the server:
|
||||
|
||||
```bash
|
||||
python mcp_calculator_server.py
|
||||
```
|
||||
|
||||
**Note**: When run directly in a terminal, you'll see JSON-RPC validation errors. This is normal behavior - the server is waiting for properly formatted MCP client messages.
|
||||
|
||||
### Testing the Functions
|
||||
|
||||
To test that the calculator functions work correctly:
|
||||
|
||||
```bash
|
||||
python test_calculator.py
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Import Errors
|
||||
|
||||
If you see `ModuleNotFoundError: No module named 'mcp'`, install the MCP Python SDK:
|
||||
|
||||
```bash
|
||||
pip install mcp>=1.18.0
|
||||
```
|
||||
|
||||
### JSON-RPC Errors When Running Directly
|
||||
|
||||
Errors like "Invalid JSON: EOF while parsing a value" when running the server directly are expected. The server needs MCP client messages, not direct terminal input.
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Sample MCP Calculator Server implementation in Python.
|
||||
|
||||
This module demonstrates how to create a simple MCP server with calculator tools
|
||||
that can perform basic arithmetic operations (add, subtract, multiply, divide).
|
||||
"""
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
# Create a FastMCP server
|
||||
mcp = FastMCP("Calculator MCP Server")
|
||||
|
||||
@mcp.tool()
|
||||
def add(a: float, b: float) -> float:
|
||||
"""Add two numbers together and return the result."""
|
||||
return a + b
|
||||
|
||||
@mcp.tool()
|
||||
def subtract(a: float, b: float) -> float:
|
||||
"""Subtract b from a and return the result."""
|
||||
return a - b
|
||||
|
||||
@mcp.tool()
|
||||
def multiply(a: float, b: float) -> float:
|
||||
"""Multiply two numbers together and return the result."""
|
||||
return a * b
|
||||
|
||||
@mcp.tool()
|
||||
def divide(a: float, b: float) -> float:
|
||||
"""
|
||||
Divide a by b and return the result.
|
||||
|
||||
Raises:
|
||||
ValueError: If b is zero
|
||||
"""
|
||||
if b == 0:
|
||||
raise ValueError("Cannot divide by zero")
|
||||
return a / b
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Start the server
|
||||
mcp.run()
|
||||
@@ -0,0 +1 @@
|
||||
mcp>=1.26.0
|
||||
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for the MCP Calculator Server.
|
||||
|
||||
This script tests the calculator functions to ensure they work correctly
|
||||
before running the MCP server.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the current directory to the path so we can import the calculator server
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_calculator_functions():
|
||||
"""Test all calculator functions."""
|
||||
# Import the functions from the calculator server
|
||||
from mcp_calculator_server import add, subtract, multiply, divide
|
||||
|
||||
print("Testing calculator functions...")
|
||||
|
||||
# Test addition
|
||||
result = add(5, 3)
|
||||
assert result == 8, f"Expected 8, got {result}"
|
||||
print("✓ Addition test passed")
|
||||
|
||||
# Test subtraction
|
||||
result = subtract(10, 4)
|
||||
assert result == 6, f"Expected 6, got {result}"
|
||||
print("✓ Subtraction test passed")
|
||||
|
||||
# Test multiplication
|
||||
result = multiply(7, 6)
|
||||
assert result == 42, f"Expected 42, got {result}"
|
||||
print("✓ Multiplication test passed")
|
||||
|
||||
# Test division
|
||||
result = divide(15, 3)
|
||||
assert result == 5, f"Expected 5, got {result}"
|
||||
print("✓ Division test passed")
|
||||
|
||||
# Test division by zero
|
||||
try:
|
||||
divide(10, 0)
|
||||
assert False, "Expected ValueError for division by zero"
|
||||
except ValueError as e:
|
||||
assert str(e) == "Cannot divide by zero", f"Expected 'Cannot divide by zero', got '{str(e)}'"
|
||||
print("✓ Division by zero test passed")
|
||||
|
||||
print("\nAll calculator function tests passed! ✅")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_calculator_functions()
|
||||
Reference in New Issue
Block a user