117 lines
2.9 KiB
Python
117 lines
2.9 KiB
Python
"""
|
|
Basic FastMCP Server Template
|
|
==============================
|
|
A minimal working FastMCP server with essential patterns.
|
|
"""
|
|
|
|
from fastmcp import FastMCP
|
|
import os
|
|
|
|
# Load environment variables (optional)
|
|
from dotenv import load_dotenv
|
|
load_dotenv()
|
|
|
|
# ============================================================================
|
|
# CRITICAL: Server must be at module level for FastMCP Cloud
|
|
# ============================================================================
|
|
|
|
mcp = FastMCP(
|
|
name="My Basic Server",
|
|
instructions="""
|
|
This is a basic MCP server demonstrating core patterns.
|
|
|
|
Available tools:
|
|
- greet: Say hello to someone
|
|
- calculate: Perform basic math operations
|
|
|
|
Available resources:
|
|
- info://status: Server status information
|
|
"""
|
|
)
|
|
|
|
# ============================================================================
|
|
# Tools
|
|
# ============================================================================
|
|
|
|
@mcp.tool()
|
|
def greet(name: str, greeting: str = "Hello") -> str:
|
|
"""
|
|
Greet someone by name.
|
|
|
|
Args:
|
|
name: The name of the person to greet
|
|
greeting: The greeting to use (default: "Hello")
|
|
|
|
Returns:
|
|
A greeting message
|
|
"""
|
|
return f"{greeting}, {name}!"
|
|
|
|
|
|
@mcp.tool()
|
|
async def calculate(operation: str, a: float, b: float) -> dict:
|
|
"""
|
|
Perform a mathematical operation.
|
|
|
|
Args:
|
|
operation: The operation to perform (add, subtract, multiply, divide)
|
|
a: First number
|
|
b: Second number
|
|
|
|
Returns:
|
|
Dictionary with the result or error message
|
|
"""
|
|
operations = {
|
|
"add": lambda x, y: x + y,
|
|
"subtract": lambda x, y: x - y,
|
|
"multiply": lambda x, y: x * y,
|
|
"divide": lambda x, y: x / y if y != 0 else None
|
|
}
|
|
|
|
if operation not in operations:
|
|
return {
|
|
"error": f"Unknown operation: {operation}",
|
|
"valid_operations": list(operations.keys())
|
|
}
|
|
|
|
result = operations[operation](a, b)
|
|
|
|
if result is None:
|
|
return {"error": "Division by zero"}
|
|
|
|
return {
|
|
"operation": operation,
|
|
"a": a,
|
|
"b": b,
|
|
"result": result
|
|
}
|
|
|
|
|
|
# ============================================================================
|
|
# Resources
|
|
# ============================================================================
|
|
|
|
@mcp.resource("info://status")
|
|
def server_status() -> dict:
|
|
"""Get current server status."""
|
|
from datetime import datetime
|
|
|
|
return {
|
|
"server": "My Basic Server",
|
|
"status": "operational",
|
|
"timestamp": datetime.now().isoformat(),
|
|
"version": "1.0.0"
|
|
}
|
|
|
|
|
|
# ============================================================================
|
|
# Main Execution
|
|
# ============================================================================
|
|
|
|
if __name__ == "__main__":
|
|
# Run with stdio transport (default)
|
|
mcp.run()
|
|
|
|
# Alternative: HTTP transport for testing
|
|
# mcp.run(transport="http", port=8000)
|