View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/python/mcp_use/agents/mcpagent.py
- The agent uses create_agent() from langchain.agents which returns a CompiledStateGraph
- New methods: astream_simplified() and run_v2() leverage the built-in astream() from CompiledStateGraph which handles the agent loop internally
- Legacy methods: stream() and run() use manual step-by-step execution for backward compatibility
MCPAgent
method init
Initialize a new MCPAgent instance.ParametersSignatureThe LangChain LLM to use. Not required if agent_id is provided for remote execution.The MCPClient to use. If provided, connector is ignored.A list of MCP connectors to use if client is not provided.The maximum number of steps to take.Whether to automatically initialize the agent when run is called.Whether to maintain conversation history for context.Complete system prompt to use (overrides template if provided).Template for system prompt with placeholder.Extra instructions to append to the system prompt.List of tool names that should not be available to the agent.List of toolsWhether to use server manager mode instead of exposing all tools.Server name or configurationEnable debug/verbose modeRemote agent ID for remote execution. If provided, creates a remote agent.API key for remote execution. If None, checks MCP_USE_API_KEY env var.Base URL for remote API calls.List of LangChain callbacks to use. If None and Langfuse is configured, uses langfuse_handler.String valueWhether to retry tool calls that fail due to validation errors.Maximum number of retries for validation errors per step.
method run
Run a query using LangChain 1.0.0’s agent and return the final result.Example:ReturnsThe query to run.Optional maximum number of steps to take.Whether to handle the connector lifecycle internally.Optional external history to use instead of theOptional Pydantic BaseModel class for structured output.
SignatureThe result of running the query as a string, or if output_schema is provided, an instance of the specified Pydantic model.
method stream
Async generator using LangChain 1.0.0’s create_agent and astream.This method leverages the LangChain 1.0.0 API where create_agent returns
a CompiledStateGraph that handles the agent loop internally via astream.Tool Updates with Server Manager:
When using server_manager mode, this method handles dynamic tool updates:- Before execution: Updates are applied immediately to the new stream
- During execution: When tools change, we wait for a “safe restart point” (after tool results complete), then interrupt the stream, recreate the agent with new tools, and resume execution with accumulated messages.
- Safe restart points: Only restart after tool results to ensure message pairs (tool_use + tool_result) are complete, satisfying LLM API requirements.
- Max restarts: Limited to 3 restarts to prevent infinite loops
ReturnsThe query to run.Integer valueWhether to handle the connector lifecycle internally.Optional external history to use instead of theBoolean flagOptional Pydantic BaseModel class for structured output.
Signature
method stream_events
Asynchronous streaming interface.Example::async for chunk in agent.stream(“hello”):
print(chunk, end=”|”, flush=True)ParametersReturnsQuery string or inputInteger valueConnector instanceList of items
Signature