
    "<i                    n    d Z ddlmZ ddlZddlZddlmZmZ ddlZ	ddl
Z
erddlmZ ddZddZddZdS )zWSEP-1686 task result converters.

Converts raw task return values to MCP result types.
    )annotationsN)TYPE_CHECKINGAny)FastMCPserverr   	raw_valuer   	tool_namestrclient_task_idreturnmcp.types.CallToolResultc                  K   ddl m}m} |                     |           d{V }dd|ii}t	          ||          r|                                }t	          |t          j        j                  r	||_	        |S t	          |t                    r'|\  }	}
t          j                            |	|
|          S t          j                            ||          S  |||j                  }d}
|j        B	 t          j        |          }t	          |t                    r|}
nK# t          j        $ r Y n:w xY wt          j        |          }|j                            d	          }|rd
|in|}
t          j                            ||
|          S )a  Convert raw tool return value to MCP CallToolResult.

    Replicates the serialization logic from tool.run() to properly handle
    output_schema, structured content, etc.

    Args:
        server: FastMCP server instance
        raw_value: The raw return value from user's tool function
        tool_name: Name of the tool (to get output_schema and serializer)
        client_task_id: Client task ID for related-task metadata

    Returns:
        CallToolResult with properly formatted content and structured content
    r   )
ToolResult_convert_to_contentN$modelcontextprotocol.io/related-tasktaskId)contentstructuredContent_meta)r   r   )
serializerzx-fastmcp-wrap-resultresult)fastmcp.tools.toolr   r   get_tool
isinstanceto_mcp_resultmcptypesCallToolResultr   tupler   output_schemapydantic_coreto_jsonable_pythondictPydanticSerializationErrorget)r   r   r	   r   r   r   toolrelated_task_meta
mcp_resultr   structured_contentunstructured_resultscjsonable_valuewrap_results                  /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/fastmcp/server/tasks/converters.pyconvert_tool_resultr/      s     & CBBBBBBB ++++++++D 	/n1
 )Z(( Y,,..
j#)":;; 	Y0J
E** 	Y*4'G'9++"4' ,    9++JFW+XXX .-iDOTTT !	1)<<B"d## (%'"7 	 	 	D	 '9)DD(,,-DEE*5IX~&&> 	 9###, $   s   1+D D/.D/prompt_namemcp.types.GetPromptResultc           
     
  K   ddl m} |                     |           d{V }t          |t          t
          z            s|g}g }|D ]}t          ||          r(|                    |                                           ;t          |t                    rT|                    t          j
                            dt          j
                            d|                               t          |t                    r8|                    t          j
        j                            |                     t          dt          |                     t          j
                            |j        pd	|d
d|ii          S )a  Convert raw prompt return value to MCP GetPromptResult.

    The user function returns raw values (strings, dicts, lists) that need
    to be converted to PromptMessage objects.

    Args:
        server: FastMCP server instance
        raw_value: The raw return value from user's prompt function
        prompt_name: Name of the prompt
        client_task_id: Client task ID for related-task metadata

    Returns:
        GetPromptResult with properly formatted messages
    r   )PromptMessageNusertext)typer5   )roler   zInvalid message type:  r   r   )descriptionmessagesr   )fastmcp.prompts.promptr3   
get_promptr   listr   appendto_mcpr
   r   r   TextContentr#   model_validate
ValueErrorr6   GetPromptResultr9   )r   r   r0   r   r3   promptr:   msgs           r.   convert_prompt_resultrF   `   s     " 544444 $$[11111111F i..  K	 /1H C Cc=)) 	COOCJJLL))))S!! 
	COO	''I11vC1HH (      T"" 	COOCI3BB3GGHHHHAd3iiAABBB9$$&,"2.5
 %       uridict[str, Any]c                  K   dd|ii}t          |t                    r
||ddg|dS t          |t                    r.|t          j        |                                          ddg|dS |t          j        |          ddg|dS )	a  Convert raw resource return value to MCP resource contents dict.

    Args:
        server: FastMCP server instance
        raw_value: The raw return value from user's resource function (str or bytes)
        uri: Resource URI (for the contents response)
        client_task_id: Client task ID for related-task metadata

    Returns:
        Dict with 'contents' key containing list of resource contents
    r   r   z
text/plain)rH   r5   mimeType)contentsr   zapplication/octet-stream)rH   blobrK   zapplication/json)r   r
   bytesbase64	b64encodedecodejsondumps)r   r   rH   r   r'   s        r.   convert_resource_resultrT      s        	/n1
 )S!! !
 % ,  '	
 	
 		
 
Iu	%	% 
 ",Y77>>@@ :  '	
 	
 		
  Jy11 2  '	
 	
 		
rG   )
r   r   r   r   r	   r
   r   r
   r   r   )
r   r   r   r   r0   r
   r   r
   r   r1   )
r   r   r   r   rH   r
   r   r
   r   rI   )__doc__
__future__r   rO   rR   typingr   r   	mcp.typesr   r!   fastmcp.server.serverr   r/   rF   rT    rG   r.   <module>r[      s    
 # " " " " "   % % % % % % % %         .------J J J JZ3 3 3 3l7
 7
 7
 7
 7
 7
rG   