
    i4                       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	 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eej6                  j8                  ej6                  j:                  z     Z G d d      Zy)z,Resource-related methods for FastMCP Client.    )annotationsN)TYPE_CHECKINGAnyLiteraloverload)AnyUrl	RootModel)Client)ResourceTask)client_span)inject_trace_context)
get_loggerc                  2   e Zd ZdZdd	 	 	 	 	 ddZddZdd	 	 	 	 	 ddZ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                  K   t         j                  d| j                   d       | j                  | j                  j                  |             d{   }|S 7 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] called list_resourcesr   N)loggerdebugname_await_with_session_monitoringsessionlist_resourcesselfr   results      o/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/client/mixins/resources.pylist_resources_mcpz'ClientResourcesMixin.list_resources_mcp!   sX       	q#:;<::LL''v'6
 
 
   AAAAc                  K   g }d}t               }	 | j                  |       d{   }|j                  |j                         |j                  s	 |S |j                  |v r3t
        j                  d| j                   d|j                  d       	 |S |j                  |j                         |j                  }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.

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

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        Nr   r   .] Server returned duplicate pagination cursor z( for list_resources; stopping pagination)	setr   extend	resources
nextCursorr   warningr   add)r   all_resourcesr   seen_cursorsr   s        r   r   z#ClientResourcesMixin.list_resources8   s      35!!$22&2AAF  !1!12$$    L0		{ #)),,TV   V../&&F A   %CB?BCc                  K   t         j                  d| j                   d       | j                  | j                  j                  |             d{   }|S 7 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
        r   z ] called list_resource_templatesr   N)r   r   r   r   r   list_resource_templatesr   s      r   list_resource_templates_mcpz0ClientResourcesMixin.list_resource_templates_mcpZ   sX       	q#CDE::LL000?
 
 
r    c                  K   g }d}t               }	 | j                  |       d{   }|j                  |j                         |j                  s	 |S |j                  |v r3t
        j                  d| j                   d|j                  d       	 |S |j                  |j                         |j                  }7 w)ah  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.

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

        Raises:
            RuntimeError: If called while the client is not connected.
            McpError: If the request results in a TimeoutError | JSONRPCError
        Nr   r   r"   z1 for list_resource_templates; stopping pagination)	r#   r.   r$   resourceTemplatesr&   r   r'   r   r(   )r   all_templatesr   r*   r   s        r   r-   z,ClientResourcesMixin.list_resource_templatesq   s      ;=!!$;;6;JJF  !9!9:$$    L0		{ #)), -++
   V../&&F Jr+   c                *  K   t        |      }t        d| d|| j                  j                         |      5  t        j                  d| j                   d|        t        |t               rt        |      }t        |      }|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 zresources/read)
session_idresource_urir   z] called read_resource: zmodelcontextprotocol.io/taskNuritask_metaparamsrequestresult_type )strr   	transportget_session_idr   r   r   
isinstancer   r   getmcptypesReadResourceRequestReadResourceRequestParamsTaskMetadatar   r   send_requestReadResourceResultread_resource)r   r6   metauri_strpropagated_meta	task_dictr<   r   s           r   read_resource_mcpz&ClientResourcesMixin.read_resource_mcp   so    " c(gY'~~446 
 LL1TYYK'?uEF#s#Sk 348O +//0NO	))7799>>DMSYY33@i@SW- ?  8   $BBLL-- '$'II$@$@ .     $BBLL..s3   G
 
2A
 
sA   7FDF F2F3F4F9
FFFFFF)versionrL   r7   c                  K   y wNr>   )r   r6   rQ   rL   r7   s        r   rK   z"ClientResourcesMixin.read_resource   s      QT   `  )rQ   rL   task_idttlc                  K   y wrS   r>   )r   r6   rQ   rL   r7   rV   rW   s          r   rK   z"ClientResourcesMixin.read_resource   s      rT   )rQ   rL   r7   rV   rW   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fastmcprQ   )rL   z"Provided resource URI is invalid: )
dictrC   _read_resource_as_taskrB   r?   r   	Exception
ValueErrorrP   contents)
r   r6   rQ   rL   r7   rV   rW   request_metaer   s
             r   rK   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                F  K   t        |      }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)        j*                  |
      | j,                  |	<   |
S |xs t        t/        j0                               }t'        | |t        |      |j2                        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
        )rW   r5   r9   r;   N)r6   immediate_result)r   rB   r?   r   rD   rE   rF   rG   rH   r   r   rI   ResourceTaskResponseUnionrootCreateTaskResultr7   taskId_submitted_task_idsr(   r   weakrefref_task_registryuuiduuid4r_   )r   r6   rV   rW   rL   rN   r<   wrapped_result
raw_resultserver_task_idtask_objsynthetic_task_ids               r   r\   z+ClientResourcesMixin._read_resource_as_task  s_    , /t4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   B8F!:F;C%F!)r   r
   r   
str | Nonereturnzmcp.types.ListResourcesResult)r   r
   rt   zlist[mcp.types.Resource])r   r
   r   rs   rt   z%mcp.types.ListResourceTemplatesResult)r   r
   rt   z list[mcp.types.ResourceTemplate]rS   )r   r
   r6   AnyUrl | strrL   dict[str, Any] | Nonert   zmcp.types.ReadResourceResult)r   r
   r6   ru   rQ   rs   rL   rv   r7   zLiteral[False]rt   zElist[mcp.types.TextResourceContents | mcp.types.BlobResourceContents])r   r
   r6   ru   rQ   rs   rL   rv   r7   zLiteral[True]rV   rs   rW   intrt   r   )r   r
   r6   ru   rQ   rs   rL   rv   r7   boolrV   rs   rW   rw   rt   zTlist[mcp.types.TextResourceContents | mcp.types.BlobResourceContents] | ResourceTask)NrU   N)r   r
   r6   ru   rV   rs   rW   rw   rL   rv   rt   r   )__name__
__module____qualname____doc__r   r   r.   r-   rP   r   rK   r\   r>       r   r   r      s   >
 /3!+	&. F /3!+	.."J HL55'5/D5	%5n 
 #&*$TTT 	T
 $T T 
OT T 
 #&*"			 		
 $	 	 	 	 
	 	 #&*"444 	4
 $4 4 4 4	4r #&*>>> > 	>
 $> 
>r}   r   ) r|   
__future__r   rl   ri   typingr   r   r   r   	mcp.typesrD   pydanticr   r	   fastmcp.client.clientr
   fastmcp.client.tasksr   fastmcp.client.telemetryr   fastmcp.telemetryr   fastmcp.utilities.loggingr   ry   r   rE   rf   rJ   rd   r   r>   r}   r   <module>r      sn    2 "   8 8  &, - 0 2 0	H	 &II!=!== 
z zr}   