Skip to main content
Glama

FastMCP OAuth Server

by jcamier
weather_demo.py•5.45 kB
""" Weather Demo for MCP OAuth This demo focuses on the weather tool functionality, showing how to authenticate and call weather-related tools. """ import asyncio import sys from pathlib import Path # Add parent directory to path to import modules sys.path.insert(0, str(Path(__file__).parent.parent)) from client.client import MCPOAuthClient async def weather_demo(): """Demonstrate weather tool functionality.""" print("šŸŒ¤ļø MCP OAuth Weather Demo") print("=" * 40) server_url = "http://localhost:8000" # Create client client = MCPOAuthClient(server_url) try: # Authenticate print("šŸ” Authenticating with MCP server...") if not await client.authenticate(): print("āŒ Authentication failed!") return False # Connect print("šŸ”Œ Connecting to MCP server...") if not await client.connect(): print("āŒ Connection failed!") return False print("āœ… Connected successfully!") # Demo: Multiple weather queries cities = ["London", "Tokyo", "New York", "Paris", "Sydney"] print(f"\nšŸŒ Getting weather for {len(cities)} cities...") print("=" * 40) for city in cities: print(f"\nšŸŒ¤ļø Getting weather for {city}...") try: result = await client.call_tool("get_weather", {"city": city}) if result.get('status') == 'success': print(f"āœ… {city}: {result['result']}") else: print(f"āŒ {city}: Failed to get weather") except Exception as e: print(f"āŒ {city}: Error - {e}") # Interactive weather queries print(f"\nšŸŽ® Interactive Weather Queries") print("=" * 40) print("Enter city names to get weather (or 'quit' to exit)") while True: city = input("\nšŸŒ Enter city name: ").strip() if city.lower() in ['quit', 'exit', 'q']: break if not city: print("āŒ Please enter a city name") continue try: print(f"šŸ” Getting weather for {city}...") result = await client.call_tool("get_weather", {"city": city}) if result.get('status') == 'success': print(f"āœ… Weather: {result['result']}") else: print(f"āŒ Failed to get weather for {city}") except Exception as e: print(f"āŒ Error getting weather: {e}") # Cleanup await client.disconnect() print("\nāœ… Weather demo completed!") return True except KeyboardInterrupt: print("\n\nšŸ‘‹ Demo interrupted by user") await client.disconnect() return True except Exception as e: print(f"\nāŒ Demo failed: {e}") await client.disconnect() return False async def batch_weather_demo(): """Demonstrate batch weather queries.""" print("šŸ“Š Batch Weather Demo") print("=" * 30) server_url = "http://localhost:8000" client = MCPOAuthClient(server_url) try: # Authenticate and connect if not await client.authenticate(): return False if not await client.connect(): return False # Get cities from user print("\nEnter cities for batch weather query:") print("(Enter city names separated by commas)") cities_input = input("Cities: ").strip() if not cities_input: cities = ["London", "Tokyo", "New York"] # Default cities print(f"Using default cities: {', '.join(cities)}") else: cities = [city.strip() for city in cities_input.split(',') if city.strip()] print(f"\nšŸ“” Querying weather for {len(cities)} cities...") # Batch queries (sequential for this demo) results = [] for i, city in enumerate(cities, 1): print(f" [{i}/{len(cities)}] {city}...", end=" ") try: result = await client.call_tool("get_weather", {"city": city}) results.append((city, result)) print("āœ…") except Exception as e: results.append((city, {"error": str(e)})) print("āŒ") # Display results print(f"\nšŸ“‹ Weather Results Summary:") print("=" * 50) for city, result in results: if 'error' in result: print(f"āŒ {city:15} - Error: {result['error']}") else: print(f"āœ… {city:15} - {result.get('result', 'No data')}") await client.disconnect() return True except Exception as e: print(f"āŒ Batch demo failed: {e}") await client.disconnect() return False def main(): """Main weather demo entry point.""" print("Choose weather demo mode:") print("1. Interactive weather demo") print("2. Batch weather demo") choice = input("\nEnter choice (1-2): ").strip() if choice == "1": success = asyncio.run(weather_demo()) elif choice == "2": success = asyncio.run(batch_weather_demo()) else: print("āŒ Invalid choice") return 1 return 0 if success else 1 if __name__ == "__main__": exit_code = main() sys.exit(exit_code)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jcamier/mcp-oauth'

If you have feedback or need assistance with the MCP directory API, please join our Discord server