o
    i;4                     @   s   d Z ddlZddlZddlmZ ddlmZmZ ddlZ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mZ dd
lmZ ddlmZmZ ddlmZ erXddlmZ eeZG dd deZG dd deZ G dd deZ!g dZ"dS )zPOpenAPI component implementations: Tool, Resource, and ResourceTemplate classes.    N)Callable)TYPE_CHECKINGAny)ToolAnnotations)AnyUrl)	HTTPRoute)RequestDirector)ResourceResourceTemplate)get_http_headers)Tool
ToolResult)
get_logger)Contextc                       s   e Zd ZdZ					ddejdedededede	ee
f d	e	ee
f dB d
ee dB dedB dedB dee
gef dB f fddZdefddZde	ee
f defddZ  ZS )OpenAPIToolz*Tool implementation for OpenAPI endpoints.Nclientroutedirectornamedescription
parametersoutput_schematagstimeoutannotations
serializerc              	      s<   t  j|||||pt |
|d || _|| _|| _|	| _d S )N)r   r   r   r   r   r   r   )super__init__set_client_route	_director_timeout)selfr   r   r   r   r   r   r   r   r   r   r   	__class__ t/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/experimental/server/openapi/components.pyr      s   	
zOpenAPITool.__init__returnc                 C   s"   d| j d| jj d| jj dS )@Custom representation to prevent recursion errors when printing.zOpenAPITool(name=z	, method=, path=))r   r    methodpathr#   r&   r&   r'   __repr__9   s   "zOpenAPITool.__repr__	argumentsc                    sF  zt | jdr| jjrt| jjnd}t | jdr"| jjr"| jjni }| j| j||}|rT|jrF| D ]\}}||jvrD||j|< q6n| D ]	\}}||j|< qJt	 }|rq|jrc|j
| n| D ]	\}}||j|< qgtd|j  | j|I dH }|  z,| }	d}
| jdur| jdrd|	i}
n|	}
nt|	tsd|	i}
n|	}
t|
dW W S  tjy   t|jd	 Y W S w  tjy } z;d
|jj d|jj }z|j }|d| 7 }W n tjtfy   |jjr|d|jj 7 }Y nw t||d}~w tjy" } ztd||d}~ww )zQExecute the HTTP request using RequestDirector for simplified parameter handling.base_urlzhttp://localhostheadersz run - sending request; headers: Nzx-fastmcp-wrap-resultresult)structured_content)contentHTTP error :  - Request error: )hasattrr   r1   strr2   r!   buildr    itemsr   updateloggerdebugsendraise_for_statusjsonr   get
isinstancedictr   JSONDecodeErrortexthttpxHTTPStatusErrorresponsestatus_codereason_phrase
ValueErrorRequestError)r#   r0   r1   cli_headersrequestkeyvaluemcp_headersrK   r3   structured_outputeerror_message
error_datar&   r&   r'   run=   s   

	









zOpenAPITool.run)NNNNN)__name__
__module____qualname____doc__rI   AsyncClientr   r   r;   rF   r   r   floatr   r   r   r/   r   rY   __classcell__r&   r&   r$   r'   r      s@    


	
"r   c                       s|   e Zd ZdZ			ddejdedededed	ed
ede	e dB de
dB f fddZdefddZdeeB fddZ  ZS )OpenAPIResourcez.Resource implementation for OpenAPI endpoints.application/jsonNr   r   r   urir   r   	mime_typer   r   c
           
         sD   |d u rt  }t jt|||||d || _|| _|| _|	| _d S )N)rc   r   r   rd   r   )r   r   r   r   r   r    r!   r"   )
