import asyncio
import os
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from mcp_use import MCPAgent, MCPClient
async def run_healthcare_chat():
print("Loading environment variables...")
load_dotenv()
groq_key = os.getenv("GROQ_API_KEY")
if not groq_key:
print("Error: GROQ_API_KEY not found in .env")
return
os.environ["GROQ_API_KEY"] = groq_key
config_file = "C:\\Users\\sniki\\OneDrive\\Desktop\\mcpserver\\ragmcp\\healthcare.json"
print(f"Loading config file: {config_file}")
if not os.path.exists(config_file):
print(f"Error: Config file {config_file} not found")
return
print("Initializing HealthcareRAGTools chat...")
try:
client = MCPClient.from_config_file(config_file)
print("MCPClient initialized")
llm = ChatGroq(model="llama-3.3-70b-versatile")
print("ChatGroq initialized")
agent = MCPAgent(
llm=llm,
client=client,
max_steps=15,
memory_enabled=True,
)
print("\n===== Interactive HealthcareRAGTools Chat =====")
print("Type 'exit' or 'quit' to end the conversation")
print("Type 'clear' to clear conversation history")
print("Example queries:")
print("- Log symptoms for patient P123: fever and cough, severity Moderate, show similar cases")
print("- Search documents for flu symptoms")
print("==================================\n")
while True:
user_input = input("\nYou: ")
if user_input.lower() in ["exit", "quit"]:
print("Ending conversation...")
break
if user_input.lower() == "clear":
agent.clear_conversation_history()
print("Conversation history cleared.")
continue
print("\nAssistant: ", end="", flush=True)
try:
response = await agent.run(user_input)
print(response)
except Exception as e:
print(f"\nError: {e}")
except Exception as e:
print(f"Initialization error: {e}")
finally:
if client and client.sessions:
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(run_healthcare_chat())