#!/usr/bin/env python3
"""
Simple MCP Server with stdio transport for Claude Desktop integration
Provides calculator and greeting tools
"""
import asyncio
import json
import logging
import sys
from typing import Any, Dict, List, Optional
from fastmcp import FastMCP
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize MCP server
mcp = FastMCP("Simple Function Server")
@mcp.tool()
def calculator(operation: str, a: float, b: float) -> Dict[str, Any]:
"""
Perform mathematical calculations with two numbers.
Args:
operation: The operation to perform (add, subtract, multiply, divide)
a: First number
b: Second number
Returns:
Dictionary containing the calculation result
"""
try:
if operation == "add":
result = a + b
elif operation == "subtract":
result = a - b
elif operation == "multiply":
result = a * b
elif operation == "divide":
if b == 0:
return {"error": "Division by zero is not allowed"}
result = a / b
else:
return {"error": f"Unsupported operation: {operation}. Use add, subtract, multiply, or divide"}
return {
"operation": operation,
"a": a,
"b": b,
"result": result,
"success": True
}
except Exception as e:
logger.error(f"Calculation error: {str(e)}")
return {"error": f"Calculation failed: {str(e)}"}
@mcp.tool()
def greeting(name: str, language: str = "english") -> Dict[str, Any]:
"""
Generate a greeting message in different languages.
Args:
name: The name of the person to greet
language: The language for the greeting (english, spanish, french, german)
Returns:
Dictionary containing the greeting message
"""
try:
greetings = {
"english": f"Hello {name}! Nice to meet you!",
"spanish": f"¡Hola {name}! ¡Mucho gusto!",
"french": f"Bonjour {name}! Ravi de vous rencontrer!",
"german": f"Hallo {name}! Freut mich, dich kennenzulernen!"
}
language = language.lower()
if language not in greetings:
return {
"error": f"Language '{language}' not supported. Available: {list(greetings.keys())}"
}
return {
"name": name,
"language": language,
"greeting": greetings[language],
"success": True
}
except Exception as e:
logger.error(f"Greeting error: {str(e)}")
return {"error": f"Greeting failed: {str(e)}"}
if __name__ == "__main__":
# Run the MCP server with stdio transport
mcp.run(transport="stdio")