example_usage.pyā¢4.11 kB
#!/usr/bin/env python3
"""
Example usage of the MCP Server Demo
This script demonstrates how to interact with the MCP server programmatically.
Note: In practice, you'd typically use the MCP Inspector or Claude Desktop.
"""
import asyncio
import json
from server import mcp
async def demo_tools():
"""Demonstrate the available tools"""
print("š ļø Testing Tools:")
print("=" * 50)
# Test basic arithmetic
result = mcp.call_tool("add", {"a": 5, "b": 3})
print(f"add(5, 3) = {result}")
result = mcp.call_tool("multiply", {"a": 4, "b": 7})
print(f"multiply(4, 7) = {result}")
# Test BMI calculation
result = mcp.call_tool("calculate_bmi", {"weight_kg": 70.0, "height_m": 1.75})
print(f"BMI (70kg, 1.75m) = {result}")
# Test number analysis
numbers = [1, 5, 3, 9, 2, 8]
result = mcp.call_tool("analyze_numbers", {"numbers": numbers})
print(f"Analysis of {numbers}: {json.dumps(result, indent=2)}")
# Test current time
result = mcp.call_tool("get_current_time", {})
print(f"Current time: {result}")
async def demo_resources():
"""Demonstrate the available resources"""
print("\nš Testing Resources:")
print("=" * 50)
# Test greeting resource
greeting = mcp.get_resource("greeting://Alice")
print(f"Greeting for Alice: {greeting}")
# Test app config
config = mcp.get_resource("config://app")
print(f"App config: {json.dumps(json.loads(config), indent=2)}")
# Test weather resource
weather = mcp.get_resource("weather://London")
print(f"Weather for London: {json.dumps(json.loads(weather), indent=2)}")
async def demo_prompts():
"""Demonstrate the available prompts"""
print("\nš¬ Testing Prompts:")
print("=" * 50)
# Test code review prompt
sample_code = """
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
"""
review_prompt = mcp.get_prompt("review_code", {"code": sample_code})
print(f"Code review prompt:\n{review_prompt[:200]}...")
# Test debug prompt
error_msg = "ModuleNotFoundError: No module named 'requests'"
debug_prompt = mcp.get_prompt("debug_error", {"error_message": error_msg})
print(f"\nDebug prompt:\n{debug_prompt[:200]}...")
# Test concept explanation prompt
concept_prompt = mcp.get_prompt("explain_concept", {"concept": "async/await"})
print(f"\nConcept explanation prompt:\n{concept_prompt[:200]}...")
def main():
"""Main demo function"""
print("š MCP Server Demo - Example Usage")
print("=" * 60)
print("This demonstrates the capabilities of our MCP server.")
print("In practice, you'd use the MCP Inspector or Claude Desktop.\n")
try:
# Note: These are simplified examples
# The actual MCP protocol is more complex and handles async communication
print("ā ļø Note: This is a simplified demonstration.")
print("For real usage, run: mcp dev server.py")
print("This will start the MCP Inspector for interactive testing.\n")
# Show available capabilities
print("Available Tools:")
tools = ["add", "multiply", "calculate_bmi", "analyze_numbers", "get_current_time"]
for tool in tools:
print(f" - {tool}")
print("\nAvailable Resources:")
resources = ["greeting://{name}", "config://app", "weather://{city}"]
for resource in resources:
print(f" - {resource}")
print("\nAvailable Prompts:")
prompts = ["review_code", "debug_error", "explain_concept"]
for prompt in prompts:
print(f" - {prompt}")
print("\nšÆ To test these capabilities:")
print("1. Run: python setup_dev.py (to set up environment)")
print("2. Run: mcp dev server.py (to start development mode)")
print("3. Use the MCP Inspector to interact with your server")
except Exception as e:
print(f"Error during demo: {e}")
if __name__ == "__main__":
main()