
    Ki-                        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y)zWSEP-1686 task result converters.

Converts raw task return values to MCP result types.
    )annotationsN)TYPE_CHECKINGAny)FastMCPc                  K   ddl m}m} | j                  |       d{   }dd|ii}t	        ||      r|j                         }t	        |t        j                  j                        r	||_	        |S t	        |t              r'|\  }	}
t        j                  j                  |	|
|      S t        j                  j                  ||      S  |||j                        }d}
|j                  )	 t        j                  |      }t	        |t              r|}
n8t        j                  |      }|j                  j#                  d	      }|rd
|in|}
t        j                  j                  ||
|      S 7 Q# t        j                   $ r Y :w xY ww)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)server	raw_value	tool_nameclient_task_idr   r	   toolrelated_task_meta
mcp_resultr   structured_contentunstructured_resultscjsonable_valuewrap_results                  k/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/server/tasks/converters.pyconvert_tool_resultr,      s    & C ++D 	/n1
 )Z(,,.
j#))":":;0J
E**4'G'99++"4' ,   99++JFW+XX .iDOOT !	11)<B"d#%'"
 '99)D((,,-DE*5X~&> 	 99###, $  a ,L 77 		s5   FE0CF.'E3 AF3F	FF		Fc           
       K   ddl m} | j                  |       d{   }t        |t        t
        z        s|g}g }|D ]  }t        ||      r|j                  |       !t        |t              rP|j                  t        j                  j                  dt        j                  j                  d|                   t        |t              r9|j                  t        j                  j                  j                  |             t        dt        |              t        j                  j                  |j                   xs d	|d
d|ii      S 7 9w)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)typer0   )roler   zInvalid message type:  r
   r   )descriptionmessagesr   )fastmcp.prompts.promptr.   
get_promptr   listr   appendstrr   r   TextContentr   model_validate
ValueErrorr1   GetPromptResultr4   )r   r    prompt_namer"   r.   promptr5   msgs           r+   convert_prompt_resultrB   `   s6    " 5 $$[11F i.K	 /1H Cc=)OOC S!OO		''II11vC1H (  T"OOCII33BB3GH5d3i[ABBC  99$$&&,"2.5
 %  1 2s   EED9Ec                   K   dd|ii}t        |t              r
||ddg|dS t        |t              r+|t        j                  |      j                         ddg|dS |t        j                  |      ddg|dS w)	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)urir0   mimeType)contentsr   zapplication/octet-stream)rD   blobrE   zapplication/json)r   r:   bytesbase64	b64encodedecodejsondumps)r   r    rD   r"   r$   s        r+   convert_resource_resultrN      s       	/n1
 )S! % , '	
 		
 
Iu	% ",,Y7>>@ : '	
 		
  JJy1 2 '	
 		
s   A9A;)
r   r   r    r   r!   r:   r"   r:   returnzmcp.types.CallToolResult)
r   r   r    r   r?   r:   r"   r:   rO   zmcp.types.GetPromptResult)
r   r   r    r   rD   r:   r"   r:   rO   zdict[str, Any])__doc__
__future__r   rI   rL   typingr   r   	mcp.typesr   r   fastmcp.server.serverr   r,   rB   rN        r+   <module>rW      s   
 #   %  -JJ #J03JEHJJZ44 #4254GJ44n7
7
 #7
*-7
?B7
7
rV   