
    [ǻi                         d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	 de
dedefd	Zd
ede
dee   fdZddd
ede
ee
   z  dz  dee   fdZy)a   Resources adapter for converting MCP resources to LangChain [Blob objects][langchain_core.documents.base.Blob].

This module provides functionality to convert MCP resources into LangChain Blob
objects, handling both text and binary resource content types.
    N)Blob)ClientSession)BlobResourceContentsResourceContentsTextResourceContentsresource_uricontentsreturnc                    t        |t              r|j                  }n@t        |t              r t	        j
                  |j                        }nd|  }t        |      t        j                  ||j                  d| i      S )zConvert an MCP resource content to a LangChain Blob.

    Args:
        resource_uri: URI of the resource
        contents: The resource contents

    Returns:
        A LangChain Blob

    z!Unsupported content type for URI uri)data	mime_typemetadata)
isinstancer   textr   base64	b64decodeblob	TypeErrorr   	from_datamimeType)r   r	   r   msgs       ^/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/langchain_mcp_adapters/resources.py&convert_mcp_resource_to_langchain_blobr      sp     (01}}	H2	3.1,@n>>X..%9N     sessionr   c                    K   | j                  |       d{   }|j                  rt        |j                        dk(  rg S |j                  D cg c]  }t        ||       c}S 7 Nc c}w w)a)  Fetch a single MCP resource and convert it to LangChain [Blob objects][langchain_core.documents.base.Blob].

    Args:
        session: MCP client session.
        uri: URI of the resource to fetch.

    Returns:
        A list of LangChain [Blob][langchain_core.documents.base.Blob] objects.
    Nr   )read_resourcer	   lenr   )r   r   contents_resultcontents       r   get_mcp_resourcer"   (   so      $11#66O##s?+C+C'D'I	 '// 	/sG< 	 7s!   A-A&8A-A(#A-(A-)urisr#   c                t  K   g }|<| j                          d{   }|j                  D cg c]  }|j                   }}nt        |t              r|g}n|}d}	 |D ])  }|}t        | |       d{   }|j                  |       + 	 |S 7 rc c}w 7 !# t        $ r}	d| }
t        |
      |	d}	~	ww xY ww)az  Load MCP resources and convert them to LangChain [Blob objects][langchain_core.documents.base.Blob].

    Args:
        session: MCP client session.
        uris: List of URIs to load. If `None`, all resources will be loaded.

            !!! note

                Dynamic resources will NOT be loaded when `None` is specified,
                as they require parameters and are ignored by the MCP SDK's
                `session.list_resources()` method.

    Returns:
        A list of LangChain [Blob][langchain_core.documents.base.Blob] objects.

    Raises:
        RuntimeError: If an error occurs while fetching a resource.
    NzError fetching resource )	list_resources	resourcesr   r   strr"   extend	ExceptionRuntimeError)r   r#   blobsresources_listruri_listcurrent_urir   resource_blobser   s              r   load_mcp_resourcesr2   <   s     . E|&5577#1#;#;<aAEE<<	D#	6K' 	)CK#3GS#AANLL(	) L# 8< B '(63Q&'sV   B8BB8B B8B 2B3B B8B8B 	B5B00B55B8)__doc__r   langchain_core.documents.baser   mcpr   	mcp.typesr   r   r   r'   r   listr"   r2    r   r   <module>r9      s     .  R R!1	4M  T
 . $(++ S	/D
 + 
$Z	+r   