Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/mcp-use/mcp-use/llms.txt

Use this file to discover all available pages before exploring further.

What are MCP Agents?

MCP Agents are AI-powered assistants that can reason, plan, and execute actions using tools from MCP servers. They combine Large Language Models (LLMs) with MCP tools to accomplish complex, multi-step tasks.

Autonomous

Agents reason about tasks and decide which tools to use

Tool-Enabled

Access to any MCP server’s tools, resources, and prompts

Multi-Step

Execute complex workflows across multiple tools

LLM-Agnostic

Works with OpenAI, Anthropic, Groq, and more

Quick Start

Installation

npm install mcp-use @langchain/openai langchain

Basic Agent

import { ChatOpenAI } from "@langchain/openai";
import { MCPAgent, MCPClient } from "mcp-use";
import "dotenv/config";

async function main() {
  // 1. Configure MCP servers
  const config = {
    mcpServers: {
      filesystem: {
        command: "npx",
        args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
      },
    },
  };

  // 2. Create client
  const client = MCPClient.fromDict(config);

  // 3. Create LLM
  const llm = new ChatOpenAI({ modelName: "gpt-4o" });

  // 4. Create agent
  const agent = new MCPAgent({ llm, client, maxSteps: 20 });

  // 5. Run a query
  const result = await agent.run("List all files in /tmp and summarize them");
  console.log(result);
}

main();

Agent Execution Modes

MCPAgent supports three execution modes for different use cases:

1. Simple Run (Blocking)

Best for simple queries where you only need the final result:
const result = await agent.run("Search for restaurants in Tokyo");
console.log(result);
// Output: "I found 10 highly-rated restaurants in Tokyo..."

2. Streaming Steps

Best for debugging and understanding the agent’s reasoning:
const stream = agent.stream("Find a restaurant and book a table");

for await (const step of stream) {
  console.log(`\n--- Step ${step.step} ---`);
  console.log(`Tool: ${step.action.tool}`);
  console.log(`Input:`, step.action.toolInput);
  console.log(`Result:`, step.observation);
}
// Final result is the return value
Output:
--- Step 1 ---
Tool: search_restaurants
Input: { city: "Tokyo", cuisine: "Japanese" }
Result: Found 5 restaurants...

--- Step 2 ---
Tool: check_availability
Input: { restaurant: "Sushi Master", date: "2024-01-15" }
Result: Available times: 18:00, 19:00, 20:00...

--- Step 3 ---
Tool: book_table
Input: { restaurant: "Sushi Master", time: "19:00", guests: 2 }
Result: Booking confirmed!

3. Stream Events (Token-Level)

Best for real-time UIs with token-by-token streaming:
const eventStream = agent.streamEvents("What's the weather today?");

for await (const event of eventStream) {
  switch (event.event) {
    case "on_chat_model_stream":
      // Token-by-token streaming from LLM
      if (event.data?.chunk?.content) {
        process.stdout.write(event.data.chunk.content);
      }
      break;

    case "on_tool_start":
      console.log(`\n🔧 Starting tool: ${event.name}`);
      break;

    case "on_tool_end":
      console.log(`✅ Tool completed: ${event.name}`);
      break;

    case "on_chain_end":
      console.log("\n🏁 Agent finished");
      break;
  }
}

LLM Providers

MCPAgent works with any LangChain-compatible LLM:
import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  modelName: "gpt-4o",
  temperature: 0,
});

const agent = new MCPAgent({ llm, client });
Only models with tool calling capabilities can be used with MCPAgent. Ensure your chosen model supports function calling.

Multi-Server Agents

Connect to multiple MCP servers for more capabilities:
const config = {
  mcpServers: {
    filesystem: {
      command: "npx",
      args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
    },
    playwright: {
      command: "npx",
      args: ["@playwright/mcp@latest"],
    },
    airbnb: {
      command: "npx",
      args: ["-y", "@openbnb/mcp-server-airbnb"],
    },
  },
};

const client = MCPClient.fromDict(config);
const agent = new MCPAgent({ llm, client, maxSteps: 30 });

// Agent can use tools from all three servers
const result = await agent.run(
  "Search for accommodations in Barcelona on Airbnb, " +
  "then use Google to find nearby restaurants"
);

Dynamic Server Selection

Enable smart server selection for better performance:
const agent = new MCPAgent({
  llm,
  client,
  useServerManager: true,  // Enable smart server selection
});

// Agent automatically selects the right server for each tool
// Reduces tool confusion and improves efficiency
When useServerManager is enabled:
  • Agent only sees relevant tools for each step
  • Reduces context window usage
  • Improves tool selection accuracy
  • Faster execution

Tool Access Control

Restrict which tools the agent can access:
const agent = new MCPAgent({
  llm,
  client,
  disallowedTools: [
    "delete_file",       // Dangerous operations
    "execute_command",
    "system_shutdown",
  ],
});

// Agent cannot use restricted tools
// Attempts to use them will be blocked

Conversation Memory

Add memory for multi-turn conversations:
import { BufferMemory } from "langchain/memory";

const memory = new BufferMemory({
  returnMessages: true,
  memoryKey: "chat_history",
});

const agent = new MCPAgent({
  llm,
  client,
  memory,
});

// First query
await agent.run("My name is Alice");
// Agent: "Nice to meet you, Alice!"

// Second query (remembers context)
await agent.run("What's my name?");
// Agent: "Your name is Alice!"

Observability

Monitor agent performance with Langfuse:

Setup

npm install langfuse langfuse-langchain
Create .env:
LANGFUSE_PUBLIC_KEY=pk-lf-your-key
LANGFUSE_SECRET_KEY=sk-lf-your-secret
LANGFUSE_HOST=https://cloud.langfuse.com

