MCP Use Framework

1.7.0 · active · verified Mon Apr 13

mcp-use is a full-stack Python framework for building Multi-Cloud Platform (MCP) agents, clients, and servers. It simplifies the development of distributed systems with a focus on asynchronous communication. The current version is 1.7.0 and it maintains an active release cadence, frequently adding features and improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic MCP Agent and a Client using `mcp-use`. The Agent defines a message handler, the Client connects to it, and they communicate via a named Channel. It showcases the core asynchronous nature and connection management.

import asyncio
from mcp_use import Agent, Client, Server, Channel

async def main():
    # Example: A simple MCP Agent and Client interaction

    # 1. Start a simple Agent
    class MyAgent(Agent):
        async def handle_message(self, message: str) -> str:
            print(f"Agent received: {message}")
            return f"Agent processed: {message.upper()}"

    agent = MyAgent(name="test-agent", host="127.0.0.1", port=8000)
    agent_task = asyncio.create_task(agent.start())

    await asyncio.sleep(0.5) # Give agent time to start

    # 2. Connect a Client to the Agent
    client = Client(name="test-client", host="127.0.0.1", port=8000)
    await client.connect()

    # 3. Send a message via a Channel
    try:
        async with client.channel("my-channel") as channel:
            response = await channel.send("hello mcp!")
            print(f"Client received: {response}")

            assert response == "Agent processed: HELLO MCP!"

    finally:
        # 4. Clean up
        await client.disconnect()
        agent_task.cancel()
        await agent.stop()
        print("Cleanup complete.")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →