Get started with mcp-use in minutes
pip install mcp-use
import asyncio
from langchain_openai import ChatOpenAI # use your preferred LLM provider
from mcp_use import MCPAgent, MCPClient
async def main():
# Create MCPClient from configuration object
client = MCPClient({
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"],
"env": {
"DISPLAY": ":1"
}
}
}
})
# Create agent with the client
agent = MCPAgent(
llm=ChatOpenAI(model="gpt-4o"), # use your preferred LLM provider
client=client,
max_steps=30
)
# Run the query
result = await agent.run(
"Find the best restaurant in San Francisco USING GOOGLE SEARCH"
)
print(f"\nResult: {result}")
# Clean up
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())
pip install mcp-use
import asyncio
from mcp_use import MCPClient
async def main():
client = MCPClient({
"mcpServers": {
"everything": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-everything"]
}
}
})
# Initialize all configured sessions
await client.create_all_sessions()
# Get the session for a specific server
session = client.get_session("everything")
# List available tools
tools = await session.list_tools()
print(f"Available tools: {[t.name for t in tools]}")
# Call a specific tool with arguments
result = await session.call_tool("add", {"a": 1, "b": 2})
print(f"Result: {result}")
# Clean up
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())
pip install mcp-use
from mcp_use import MCPServer
server = MCPServer(
name="my-server",
version="1.0.0",
description="My custom MCP server"
)
# Define a tool
@server.tool()
def get_weather(city: str) -> dict:
"""Get weather for a city"""
return {"temperature": 72, "condition": "sunny", "city": city}
# Start the server
if __name__ == "__main__":
server.run()
Was this page helpful?