Skip to main content
Glama
server.py3.89 kB
from fastapi import FastAPI from pydantic import BaseModel from contextlib import asynccontextmanager import os from dotenv import load_dotenv from providers.google_llm import ask_google from tools.weather_tool import get_weather from tools.news_tool import get_news from tools.search_tool import search_web from tools.dictionary_tool import define_word from tools.quotes_tool import get_quote # Load environment variables load_dotenv() # Lifespan event handler @asynccontextmanager async def lifespan(app: FastAPI): # Startup api_key = os.getenv("GOOGLE_API_KEY") if not api_key: print("\n⚠️ WARNING: GOOGLE_API_KEY tidak ditemukan!") print(" Buat file .env dan tambahkan: GOOGLE_API_KEY=your_api_key") print(" Dapatkan API key dari: https://aistudio.google.com/app/apikey\n") else: print(f"\n✅ GOOGLE_API_KEY ditemukan (length: {len(api_key)})") print(" Server siap digunakan!\n") yield # Shutdown (jika ada cleanup yang diperlukan) print("\n👋 Server shutting down...\n") app = FastAPI(lifespan=lifespan) class Prompt(BaseModel): prompt: str # --------------------------------------------- # Auto Tool Router Berdasarkan Prompt # --------------------------------------------- @app.post("/auto") def auto_tool(prompt: Prompt): print(f"\n📥 Request: {prompt.prompt}") text = prompt.prompt.lower() tool_result = None tool_context = "" try: if "cuaca" in text or "weather" in text: city = text.split("di ")[-1] if "di " in text else "Jakarta" print(f"🌤️ Fetching weather for: {city}") tool_result = get_weather(city) tool_context = f"Data cuaca untuk kota {city}: {tool_result}" elif "berita" in text or "news" in text: topic = text.replace("berita", "").strip() or "technology" print(f"📰 Fetching news for: {topic}") tool_result = get_news(topic) tool_context = f"Berita tentang {topic}: {tool_result}" elif "definisi" in text or "arti" in text: word = text.split()[-1] print(f"📖 Fetching definition for: {word}") tool_result = define_word(word) tool_context = f"Definisi kata '{word}': {tool_result}" elif "quote" in text or "bijak" in text: print("💭 Fetching quote...") tool_result = get_quote() tool_context = f"Quote bijak: {tool_result}" else: # default: search print(f"🔍 Searching web for: {prompt.prompt}") tool_result = search_web(prompt.prompt) tool_context = f"Hasil pencarian untuk '{prompt.prompt}': {tool_result}" print(f"✅ Tool result obtained") # Gabungkan hasil tool dengan LLM untuk response yang natural llm_prompt = f"""Berdasarkan data berikut, berikan jawaban yang informatif dan natural untuk pertanyaan user: "{prompt.prompt}" Data dari tool: {tool_context} Berikan jawaban yang mudah dipahami, ringkas, dan informatif. Jangan sebutkan bahwa ini dari tool atau API.""" print("🤖 Calling LLM...") llm_response = ask_google(llm_prompt) print("✅ LLM response received\n") return { "response": llm_response, "raw_data": tool_result } except Exception as e: print(f"❌ Error: {str(e)}\n") return { "response": f"Terjadi error: {str(e)}", "raw_data": None } # --------------------------------------------- # Route LLM Google AI Studio # --------------------------------------------- @app.post("/llm") def google_llm(prompt: Prompt): return {"response": ask_google(prompt.prompt)} @app.get("/") def root(): return {"status": "MCP Server with Google AI Studio - OK"}

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/jamalexfo/mcp-api-tools'

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