-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathserver.py
More file actions
33 lines (27 loc) · 1.01 KB
/
server.py
File metadata and controls
33 lines (27 loc) · 1.01 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
import asyncio
from mcp_use import MCPAgent, MCPClient
from langchain_openai import ChatOpenAI
async def main():
client = MCPClient.from_config_file("mcp-config.json")
# Create sessions first
await client.create_all_sessions()
# Discover available tools from MCP server
tools = set()
for name in client.get_server_names():
session = client.get_session(name)
for t in await session.list_tools():
tools.add(str(getattr(t, "name", None) or getattr(t, "tool", None) or "unknown"))
print("Available MCP tools:", sorted(tools))
llm = ChatOpenAI(model="gpt-4o-mini")
agent = MCPAgent(
llm=llm,
client=client,
system_prompt="You can browse with Stagehand MCP tools and answer user queries.",
memory_enabled=True,
max_steps=20,
)
result = await agent.run("Go to example.com and extract the title")
print(result)
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())