-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathgemini-mcp-agent.py
More file actions
60 lines (49 loc) · 1.93 KB
/
gemini-mcp-agent.py
File metadata and controls
60 lines (49 loc) · 1.93 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import asyncio
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_google_genai import ChatGoogleGenerativeAI
import os
# Get the Gemini API key from the environment variable
api_key = os.environ.get("GEMINI_API_KEY")
# Create Gemini instance LLM class
model = ChatGoogleGenerativeAI(
model="gemini-2.5-pro-exp-03-25", # or "gemini-2.0-flash"
temperature=0.7,
max_retries=2,
google_api_key=api_key,
)
async def main():
async with MultiServerMCPClient(
{
"airbnb": {
"command": "npx",
"args": ["-y", "@openbnb/mcp-server-airbnb", "--ignore-robots-txt"],
"transport": "stdio",
},
# Add more or your own servers here, works with remote servers too via sse
# "weather": {
# "url": "http://localhost:8000/sse", # Ensure the weather server is running
# "transport": "sse",
# },
}
) as client:
# Create ReAct Agent with MCP servers
graph = create_react_agent(model, client.get_tools())
# Initialize conversation history using simple tuples
inputs = {"messages": []}
print("Agent is ready. Type 'exit' to quit.")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("Exiting chat.")
break
# Append user message to history
inputs["messages"].append(("user", user_input))
# call our graph with streaming to see the steps
async for state in graph.astream(inputs, stream_mode="values"):
last_message = state["messages"][-1]
last_message.pretty_print()
# update the inputs with the agent's response
inputs["messages"] == state["messages"]
if __name__ == "__main__":
asyncio.run(main())