Building an MCP-Enabled Agent in 6 Lines with mcp-use

~12 min read

mcp-use removes the low-level protocol plumbing — an MCP client, server connection, tool discovery, and LLM wiring all come together in about 6 lines of code.

With MCP's core protocol ideas established elsewhere in this curriculum, the practical question becomes: how do you actually BUILD something using MCP without hand-writing all the low-level protocol handling yourself? This is exactly the gap mcp-use fills.

mcp-use makes it easy to build MCP-enabled agents without handling low-level protocol details yourself. Rather than you manually implementing the capability handshake, the transport layer, and tool-schema wiring, the library packages all of that into a small, high-level API. Specifically, mcp-use sets up the MCP client, connects to one or more servers, discovers available tools, and exposes them to the LLM in a structured way — this allows the agent to decide when to call a tool, while the framework manages capability loading and communication under the hood.

This course demonstrates this concretely: you can build an mcp-enabled agent using mcp-use in just 6 lines of code. This creates an MCP client, connects it to a server (this course's example uses a Playwright server, useful for browser automation), wraps the server's capabilities as tools, and passes them to an LLM-powered agent. From here, the LLM can request tool calls naturally during reasoning, while mcp-use handles execution and streaming — meaning once this initial setup is done, you interact with the resulting agent the same way you'd interact with any other LLM-powered agent; the MCP plumbing underneath is invisible from that point forward.

This is a genuinely different developer experience from implementing MCP's client-server handshake and message protocol by hand — mcp-use exists specifically to compress what would otherwise be substantial boilerplate (connection setup, capability negotiation, tool schema translation into an LLM-callable format) into a small, declarative setup step, so the actual application logic (what the agent should DO with those tools) is where a developer's attention goes, not the protocol mechanics underneath it.

💻 Code example

# The book's 6-line pattern, in Python: MCPClient + MCPAgent from
# mcp-use handle the connection, tool discovery, and LLM wiring.
from mcp_use import MCPAgent, MCPClient
from langchain_openai import ChatOpenAI

async def build_agent():
    client = MCPClient.from_config_file("playwright_mcp_config.json")  # 1. client
    llm = ChatOpenAI(model="gpt-4o")                                     # 2. LLM
    agent = MCPAgent(llm=llm, client=client, max_steps=15)                # 3. agent

    # From here, the LLM can request tool calls naturally during
    # reasoning -- mcp-use handles execution and streaming under the hood
    result = await agent.run("Go to example.com and summarize the page")
    return result

import asyncio
print(asyncio.run(build_agent()))

💬 Deep Dive with AI

Key points

  • mcp-use removes the need to hand-implement MCP's capability handshake, transport layer, and tool-schema wiring
  • It sets up the MCP client, connects to one or more servers, discovers tools, and exposes them to the LLM automatically
  • The book's example builds a working MCP-enabled agent in about 6 lines of code, connecting to a Playwright server
  • Once set up, the LLM requests tool calls naturally during reasoning — mcp-use handles execution and streaming
  • This compresses substantial protocol boilerplate into a small setup step, keeping developer attention on application logic