o
    inF                     @  s(  d Z ddlm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dlZddlmZ ddlZddlmZ ddlmZ eG d	d
 d
ZeG dd dZeG dd dZeG dd dZeG dd dZd+ddZd,ddZd-ddZG dd deeZd.d"d#Zd/d$d%Z	d0d1d)d*Z dS )2z+Utilities for inspecting FastMCP instances.    )annotationsN)	dataclass)Enum)AnyLiteralcast)FastMCP)Clientc                   @  s   e Zd ZU dZded< ded< ded< ded< d	Zd
ed< d	Zd
ed< d	Zded< d	Zded< d	Z	ded< d	Z
ded< d	Zd
ed< d	S )ToolInfozInformation about a tool.strkeyname
str | Nonedescriptiondict[str, Any]input_schemaNdict[str, Any] | Noneoutput_schemar   list[str] | Nonetagsbool | Noneenabledtitlelist[dict[str, Any]] | Noneiconsmeta)__name__
__module____qualname____doc____annotations__r   r   r   r   r   r   r    r!   r!   _/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/utilities/inspect.pyr
         
 r
   c                   @  sr   e Zd ZU dZded< ded< ded< dZded	< dZd
ed< dZded< dZded< dZ	ded< dZ
ded< dS )
PromptInfozInformation about a prompt.r   r   r   r   r   Nr   	argumentsr   r   r   r   r   r   r   r   )r   r   r   r   r    r%   r   r   r   r   r   r!   r!   r!   r"   r$   #   s   
 r$   c                   @  s   e Zd ZU dZded< ded< ded< ded< dZded	< dZd
ed< dZded< dZded< dZ	ded< dZ
ded< dZd
ed< dS )ResourceInfozInformation about a resource.r   r   urir   r   r   N	mime_typer   r   r   r   r   r   r   r   r   r   )r   r   r   r   r    r(   r   r   r   r   r   r   r!   r!   r!   r"   r&   2   r#   r&   c                   @  s   e Zd ZU dZded< ded< ded< ded< dZded	< dZd
ed< dZd
ed< dZded< dZ	ded< dZ
ded< dZded< dZd
ed< dS )TemplateInfoz&Information about a resource template.r   r   uri_templater   r   r   Nr(   r   
parametersr   r   r   r   r   r   r   r   r   )r   r   r   r   r    r(   r+   r   r   r   r   r   r   r!   r!   r!   r"   r)   C   s   
 r)   c                   @  sz   e Zd ZU dZded< ded< ded< ded< ded	< ded
< ded< ded< ded< ded< ded< ded< ded< dS )FastMCPInfoz.Information extracted from a FastMCP instance.r   r   r   instructionsversionwebsite_urlr   r   fastmcp_versionmcp_versionintserver_generationzlist[ToolInfo]toolszlist[PromptInfo]promptszlist[ResourceInfo]	resourceszlist[TemplateInfo]	templatesr   capabilitiesN)r   r   r   r   r    r!   r!   r!   r"   r,   U   s   
 r,   mcpFastMCP[Any]returnc                   s  |   I dH }|  I dH }|  I dH }|  I dH }g }|D ]F}|j|jd}|t|j|jp4|j|j	|j
