
    6?i9                         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)zResources adapter for converting MCP resources to LangChain Blobs.

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       b/opt/tws/coop-23/cliente-mcp/venv/lib/python3.12/site-packages/langchain_mcp_adapters/resources.py&convert_mcp_resource_to_langchain_blobr      sl     (01}}	H2	3.1,@n>>tx/@/@ES_K`aa    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)zFetch a single MCP resource and convert it to LangChain Blobs.

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

    Returns:
        A list of LangChain Blobs.
    Nr   )read_resourcer	   lenr   )r   r   contents_resultcontents       r   get_mcp_resourcer"   $   so      $11#66O##s?+C+C'D'I	 M\LdLdAH.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)a  Load MCP resources and convert them to LangChain Blobs.

    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 Blobs.

    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   7   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b bHX b]a b,M  T
 , $((( S	/D
 ( 
$Z	(r   