LangChain.MCP.ContentMapper (LangChain MCP v0.2.0)
View SourceMaps MCP content arrays to LangChain ContentPart structures.
MCP tool responses contain a content array with multi-modal content items.
This module converts them to LangChain's ContentPart format for use in messages
and tool results.
MCP Content Types
MCP supports these content types:
text- Plain text contentimage- Base64 encoded image data with mimeTyperesource- Reference to a resource (file, URL, etc.)
LangChain ContentPart Types
These are mapped to:
:text- Text content:image- Base64 image with media type:file- File data (for resources):unsupported- For unknown types
Examples
# Text content
mcp_content = [%{"type" => "text", "text" => "Hello world"}]
parts = ContentMapper.to_content_parts(mcp_content)
# => [%ContentPart{type: :text, content: "Hello world"}]
# Image content
mcp_content = [
%{"type" => "image", "data" => "base64data...", "mimeType" => "image/png"}
]
parts = ContentMapper.to_content_parts(mcp_content)
# => [%ContentPart{type: :image, content: "base64data...", options: [media: "image/png"]}]
# Mixed content
mcp_content = [
%{"type" => "text", "text" => "See this image:"},
%{"type" => "image", "data" => "...", "mimeType" => "image/jpeg"}
]
parts = ContentMapper.to_content_parts(mcp_content)
# => [%ContentPart{type: :text, ...}, %ContentPart{type: :image, ...}]
Summary
Functions
Extracts just the text content from an MCP content array.
Converts LangChain ContentParts back to MCP content format.
Checks if MCP content contains any images.
Checks if MCP content contains any text.
Converts an MCP content array to a list of ContentPart structs.
Functions
Extracts just the text content from an MCP content array.
Useful when you only care about text responses and want to ignore other content types.
Parameters
mcp_content- List of MCP content items
Returns
- String of concatenated text content, or
nilif no text found
Examples
iex> mcp_content = [
...> %{"type" => "text", "text" => "Hello "},
...> %{"type" => "text", "text" => "world"},
...> %{"type" => "image", "data" => "..."}
...> ]
iex> ContentMapper.extract_text(mcp_content)
"Hello world"
@spec from_content_parts([LangChain.Message.ContentPart.t()]) :: [map()]
Converts LangChain ContentParts back to MCP content format.
This is useful for testing or when you need to send content back to an MCP server.
Parameters
content_parts- List of ContentPart structs
Returns
- List of MCP content maps
Checks if MCP content contains any images.
Examples
iex> mcp_content = [%{"type" => "image", "data" => "..."}]
iex> ContentMapper.has_images?(mcp_content)
true
Checks if MCP content contains any text.
Examples
iex> mcp_content = [%{"type" => "text", "text" => "Hello"}]
iex> ContentMapper.has_text?(mcp_content)
true
iex> mcp_content = [%{"type" => "image", "data" => "..."}]
iex> ContentMapper.has_text?(mcp_content)
false
@spec to_content_parts([map()]) :: [LangChain.Message.ContentPart.t()]
Converts an MCP content array to a list of ContentPart structs.
Parameters
mcp_content- List of MCP content items (maps)
Returns
- List of
ContentPart.t()structs
Examples
iex> mcp_content = [%{"type" => "text", "text" => "Result"}]
iex> [part] = ContentMapper.to_content_parts(mcp_content)
iex> part.type
:text
iex> part.content
"Result"