r<|j
ni |j|jrG|j nd|jrPt|jnd|j|j|jr`dd |jD nd|jd q!g }|D ]9}	|t|	j|	jpx|	j|	j	|	jrdd |	jD nd|	jrt|	jnd|	j|	j|	jrdd |	jD nd|	jd	 qlg }
|D ]7}|
t|j|j|j|j	|j|jr|j nd|jrt|jnd|j|j|jrd	d |jD nd|jd
 qg }|D ];}|t|j|j|j|j	|j|j|jr|j nd|jr	t|jnd|j|j|jrdd |jD nd|jd qddidddddii d}t| jdrE| jjrEdd | jjD nd}t| jdrR| jjnd}t| j| jt| drb| jn| jj||t j!t"j#dd|||
||dS )zExtract information from a FastMCP v2.x instance.

    Args:
        mcp: The FastMCP v2.x instance to inspect

    Returns:
        FastMCPInfo dataclass containing the extracted information
    N)r   c                 S     g | ]}|  qS r!   
model_dump.0iconr!   r!   r"   
<listcomp>       z&inspect_fastmcp_v2.<locals>.<listcomp>r   r   r   r   r   r   r   r   r   r   r   c                 S  r<   r!   r=   r@   argr!   r!   r"   rB      rC   c                 S  r<   r!   r=   r?   r!   r!   r"   rB      rC   	r   r   r   r%   r   r   r   r   r   c                 S  r<   r!   r=   r?   r!   r!   r"   rB      rC   r   r'   r   r   r(   r   r   r   r   r   r   c                 S  r<   r!   r=   r?   r!   r!   r"   rB      rC   r   r*   r   r   r(   r+   r   r   r   r   r   r   listChangedTF	subscriberJ   r4   r6   r5   loggingr   c                 S  r<   r!   r=   r?   r!   r!   r"   rB      rC   r/   r.   r9      r   r-   r.   r/   r   r0   r1   r3   r4   r5   r6   r7   r8   )$_list_tools_middleware_list_prompts_middleware_list_resources_middleware#_list_resource_templates_middlewareto_mcp_toolr   appendr
   r   r   inputSchemar   r   r>   r   listr   r   r   r   r$   r%   r&   r(   r)   r+   hasattr_mcp_serverr/   r,   r-   r.   fastmcp__version__	importlibmetadata)r9   
tools_listprompts_listresources_listtemplates_list
tool_infostoolmcp_toolprompt_infospromptresource_infosresourcetemplate_infostemplater8   server_iconsserver_website_urlr!   r!   r"   inspect_fastmcp_v2h   s   





rn   	FastMCP1xc                   s  t | 4 I dH W}| I dH }| I dH }| I dH }z	| I dH }W n ty4   g }Y nw g }|D ]-}|t|j|j|j	|j
rJ|j
ni dddddt|dr`|jr`dd |jD nddd q9g }|D ]7}	d}
t|	dr|	jrdd |	jD }
|t|	j|	j|	j	|
dddt|	dr|	jrdd |	jD nddd		 qkg }|D ]-}|tt|jt|j|j|j	|jddddt|dr|jrd
d |jD nddd qg }|D ]0}|tt|jt|j|j|j	|jdddddt|dr|jrdd |jD nddd qddidddddii d}|jj}t|dr.|jr.dd |jD nd}t|dr9|jnd}t| jj| jj| jj||tjtjdd|||||dW  d  I dH  S 1 I dH shw   Y  dS )zExtract information from a FastMCP v1.x instance using a Client.

    Args:
        mcp: The FastMCP v1.x instance to inspect

    Returns:
        FastMCPInfo dataclass containing the extracted information
    Nr   c                 S  r<   r!   r=   r?   r!   r!   r"   rB     rC   z&inspect_fastmcp_v1.<locals>.<listcomp>rD   r%   c                 S  r<   r!   r=   rE   r!   r!   r"   rB   %  rC   c                 S  r<   r!   r=   r?   r!   r!   r"   rB   0  rC   rG   c                 S  r<   r!   r=   r?   r!   r!   r"   rB   E  rC   rH   c                 S  r<   r!   r=   r?   r!   r!   r"   rB   [  rC   rI   rJ   TFrK   rM   c                 S  r<   r!   r=   r?   r!   r!   r"   rB   m  rC   
websiteUrlr9      rP   ) r	   
list_toolslist_promptslist_resourceslist_resource_templates	ExceptionrV   r
   r   r   rW   rY   r   r%   r$   r&   r   r'   mimeTyper)   uriTemplateinitialize_result
serverInforp   r,   rZ   r-   r.   r[   r\   r]   r^   )r9   client	mcp_toolsmcp_promptsmcp_resourcesmcp_templatesrc   re   rf   
mcp_promptr%   rh   mcp_resourcerj   mcp_templater8   server_inforl   rm   r!   r!   r"   inspect_fastmcp_v1   s   

2r   FastMCP[Any] | FastMCP1xc                   s2   t | trt| I dH S tttt | I dH S )a`  Extract information from a FastMCP instance into a dataclass.

    This function automatically detects whether the instance is FastMCP v1.x or v2.x
    and uses the appropriate extraction method.

    Args:
        mcp: The FastMCP instance to inspect (v1.x or v2.x)

    Returns:
        FastMCPInfo dataclass containing the extracted information
    N)
isinstancero   r   rn   r   r   r   )r9   r!   r!   r"   inspect_fastmcp  s   
r   c                   @  s   e Zd ZdZdZdZdS )InspectFormatz"Output format for inspect command.r[   r9   N)r   r   r   r   FASTMCPMCPr!   r!   r!   r"   r     s    r   infobytesc                 C  sP   | j | j| j| j| j| j| jd| j| jd| j	| j
| j| jd}tj|ddS )zFormat FastMCPInfo as FastMCP-specific JSON.

    This includes FastMCP-specific fields like tags, enabled, annotations, etc.
    )r   r-   r.   r/   r   
generationr8   r[   r9   )serverenvironmentr4   r5   r6   r7   rO   indent)r   r-   r.   r/   r   r3   r8   r0   r1   r4   r5   r6   r7   pydantic_coreto_json)r   resultr!   r!   r"   format_fastmcp_info  s"   
r   c              	     s   t | 4 I dH H}| I dH }| I dH }| I dH }| I dH }|jj}tjt	j
dd|i |j|j|j|jd}tj|ddW  d  I dH  S 1 I dH sWw   Y  dS )zFormat server info as standard MCP protocol JSON.

    Uses Client to get the standard MCP protocol format with camelCase fields.
    Includes version metadata at the top level.
    Nr9   r   )r   rz   r8   r4   r5   r6   resourceTemplatesrO   r   )r	   list_tools_mcplist_prompts_mcplist_resources_mcplist_resource_templates_mcpry   rz   r[   r\   r]   r^   r.   r4   r5   r6   r   r   r   )r9   r{   tools_resultprompts_resultresources_resulttemplates_resultr   r   r!   r!   r"   format_mcp_info  s&   
0r   format)InspectFormat | Literal['fastmcp', 'mcp']FastMCPInfo | Nonec                   sb   t |tr
t|}|tjkrt| I dH S |tjkr*|du r&t| I dH }t|S td| )a0  Format server information according to the specified format.

    Args:
        mcp: The FastMCP instance
        format: Output format ("fastmcp" or "mcp")
        info: Pre-extracted FastMCPInfo (optional, will be extracted if not provided)

    Returns:
        JSON bytes in the requested format
    NzUnknown format: )	r   r   r   r   r   r   r   r   
ValueError)r9   r   r   r!   r!   r"   format_info  s   


r   )r9   r:   r;   r,   )r9   ro   r;   r,   )r9   r   r;   r,   )r   r,   r;   r   )r9   r   r;   r   )N)r9   r   r   r   r   r   r;   r   )!r   
__future__r   importlib.metadatar]   dataclassesr   enumr   typingr   r   r   r   mcp.server.fastmcpr   ro   r[   r	   fastmcp.server.serverr
   r$   r&   r)   r,   rn   r   r   r   r   r   r   r   r!   r!   r!   r"   <module>   s>    
 
 


$