
    kKj:                        d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
 ddlZddlmZmZ erddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ  ee      ZdZeej:                  j<                  ej:                  j>                  z     Z  G d d      Z!y)z,Resource-related methods for FastMCP Client.    )annotationsN)TYPE_CHECKINGAnyLiteralcastoverload)AnyUrl	RootModel)Client)ResourceTask)client_span)inject_trace_context)
get_logger   c                  N   e Zd ZdZdd	 	 	 	 	 ddZef	 	 	 	 	 ddZdd	 	 	 	 	 ddZef	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	e
ddd	d
	 	 	 	 	 	 	 	 	 	 	 dd       Ze
ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zddd	ddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZy)ClientResourcesMixinz4Mixin providing resource-related methods for Client.Ncursorc               8  K   t        ddd| j                  j                               5  t        j	                  d| j
                   d       | j                  | j                  j                  |             d{   }|cddd       S 7 # 1 sw Y   yxY ww)a  Send a resources/list request and return the complete MCP protocol result.

        Args:
            cursor: Optional pagination cursor from a previous request's nextCursor.

        Returns:
            mcp.types.ListResourcesResult: The complete response object from the protocol,
                containing the list of resources and any additional metadata.

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        zresources/list 
session_id[z] called list_resourcesr   N)	r   	transportget_session_idloggerdebugname_await_with_session_monitoringsessionlist_resourcesselfr   results      p/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/client/mixins/resources.pylist_resources_mcpz'ClientResourcesMixin.list_resources_mcp#   s       ~~446	
 LL1TYYK'>?@>>++6+: F 
 

 
/   (BAB<B=B
BBBBc                  K   g }d}t               }t        |      D ]  }| j                  |       d{   }|j                  |j                         |j
                  s |S |j
                  |v r3t        j                  d| j                   d|j
                  d        |S |j                  |j
                         |j
                  } t        d| j                   d| d      7 w)a  Retrieve all resources available on the server.

        This method automatically fetches all pages if the server paginates results,
        returning the complete list. For manual pagination control (e.g., to handle
        large result sets incrementally), use list_resources_mcp() with the cursor parameter.

        Args:
            max_pages: Maximum number of pages to fetch before raising. Defaults to 250.

        Returns:
            list[mcp.types.Resource]: A list of all Resource objects.

        Raises:
            RuntimeError: If the page limit is reached before pagination completes.
            McpError: If the request results in a TimeoutError | JSONRPCError
        Nr   r   .] Server returned duplicate pagination cursor z( for list_resources; stopping pagination!] Reached auto-pagination limit (zn pages) for list_resources. Use list_resources_mcp() with cursor for manual pagination, or increase max_pages.)setranger&   extend	resources
nextCursorr   warningr   addRuntimeError)r#   	max_pagesall_resourcesr   seen_cursors_r$   s          r%   r!   z#ClientResourcesMixin.list_resources@   s	    ( 35!!$y!A22&2AAF  !1!12$$" !   L0		{ #)),,TV   V../&&F " DII; K  **  B   2C+C)B5C+c               8  K   t        ddd| j                  j                               5  t        j	                  d| j
                   d       | j                  | j                  j                  |             d{   }|cddd       S 7 # 1 sw Y   yxY ww)a:  Send a resources/listResourceTemplates request and return the complete MCP protocol result.

        Args:
            cursor: Optional pagination cursor from a previous request's nextCursor.

        Returns:
            mcp.types.ListResourceTemplatesResult: The complete response object from the protocol,
                containing the list of resource templates and any additional metadata.

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        zresources/templates/listr   r   r   z ] called list_resource_templatesr   N)	r   r   r   r   r   r   r   r    list_resource_templatesr"   s      r%   list_resource_templates_mcpz0ClientResourcesMixin.list_resource_templates_mcpo   s       &&~~446	
 LL1TYYK'GHI>>44F4C F 
 

 
