LangChain.MCP.Config (LangChain MCP v0.2.0)

View Source

Configuration structure for MCP adapter.

Holds settings for MCP client integration including caching, timeouts, fallback behavior, and tool filtering.

Fields

  • :client - The Anubis.Client module to use for MCP operations (required)
  • :cache_tools - Whether to cache discovered tools (default: true)
  • :timeout - Timeout for tool calls in milliseconds (default: 30_000)
  • :async - Whether to mark tools as async (default: false)
  • :fallback_client - Optional fallback client module if primary fails
  • :before_fallback - Optional function called before fallback, receives (config, tool_name, args)
  • :tool_filter - Optional function to filter tools, receives tool map, returns boolean
  • :context - Optional context map passed to tool execution callbacks

Examples

# Basic configuration
config = Config.new!(client: MyApp.MCPClient)

# With fallback
config = Config.new!(
  client: MyApp.PrimaryMCP,
  fallback_client: MyApp.BackupMCP,
  before_fallback: fn _config, tool_name, _args ->
    Logger.warning("Falling back for tool: #{tool_name}")
    :continue
  end
)

# With tool filtering
config = Config.new!(
  client: MyApp.MCPClient,
  tool_filter: fn tool ->
    tool["name"] not in ["dangerous_tool", "admin_only"]
  end
)

Summary

Functions

Calls the before_fallback callback if configured.

Applies the tool filter function if configured.

Returns true if a fallback client is configured.

Creates a new Config struct with validation, returning ok/error tuple.

Creates a new Config struct with validation.

Types

t()

@type t() :: %LangChain.MCP.Config{
  async: boolean(),
  before_fallback: function() | nil,
  cache_tools: boolean(),
  client: module(),
  context: map(),
  fallback_client: module() | nil,
  timeout: pos_integer(),
  tool_filter: function() | nil
}

Functions

before_fallback(config, tool_name, args)

@spec before_fallback(t(), String.t(), map()) :: :continue | :skip

Calls the before_fallback callback if configured.

Returns :continue to proceed with fallback, :skip to skip it.

Examples

iex> config = Config.new!(client: MyApp.MCP)
iex> Config.before_fallback(config, "tool_name", %{})
:continue

iex> config = Config.new!(
...>   client: MyApp.MCP,
...>   before_fallback: fn _, _, _ -> :skip end
...> )
iex> Config.before_fallback(config, "tool_name", %{})
:skip

filter_tool?(config, tool)

@spec filter_tool?(t(), map()) :: boolean()

Applies the tool filter function if configured.

Returns true if no filter is configured or if the filter returns true.

Examples

iex> config = Config.new!(client: MyApp.MCP)
iex> Config.filter_tool?(config, %{"name" => "any_tool"})
true

iex> config = Config.new!(client: MyApp.MCP, tool_filter: fn t -> t["name"] == "allowed" end)
iex> Config.filter_tool?(config, %{"name" => "allowed"})
true
iex> Config.filter_tool?(config, %{"name" => "blocked"})
false

has_fallback?(config)

@spec has_fallback?(t()) :: boolean()

Returns true if a fallback client is configured.

Examples

iex> config = Config.new!(client: MyApp.MCP)
iex> Config.has_fallback?(config)
false

iex> config = Config.new!(client: MyApp.MCP, fallback_client: MyApp.Backup)
iex> Config.has_fallback?(config)
true

new(attrs)

@spec new(keyword()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Creates a new Config struct with validation, returning ok/error tuple.

See new!/1 for options.

new!(attrs)

@spec new!(keyword()) :: t() | no_return()

Creates a new Config struct with validation.

Options

  • :client - Required. The Anubis.Client module
  • :cache_tools - Boolean, default true
  • :timeout - Positive integer in ms, default 30_000
  • :async - Boolean, default false
  • :fallback_client - Optional module
  • :before_fallback - Optional 3-arity function
  • :tool_filter - Optional 1-arity function
  • :context - Optional map

Examples

iex> Config.new!(client: MyApp.MCPClient)
%Config{client: MyApp.MCPClient, cache_tools: true, ...}

iex> Config.new!(client: MyApp.MCPClient, timeout: 60_000, async: true)
%Config{client: MyApp.MCPClient, timeout: 60_000, async: true, ...}