View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/mcp_use/agents/mcpagent.py
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 the MCP tools and return the final result.This method uses the streaming implementation internally and returns
the final result after consuming all intermediate steps. If output_schema
is provided, the agent will be schema-aware and return structured output.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
Run the agent and yield intermediate steps as an async generator.Yields:
Intermediate steps as (AgentAction, str) tuples, followed by the final result.
If output_schema is provided, yields structured output as instance of the schema.ParametersReturnsThe query to run.Optional maximum number of steps to take.Whether to handle the connector lifecycle internally.Optional external history to use instead of theWhether to track execution for telemetry.Optional Pydantic BaseModel class for structured output.
Signature
method
stream_events
Asynchronous streaming interface.Example::async for chunk in agent.astream(“hello”):
print(chunk, end=”|”, flush=True)ParametersReturnsQuery string or inputInteger valueConnector instanceList of items
Signature