r'   c                  K   g }d}t               }t        |      D ]  }| j                  |       d{   }|j                  |j                         |j
                  s |S |j
                  |v r3t        j                  d| j                   d|j
                  d        |S |j                  |j
                         |j
                  } t        d| j                   d| d      7 w)a  Retrieve all resource templates available on the server.

        This method automatically fetches all pages if the server paginates results,
        returning the complete list. For manual pagination control (e.g., to handle
        large result sets incrementally), use list_resource_templates_mcp() with the
        cursor parameter.

        Args:
            max_pages: Maximum number of pages to fetch before raising. Defaults to 250.

        Returns:
            list[mcp.types.ResourceTemplate]: A list of all ResourceTemplate objects.

        Raises:
            RuntimeError: If the page limit is reached before pagination completes.
            McpError: If the request results in a TimeoutError | JSONRPCError
        Nr   r   r)   z1 for list_resource_templates; stopping paginationr*   z pages) for list_resource_templates. Use list_resource_templates_mcp() with cursor for manual pagination, or increase max_pages.)r+   r,   r:   r-   resourceTemplatesr/   r   r0   r   r1   r2   )r#   r3   all_templatesr   r5   r6   r$   s          r%   r9   z,ClientResourcesMixin.list_resource_templates   s
    * ;=!!$y!A;;6;JJF  !9!9:$$$ #   L0		{ #)), -++
   V../&&F " DII; K  **  Kr7   c                  K   t        |      }t        dd|| j                  j                         |      5  t        j                  d| j                   d|        t        |t               rt        |      }t        |      }t        t        j                  j                  j                  dz  |      }|r|j                  d      }t        j                  j!                  t        j                  j#                  ||rt        j                  j$                  d
i |nd|            }| j'                  | j(                  j+                  |t        j                  j,                  	             d{   }n2| j'                  | j(                  j/                  |             d{   }|cddd       S 7 B7 # 1 sw Y   yxY ww)a  Send a resources/read request and return the complete MCP protocol result.

        Args:
            uri (AnyUrl | str): The URI of the resource to read. Can be a string or an AnyUrl object.
            meta (dict[str, Any] | None, optional): Request metadata (e.g., for SEP-1686 tasks). Defaults to None.

        Returns:
            mcp.types.ReadResourceResult: The complete response object from the protocol,
                containing the resource contents and any additional metadata.

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        zresources/read)r   resource_urir   z] called read_resource: Nzmodelcontextprotocol.io/taskuritask_metaparamsrequestresult_type )strr   r   r   r   r   r   
isinstancer	   r   r   mcptypesRequestParamsMetagetReadResourceRequestReadResourceRequestParamsTaskMetadatar   r    send_requestReadResourceResultread_resource)	r#   rA   metauri_strpropagated_metarequest_meta	task_dictrG   r$   s	            r%   read_resource_mcpz&ClientResourcesMixin.read_resource_mcp   s    " c(~~446 
 LL1TYYK'?uEF#s#Sk 348O		 7 7 < <t C_UL +//0NO	))7799>>DMSYY33@i@SW* ?  8   $BBLL-- '$'II$@$@ .     $BBLL..s3   I
 
4C
 
sA   4GD8F5.F1/2F5!F3"F5'
G1F53F55F>:GF)versionrW   rB   c                  K   y wNrI   )r#   rA   r]   rW   rB   s        r%   rV   z"ClientResourcesMixin.read_resource   s      QT   `  )r]   rW   task_idttlc                  K   y wr_   rI   )r#   rA   r]   rW   rB   rb   rc   s          r%   rV   z"ClientResourcesMixin.read_resource   s      r`   )r]   rW   rB   rb   rc   c                 K   |rt        |      ni }|i |j                  di       d|i|d<   |r!| j                  ||||xs d       d{   S t        |t              r	 t        |      }| j                  ||xs d       d{   }	|	j                  S 7 K# t        $ r}t        dt	        |            |d}~ww xY w7 ;w)a  Read the contents of a resource or resolved template.

        Args:
            uri (AnyUrl | str): The URI of the resource to read. Can be a string or an AnyUrl object.
            version (str | None): Specific version to read. If None, reads highest version.
            meta (dict[str, Any] | None): Optional request-level metadata.
            task (bool): If True, execute as background task (SEP-1686). Defaults to False.
            task_id (str | None): Optional client-provided task ID (auto-generated if not provided).
            ttl (int): Time to keep results available in milliseconds (default 60s).

        Returns:
            list[mcp.types.TextResourceContents | mcp.types.BlobResourceContents] | ResourceTask:
                A list of content objects if task=False, or a ResourceTask object if task=True.

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        Nfastmcpr]   )rW   z"Provided resource URI is invalid: )
dictrP   _read_resource_as_taskrK   rJ   r	   	Exception
ValueErrorr\   contents)
r#   rA   r]   rW   rB   rb   rc   rZ   er$   s
             r%   rV   z"ClientResourcesMixin.read_resource  s     @ &*tDzr'""9b1'7'L#
 44Wc(< 5    c3Sk
 --c8L-MM   8SE NsB   ACBC#B .CC	C	C$B<<CCc                  K   t        |      }t        t        j                  j                  j
                  dz  |      }t        |t              rt        |      }t        j                  j                  t        j                  j                  |t        j                  j                  |      |            }| j                  | j                  j                  |t                     d{   }|j                   }	t        |	t        j                  j"                        rm|	j$                  j&                  }
