LangChain.MCP.SchemaConverter (LangChain MCP v0.2.0)

View Source

Converts MCP tool input schemas (JSON Schema format) to LangChain FunctionParam structures.

MCP tools define their parameters using JSON Schema format. This module handles the conversion to LangChain's FunctionParam format, supporting:

  • Simple types (string, integer, number, boolean)
  • Arrays (with item types)
  • Objects (with nested properties)
  • Enums
  • Required fields
  • Descriptions

Examples

# Simple parameter
schema = %{
  "type" => "object",
  "properties" => %{
    "query" => %{"type" => "string", "description" => "Search query"}
  },
  "required" => ["query"]
}

params = SchemaConverter.to_parameters(schema)
# => [%FunctionParam{name: "query", type: :string, description: "Search query", required: true}]

# Complex nested object
schema = %{
  "type" => "object",
  "properties" => %{
    "user" => %{
      "type" => "object",
      "properties" => %{
        "name" => %{"type" => "string"},
        "age" => %{"type" => "integer"}
      },
      "required" => ["name"]
    }
  }
}

params = SchemaConverter.to_parameters(schema)

Summary

Functions

Converts a single property from JSON Schema to FunctionParam.

Converts LangChain FunctionParam list back to JSON Schema format.

Converts a JSON Schema object to a list of LangChain FunctionParam structs.

Types

json_schema()

@type json_schema() :: map()

Functions

convert_property(name, prop_schema, required \\ false)

@spec convert_property(String.t(), map(), boolean()) ::
  LangChain.FunctionParam.t() | nil

Converts a single property from JSON Schema to FunctionParam.

Parameters

  • name - Property name
  • prop_schema - Property schema map
  • required - Whether this property is required

Returns

  • FunctionParam.t() or nil if conversion fails

from_parameters(params)

@spec from_parameters([LangChain.FunctionParam.t()]) :: json_schema()

Converts LangChain FunctionParam list back to JSON Schema format.

This is useful for debugging or when you need to reconstruct the original schema.

Parameters

  • params - List of FunctionParam structs

Returns

  • JSON Schema map

Examples

iex> params = [
...>   FunctionParam.new!(%{name: "query", type: :string, required: true})
...> ]
iex> schema = SchemaConverter.from_parameters(params)
iex> schema["type"]
"object"
iex> schema["required"]
["query"]

to_parameters(schema)

@spec to_parameters(json_schema()) :: [LangChain.FunctionParam.t()]

Converts a JSON Schema object to a list of LangChain FunctionParam structs.

The schema should be an object with type: "object", properties, and optionally required.

Parameters

  • schema - JSON Schema map (typically from MCP tool's inputSchema)

Returns

  • List of FunctionParam structs

Examples

iex> schema = %{
...>   "type" => "object",
...>   "properties" => %{
...>     "name" => %{"type" => "string"},
...>     "count" => %{"type" => "integer"}
...>   },
...>   "required" => ["name"]
...> }
iex> params = SchemaConverter.to_parameters(schema)
iex> length(params)
2
iex> Enum.find(params, & &1.name == "name").required
true