File size: 783 Bytes
e7d289d
b9a84a1
 
98cc7d2
b9a84a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import asyncio
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from mcp_use import MCPAgent, MCPClient
import os

# Load env once
load_dotenv()
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")

# Initialize MCP client and agent once (memory preserved)
CONFIG_FILE = "server/weather.json"
client = MCPClient.from_config_file(CONFIG_FILE)
llm = ChatGroq(model="qwen-qwq-32b")

agent = MCPAgent(
    llm=llm,
    client=client,
    max_steps=15,
    memory_enabled=True  # Keep memory for conversation
)

async def run_memory_chat(question: str) -> str:
    """Run a single question through the MCPAgent and get a response."""
    try:
        response = await agent.run(question)
        return response
    except Exception as e:
        return f"Error: {e}"