Skip to main content
MCPAgent connects an LLM to one or more MCP servers. The agent discovers tools, resources, and prompts, exposes them to a LangChain-compatible chat model, runs a bounded multi-step loop, and returns the final answer. Use MCPAgent when you want the model to decide which MCP tools to call. Use MCPClient directly when your application should choose exact tools, resources, or prompts itself.

Key features

  • Tool-calling loop: Give the agent a prompt and let the LLM call MCP tools until it can answer.
  • Simplified or explicit setup: Pass "provider/model" plus mcpServers, or provide your own LangChain model and MCPClient.
  • Streaming: Stream tool-call steps or low-level LangChain events as the agent runs.
  • Structured output: Pass a Zod schema to get validated TypeScript data instead of plain text.
  • Conversation memory: Keep previous turns across run calls by default.
  • Multi-server routing: Enable Server Manager when many servers expose more tools than you want in context at once.

Installation

Install mcp-use and one LangChain provider package for the LLM you want to use.
Set the matching API key in your environment: OPENAI_API_KEY, ANTHROPIC_API_KEY, GOOGLE_API_KEY, or GROQ_API_KEY.

Quick start

This example connects to the filesystem MCP server and answers a question about the current directory. It uses simplified mode, so the agent creates the LangChain model and MCPClient during initialization.
agent.run() returns the final answer as a string. maxSteps defaults to 5; raise it for complex tasks with new MCPAgent({ ..., maxSteps: 10 }) or per call with agent.run({ prompt, maxSteps: 10 }). Always call agent.close() when the agent owns its client, or when you want to shut down explicit client connections.

Choose a setup mode

MCPAgent supports two construction patterns. For constructor options, defaults, run options, memory methods, streaming signatures, and observability methods, see the MCPAgent API Reference.

Core capabilities

Structured output

Pass a Zod schema to run to get a typed object back instead of a string.
See Structured Output for details.

Streaming

Iterate over agent.stream to receive each step as the agent runs.
agent.prettyStreamEvents and agent.streamEvents give formatted and low-level output. See Streaming.

Memory

Conversation memory is on by default, so the agent remembers earlier turns across run calls.
Pass memoryEnabled: false to new MCPAgent({ ... }) to disable it. See Memory Management.

Next steps

LLM Integration

Connect OpenAI, Anthropic, Google, or Groq.

Structured Output

Return typed responses with Zod schemas.

Streaming

Stream steps and events as the agent runs.

Memory Management

Keep or reset conversation history across runs.

Server Manager

Manage tools across multiple MCP servers.

Observability

Trace and monitor agent runs.

MCPAgent API Reference

Check constructor options, method signatures, and types.