r#   r   r   r   rc   r   r   rd   r   r   r$   r&   r'   r      s   
zOpenAPIResource.__init__r(   c                 C       d| j d| jd| jj dS )r)   zOpenAPIResource(name=z, uri=r*   r+   )r   rc   r    r-   r.   r&   r&   r'   r/          zOpenAPIResource.__repr__c                    s  z| j j}t| j}d|v r`d|v r`|d}t|dkr`i }td|}|rK|jdd t|d }t	|D ]\}}||k rJ|d|  }	|	||< q8|
 D ]\}}	|d| dt|	}qOi }
| j jD ]&}|jd	krt| d
|j rt| d
|j }|dur|dkr||
|j< qfi }t| jdr| jjr| jjni }|| t }|| | jj| j j||
|| jdI dH }|  |jdd  d v r| }t|W S t fdddD r|jW S |jW S  t j!y3 } z;d|j"j# d|j"j$ }z|j" }|d| 7 }W n tj%t&fy)   |j"jr'|d|j"j 7 }Y nw t&||d}~w t j'yH } zt&d||d}~ww )z2Fetch the resource data by making an HTTP request.{}/   z\{([^}]+)\}T)reversequery_N r2   )r,   urlparamsr2   r   zcontent-typerb   c                 3   s    | ]}| v V  qd S Nr&   ).0ctcontent_typer&   r'   	<genexpr>  s    z'OpenAPIResource.read.<locals>.<genexpr>)ztext/zapplication/xmlr6   r7   r8   r9   )(r    r-   r;   rc   splitlenrefindallsort	enumerater=   replacer   locationr:   r   getattrr   r2   r>   r   rQ   r,   r"   rB   rD   lowerrC   dumpsanyrH   r5   rI   rJ   rK   rL   rM   rG   rN   rO   )r#   r-   resource_uripartspath_paramsparam_matchesexpected_param_counti
param_nameparam_valuequery_paramsparamrS   r2   rP   rT   rK   r3   rV   rW   rX   r&   ru   r'   read   s   






	


zOpenAPIResource.read)rb   NN)rZ   r[   r\   r]   rI   r^   r   r   r;   r   r_   r   r/   bytesr   r`   r&   r&   r$   r'   ra      s4    

	
ra   c                       s   e Zd ZdZ		ddejdededededed	e	ee
f d
ee dB dedB f fddZdefddZ	ddede	ee
f dddefddZ  ZS )OpenAPIResourceTemplatez7Resource template implementation for OpenAPI endpoints.Nr   r   r   uri_templater   r   r   r   r   c
           
         s@   |d u rt  }t j|||||d || _|| _|| _|	| _d S )N)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   #  s   
z OpenAPIResourceTemplate.__init__r(   c                 C   re   )r)   zOpenAPIResourceTemplate(name=z, uri_template=r*   r+   )r   r   r    r-   r.   r&   r&   r'   r/   =  rf   z OpenAPIResourceTemplate.__repr__rc   rq   contextzContext | Nonec                    s|   g }|  D ]\}}|| d|  qt| j| j| j|| j dd| | jp1d| jj	 dt
| jjp8g | jd	S )z,Create a resource with the given parameters.=-zResource for rb   )	r   r   r   rc   r   r   rd   r   r   )r=   appendra   r   r    r!   r   joinr   r-   r   r   r"   )r#   rc   rq   r   	uri_partsrR   rS   r&   r&   r'   create_resourceA  s   z'OpenAPIResourceTemplate.create_resource)NNrr   )rZ   r[   r\   r]   rI   r^   r   r   r;   rF   r   r   r_   r   r/   r	   r   r`   r&   r&   r$   r'   r      sD    

	

r   )ra   r   r   )#r]   rC   rz   collections.abcr   typingr   r   rI   	mcp.typesr   pydantic.networksr   &fastmcp.experimental.utilities.openapir   /fastmcp.experimental.utilities.openapi.directorr   fastmcp.resourcesr	   r
   fastmcp.server.dependenciesr   fastmcp.tools.toolr   r   fastmcp.utilities.loggingr   fastmcp.serverr   rZ   r?   r   ra   r   __all__r&   r&   r&   r'   <module>   s.      <