forked from mcp-use/mcp-use
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_example.py
More file actions
53 lines (35 loc) · 1.36 KB
/
http_example.py
File metadata and controls
53 lines (35 loc) · 1.36 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
"""
HTTP Example for mcp_use.
This example demonstrates how to use the mcp_use library with MCPClient
to connect to an MCP server running on a specific HTTP port.
Before running this example, you need to start the Playwright MCP server
in another terminal with:
npx @playwright/mcp@latest --port 8931
This will start the server on port 8931. Resulting in the config you find below.
Of course you can run this with any server you want at any URL.
Special thanks to https://github.com/microsoft/playwright-mcp for the server.
"""
import asyncio
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
async def main():
"""Run the example using a configuration file."""
# Load environment variables
load_dotenv()
config = {"mcpServers": {"http": {"url": "http://localhost:8931/sse"}}}
# Create MCPClient from config file
client = MCPClient.from_dict(config)
# Create LLM
llm = ChatOpenAI(model="gpt-5")
# Create agent with the client
agent = MCPAgent(llm=llm, client=client, max_steps=30, pretty_print=True)
# Run the query
result = await agent.run(
"Find the best restaurant in San Francisco USING GOOGLE SEARCH",
max_steps=30,
)
print(f"\nResult: {result}")
if __name__ == "__main__":
# Run the appropriate example
asyncio.run(main())