mcp-use TypeScript API
    Preparing search index...

    Class LangChainMCPAgent

    Runs a LangChain tool-calling agent against MCP servers.

    Index
    • Creates a LangChain MCP agent.

      Parameters

      • options: MCPAgentOptions

        Model, MCP servers, tools, and execution settings.

      Returns LangChainMCPAgent

      Error if local execution does not include a model and MCP client, connectors, or server configurations.

    observabilityManager: ObservabilityManager

    Observability callbacks and trace lifecycle manager.

    toolsUsedNames: string[] = []

    Names of tools invoked during the current or most recent execution.

    • Clears stored history, retaining the system message when memory is enabled.

      Returns void

    • Flushes observability, closes owned MCP resources, and resets the executor.

      Returns Promise<void>

    • Flush observability traces to the configured observability platform. Important for serverless environments where traces need to be sent before function termination.

      Returns Promise<void>

    • Returns BaseMessage[]

      A shallow copy of the stored LangChain message history.

    • Returns string[]

      The configured MCP tool denylist.

    • Get current metadata

      Returns Record<string, any>

      A copy of the current metadata object

    • Returns SystemMessage<MessageStructure<MessageToolSet>> | null

      The current LangChain system message, or null before creation.

    • Get current tags

      Returns string[]

      A copy of the current tags array

    • Creates configured clients and models, connects MCP servers, loads tools, and builds the LangChain executor.

      Returns Promise<void>

      Error if a configured model or MCP server cannot be initialized.

    • Yields with pretty-printed output for code mode with options object.

      Parameters

      Returns AsyncGenerator<void, string, void>

    • Yields with pretty-printed output for code mode with options object and structured output.

      Type Parameters

      • T

      Parameters

      Returns AsyncGenerator<void, string, void>

    • Yields with pretty-printed output for code mode. This method formats and displays tool executions in a user-friendly way with syntax highlighting.

      Type Parameters

      • T = string

      Parameters

      • query: string
      • OptionalmaxSteps: number
      • OptionalmanageConnector: boolean
      • OptionalexternalHistory: BaseMessage[]
      • OptionaloutputSchema: ZodType<T, unknown, $ZodTypeInternals<T, unknown>>

      Returns AsyncGenerator<void, string, void>

      Use the options object instead: prettyStreamEvents({ prompt, maxSteps, ... }).

    • Runs the agent with options object and returns a promise for the final result.

      Parameters

      Returns Promise<string>

    • Runs the agent with options object and structured output, returns a promise for the typed result.

      Type Parameters

      • T

      Parameters

      Returns Promise<T>

    • Runs the agent and returns a promise for the final result.

      Parameters

      • query: string
      • OptionalmaxSteps: number
      • OptionalmanageConnector: boolean
      • OptionalexternalHistory: BaseMessage[]
      • OptionaloutputSchema: undefined
      • Optionalsignal: AbortSignal

      Returns Promise<string>

      Use the options object instead: run({ prompt, maxSteps, ... }).

    • Runs the agent with structured output and returns a promise for the typed result.

      Type Parameters

      • T

      Parameters

      • query: string
      • OptionalmaxSteps: number
      • OptionalmanageConnector: boolean
      • OptionalexternalHistory: BaseMessage[]
      • OptionaloutputSchema: ZodType<T, unknown, $ZodTypeInternals<T, unknown>>
      • Optionalsignal: AbortSignal

      Returns Promise<T>

      Use the options object instead: run({ prompt, schema, maxSteps, ... }).

    • Replaces the tool denylist for the next initialization.

      Parameters

      • disallowedTools: string[]

        MCP tool names to omit.

      Returns void

    • Set metadata for observability traces

      Parameters

      • newMetadata: Record<string, any>

        Key-value pairs to add to metadata. Keys should be strings, values should be serializable.

      Returns void

    • Replaces the system instruction and rebuilds an initialized executor.

      Parameters

      • message: string

        New system instruction.

      Returns void

    • Set tags for observability traces

      Parameters

      • newTags: string[]

        Array of tag strings to add. Duplicates will be automatically removed.

      Returns void

    • Streams the agent execution with options object and returns string result.

      Parameters

      Returns AsyncGenerator<LangChainAgentStep, string, void>

    • Streams the agent execution with options object and structured output.

      Type Parameters

      • T

      Parameters

      Returns AsyncGenerator<LangChainAgentStep, T, void>

    • Streams the agent execution and yields agent steps.

      Type Parameters

      • T = string

      Parameters

      • query: string
      • OptionalmaxSteps: number
      • OptionalmanageConnector: boolean
      • OptionalexternalHistory: BaseMessage[]
      • OptionaloutputSchema: ZodType<T, unknown, $ZodTypeInternals<T, unknown>>
      • Optionalsignal: AbortSignal

      Returns AsyncGenerator<LangChainAgentStep, string | T, void>

      Use the options object instead: stream({ prompt, maxSteps, ... }).

    • Yields LangChain StreamEvent objects with options object.

      Parameters

      Returns AsyncGenerator<StreamEvent, void, void>

    • Yields LangChain StreamEvent objects with options object and structured output.

      Type Parameters

      • T

      Parameters

      Returns AsyncGenerator<StreamEvent, void, void>

    • Yields LangChain StreamEvent objects from the underlying streamEvents() method. This provides token-level streaming and fine-grained event updates.

      Type Parameters

      • T = string

      Parameters

      • query: string
      • OptionalmaxSteps: number
      • OptionalmanageConnector: boolean
      • OptionalexternalHistory: BaseMessage[]
      • OptionaloutputSchema: ZodType<T, unknown, $ZodTypeInternals<T, unknown>>
      • Optionalsignal: AbortSignal

      Returns AsyncGenerator<StreamEvent, void, void>

      Use the options object instead: streamEvents({ prompt, maxSteps, ... }).

    • Get the mcp-use package version. Works in all environments (Node.js, browser, Cloudflare Workers, Deno, etc.)

      Returns string