AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

MCP ResourceLink/EmbeddedResource Metadata Dropped When Converting To LangChain Content Blocks

The `_convert_content_block` function in `langchain.mcp` silently discards metadata fields (name, title, description, size, and URI for embedded resources) when converting MCP `ResourceLink` and `EmbeddedResource` objects to LangChain content blocks, leading to loss of information.

mediumConfidence 95%Langchain

Origin Analysis

The conversion function only extracts `uri` and `mime_type` from `ResourceLink` and `text` or `blob` from `EmbeddedResource`, ignoring other available fields because they are not explicitly forwarded via `**kwargs` to the `create_*_block` helper functions, which natively support passing extra metadata.
Install `mcp>=2.0` and `langchain[mcp]`, then import `_convert_content_block` from `langchain.mcp.tools` and call it with a `ResourceLink` containing name/title/description/size, or with an `EmbeddedResource` containing a `TextResourceContents` with a URI; observe that the returned content block lacks these fields.

Fixing Code Block

if isinstance(content, ResourceLink): mime_type = content.mime_type or None extra = { "name": content.name, "title": content.title, "description": content.description, "size": content.size, } extra = {k: v for k, v in extra.items() if v is not None} if mime_type and mime_type.startswith("image/"): return create_image_block(url=content.uri, mime_type=mime_type, **extra) return create_file_block(url=content.uri, mime_type=mime_type, **extra) if isinstance(content, EmbeddedResource): resource = content.resource if isinstance(resource, TextResourceContents): extra = {"uri": resource.uri} if resource.mime_type: extra["mime_type"] = resource.mime_type return create_text_block(text=resource.text, **extra) if isinstance(resource, BlobResourceContents): mime_type = resource.mime_type or None extra = {"uri": resource.uri} if mime_type and mime_type.startswith("image/"): return create_image_block(base64=resource.blob, mime_type=mime_type, **extra) return create_file_block(base64=resource.blob, mime_type=mime_type, **extra)
The fix extracts the additional metadata fields from the MCP objects and passes them as keyword arguments to the `create_*_block` functions. Since these functions accept arbitrary keyword arguments and store them in the content block's `extra` dictionary, the metadata is preserved without changing the primary content payload. Replace the original two `if isinstance(content, ...)` blocks in `_convert_content_block` with the provided code.

Edge Case Audit

This change introduces extra keys into content block extras that downstream code may not expect. Consumers that compare content blocks for exact equality or assume a specific extra structure could break. To mitigate, consider testing downstream integrations and providing a migration guide. If issues arise, rollback by reverting to the original code that discards metadata; no data corruption occurs, but metadata is lost again.

Ecosystem Topology