MCPAgent from @mcp-use/agent connects a model to MCP tools, runs a bounded tool-calling loop, and returns the model’s final answer.
Install the agent
@mcp-use/agent requires Node.js 22.22.2 or later.
Run your first agent
SetOPENAI_API_KEY, then run this example from a directory the filesystem server may read.
Initialization is manual by default. Call
await agent.initialize() before
run(), stream(), or streamEvents(). Set autoInitialize: true only when
you want the first call to initialize the agent for you.Understand the runtime defaults
maxSteps defaults to 10. Override it for the agent or for one call:
run() and stream() calls. It does not store tool transcripts or streamEvents() output.
When you pass mcpServers, the agent creates and owns its MCPClient; close() closes those sessions. If you pass an existing client or connectors, their lifecycle remains under your control.
Choose the next guide
LLM providers
Configure OpenAI, Anthropic, Google, OpenRouter, Ollama, or an OpenAI-compatible endpoint.
Memory management
Inspect, disable, and clear conversation history.
Streaming
Stream tool steps or provider-neutral events.
LangChain integration
Add LangChain models, structured output, Server Manager, and callbacks.