| j(                  j+                  |
       t-        | |
t        |      d      }t/        j0                  |      | j2                  |
<   |S |xs t        t5        j6                               }t-        | |t        |      |	j8                        S 7 w)a0  Read a resource for background execution (SEP-1686).

        Returns a ResourceTask object that handles both background and immediate execution.

        Args:
            uri: Resource URI to read
            task_id: Optional client-provided task ID (ignored, for backward compatibility)
            ttl: Time to keep results available in milliseconds (default 60s)
            meta: Optional metadata to pass with the request (e.g., version info)

        Returns:
            ResourceTask: Future-like object for accessing task status and results
        N)rc   r@   rD   rF   )rA   immediate_result)r   r   rL   rM   rN   rO   rK   rJ   r	   rQ   rR   rS   r   r    rT   ResourceTaskResponseUnionrootCreateTaskResultrB   taskId_submitted_task_idsr1   r   weakrefref_task_registryuuiduuid4rk   )r#   rA   rb   rc   rW   rY   rZ   rG   wrapped_result
raw_resultserver_task_idtask_objsynthetic_task_ids                r%   rh   z+ClientResourcesMixin._read_resource_as_taskA  s    , /t4CII33884?Qc3+C))//9966YY+++4" 7  0 
  $BBLL%%5 &  
 
 $((
j#))"<"<='__33N$$((8#n#c(TH 3:++h2GD/O !( <3tzz|+<!H!+!4!4	 +
s   C)G+G,C%G)r#   r   r   
str | Nonereturnzmcp.types.ListResourcesResult)r#   r   r3   intr   zlist[mcp.types.Resource])r#   r   r   r~   r   z%mcp.types.ListResourceTemplatesResult)r#   r   r3   r   r   z list[mcp.types.ResourceTemplate]r_   )r#   r   rA   AnyUrl | strrW   dict[str, Any] | Noner   zmcp.types.ReadResourceResult)r#   r   rA   r   r]   r~   rW   r   rB   zLiteral[False]r   zElist[mcp.types.TextResourceContents | mcp.types.BlobResourceContents])r#   r   rA   r   r]   r~   rW   r   rB   zLiteral[True]rb   r~   rc   r   r   r   )r#   r   rA   r   r]   r~   rW   r   rB   boolrb   r~   rc   r   r   zTlist[mcp.types.TextResourceContents | mcp.types.BlobResourceContents] | ResourceTask)Nra   N)r#   r   rA   r   rb   r~   rc   r   rW   r   r   r   )__name__
__module____qualname____doc__r&   AUTO_PAGINATION_MAX_PAGESr!   r:   r9   r\   r   rV   rh   rI       r%   r   r      s9   >
 /3!+	&> 3--- 
"-` /3!+	.> 3/// 
*/d HL66'6/D6	%6p 
 #&*$TTT 	T
 $T T 
OT T 
 #&*"			 		
 $	 	 	 	 
	 	 #&*"444 	4
 $4 4 4 4	4r #&*??? ? 	?
 $? 
?r   r   )"r   
__future__r   rw   rt   typingr   r   r   r   r   	mcp.typesrL   pydanticr	   r
   fastmcp.client.clientr   fastmcp.client.tasksr   fastmcp.client.telemetryr   fastmcp.telemetryr   fastmcp.utilities.loggingr   r   r   r   rM   rq   rU   ro   r   rI   r   r%   <module>r      st    2 "   > >  &, - 0 2 0	H	  &II!=!== 
b br   