Usage

const agent = new MCPAgent({
  llm,
  client,
  observe: true,  // Enable observability
});

// Set custom metadata
agent.setMetadata({
  userId: "user123",
  sessionId: "session456",
  environment: "production",
});

// Set tags for organization
agent.setTags(["production", "customer-support", "priority-high"]);

// Run queries (automatically tracked)
const result = await agent.run("Complex multi-step task");

// View in Langfuse dashboard:
// - Token usage
// - Latency
// - Tool calls
// - Errors
// - Cost

Monitoring Events

const eventStream = agent.streamEvents("Query");

for await (const event of eventStream) {
  switch (event.event) {
    case "on_llm_start":
      console.log("LLM call started:", event.data);
      break;
    case "on_tool_start":
      console.log("Tool execution started:", event.name);
      break;
    case "on_tool_end":
      console.log("Tool execution completed:", event.name);
      break;
  }
}

Advanced Examples

Web Research Agent

import { ChatOpenAI } from "@langchain/openai";
import { MCPAgent, MCPClient } from "mcp-use";

async function webResearchAgent() {
  const config = {
    mcpServers: {
      playwright: {
        command: "npx",
        args: ["@playwright/mcp@latest"],
      },
    },
  };

  const client = MCPClient.fromDict(config);
  const llm = new ChatOpenAI({ modelName: "gpt-4o" });
  const agent = new MCPAgent({ llm, client, maxSteps: 30 });

  const result = await agent.run(`
    Research the latest developments in quantum computing.
    
    Tasks:
    1. Search for recent news articles
    2. Find academic papers from 2024
    3. Summarize the top 3 breakthroughs
    4. Identify leading researchers in the field
  `);

  console.log(result);
}

webResearchAgent();

Data Analysis Agent

async function dataAnalysisAgent() {
  const config = {
    mcpServers: {
      filesystem: {
        command: "npx",
        args: ["-y", "@modelcontextprotocol/server-filesystem", "./data"],
      },
    },
  };

  const client = MCPClient.fromDict(config);
  const llm = new ChatOpenAI({ modelName: "gpt-4o" });
  const agent = new MCPAgent({ llm, client, maxSteps: 20 });

  const result = await agent.run(`
    Analyze the sales data in sales_2024.csv:
    
    1. Load and parse the CSV file
    2. Calculate total revenue by month
    3. Identify the top 5 products
    4. Find any seasonal trends
    5. Create a summary report
  `);

  console.log(result);
}

Travel Planning Agent

async function travelPlanningAgent() {
  const config = {
    mcpServers: {
      airbnb: {
        command: "npx",
        args: ["-y", "@openbnb/mcp-server-airbnb"],
      },
      playwright: {
        command: "npx",
        args: ["@playwright/mcp@latest"],
      },
    },
  };

  const client = MCPClient.fromDict(config);
  const llm = new ChatOpenAI({ modelName: "gpt-4o" });
  const agent = new MCPAgent({
    llm,
    client,
    maxSteps: 50,
    useServerManager: true,
  });

  const result = await agent.run(`
    Plan a 5-day trip to Barcelona for 2 people:
    
    1. Find accommodations on Airbnb (budget: $150/night)
    2. Research top attractions and restaurants
    3. Create a day-by-day itinerary
    4. Estimate total trip cost
    5. Provide travel tips and recommendations
  `);

  console.log(result);
}

Best Practices

const agent = new MCPAgent({
  llm,
  client,
  maxSteps: 20,  // Adjust based on task complexity
});
  • Simple tasks: 5-10 steps
  • Medium complexity: 10-20 steps
  • Complex workflows: 20-50 steps
  • Prevents infinite loops
Good:
"Search Google for restaurants in Tokyo with ratings above 4.5,
then summarize the top 3 results"
Bad:
"Find restaurants"
Clear instructions lead to better results.
try {
  const result = await agent.run("Query");
  console.log(result);
} catch (error) {
  if (error.code === "MAX_STEPS_EXCEEDED") {
    console.error("Agent took too many steps");
  } else if (error.code === "TOOL_ERROR") {
    console.error("Tool execution failed");
  } else {
    console.error("Unexpected error:", error);
  }
} finally {
  await client.closeAllSessions();
}
agent.setMetadata({ userId: "user123" });
agent.observe = true;

const result = await agent.run("Query");

// Check Langfuse dashboard for:
// - Token count
// - Cost
// - Latency
Different LLMs have different strengths:
  • GPT-4o: Best overall, excellent tool calling
  • Claude Sonnet: Great reasoning, good for complex tasks
  • GPT-3.5: Fast, cost-effective for simple tasks
  • Llama 3: Free, good for privacy-sensitive applications

Troubleshooting

Problem: Agent returns answers without calling toolsSolutions:
  • Make instructions more explicit
  • Verify tools are loaded: await client.getTools()
  • Check LLM supports tool calling
  • Increase temperature slightly (0.1-0.3)
Problem: Agent calls same tool multiple timesSolutions:
  • Reduce maxSteps
  • Make tool descriptions clearer
  • Add explicit “stop” conditions in prompt
  • Use streaming to detect loops early
Problem: Agent exceeds maximum stepsSolutions:
  • Break task into smaller subtasks
  • Increase maxSteps if task is legitimately complex
  • Simplify the query
  • Check for tool errors causing retries
Problem: Tools fail during executionSolutions:
  • Verify server is running
  • Check tool parameters
  • Look at server logs
  • Test tools directly in inspector

Next Steps

Build MCP Servers

Create custom MCP servers for your agents

Deploy Agents

Deploy agents to production with Manufact

Examples

Browse agent examples and templates

API Reference

Complete agent API documentation

Build docs developers (and LLMs) love