o
    i<                     @   s  d Z ddlZddlZddlmZ ddlmZmZmZ e	e
Z	d!deded	ed
eeB fddZdeded
eeef fddZdedB d
efddZd"deded
efddZ		d#dedee dB dedB d
efddZ		d#dedeeef dee dB dedB d
ef
ddZg d ZdS )$z6Parameter formatting functions for OpenAPI operations.    N)Any   )
JsonSchemaParameterInfoRequestBodyInfoFvaluesparameter_nameis_query_parameterreturnc                 C   s"  t dd | D rddd | D S z7g }| D ],}t|tr=g }| D ]\}}|| d|  q%|d| q|t| qd|W S  ty } z:|rVdnd}	t	d	|	 d
| d|  |rp| W  Y d}~S t| 
dd
dd
dd
dd}
|
W  Y d}~S d}~ww )aj  
    Format an array parameter according to OpenAPI specifications.

    Args:
        values: List of values to format
        parameter_name: Name of the parameter (for error messages)
        is_query_parameter: If True, can return list for explode=True behavior

    Returns:
        String (comma-separated) or list (for query params with explode=True)
    c                 s   s&    | ]}t |ttB tB tB V  qd S N)
isinstancestrintfloatbool).0item r   w/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/experimental/utilities/openapi/formatters.py	<genexpr>   s   $ z)format_array_parameter.<locals>.<genexpr>,c                 s   s    | ]}t |V  qd S r   )r   )r   vr   r   r   r      s    :.querypathzFailed to format complex array z parameter 'z': N[ ]'")alljoinr   dictitemsappendr   	Exceptionloggerwarningreplace)r   r   r	   formatted_partsr   
item_partskr   e
param_type	str_valuer   r   r   format_array_parameter   s8   
r0   param_valuec                 C   s^   t | tstd| dt|   i S i }|  D ]\}}| d| d}t|||< q|S )a  
    Format a dictionary parameter for deepObject style serialization.

    According to OpenAPI 3.0 spec, deepObject style with explode=true serializes
    object properties as separate query parameters with bracket notation.

    For example: {"id": "123", "type": "user"} becomes:
    param[id]=123&param[type]=user

    Args:
        param_value: Dictionary value to format
        parameter_name: Name of the parameter

    Returns:
        Dictionary with bracketed parameter names as keys
    zdeepObject style parameter 'z' expected dict, got r   r   )r   r#   r'   r(   typer$   r   )r1   r   resultkeyvaluebracketed_keyr   r   r   format_deep_object_parameterB   s   
r7   schemac                 C   s  | rt | ts	dS d| v r| d S d| v r&t | d tr&| d r&| d d S d| v r;t | d tr;| d r;| d d S d| v rC| d S | d}|dkri }| d	i }t |trt| d
g }t| dd }|D ]}||v r{t|| ||< qm|D ]}||vr||v rt|| ||< q~|r|S ddiS |dkr| d}t |trt|}	|	dur|	gS g S dgS |dkr| d}
|
dkrdS |
dkrdS |
dkrdS |
dkrdS |
dkrdS dS |dkrdS |d krd!S |d"krd#S |d$krdS d%S )&zy
    Generate a simple example value from a JSON schema dictionary.
    Very basic implementation focusing on types.
    unknowndefaultenumr   examplesexampler2   object
propertiesrequiredN   r4   r5   arrayr$   example_itemstringformatz	date-timez2024-01-01T12:00:00Zdatez
2024-01-01emailzuser@example.comuuidz$123e4567-e89b-12d3-a456-426614174000bytezZXhhbXBsZQ==integerr   numberg      ?booleanTnullunknown_type)r   r#   listgetsetkeysgenerate_example_from_schema)r8   schema_typer3   r?   required_propsprops_to_include	prop_namereq_propitems_schemaitem_exampleformat_typer   r   r   rS   d   s   





rS      dataindentc                 C   s<   zt j| |d}d| dW S  ty   d|  d Y S w )z8Formats Python data as a JSON string block for markdown.r^   z```json
z
```z!```
Could not serialize to JSON: )jsondumps	TypeError)r]   r^   json_strr   r   r   format_json_for_description   s   rd   base_description
parametersrequest_bodyc                 C   sT   | g}|r%dd |D }|r%| d |D ]}| d|j d|j  qd|S )a  
    Formats a simple description for MCP objects (tools, resources, prompts).
    Excludes response details, examples, and verbose status codes.

    Args:
        base_description (str): The initial description to be formatted.
        parameters (list[ParameterInfo] | None, optional): A list of parameter information.
        request_body (RequestBodyInfo | None, optional): Information about the request body.

    Returns:
        str: The formatted description string with minimal details.
    c                 S   s    g | ]}|j d kr|jr|qS r   )locationdescriptionr   pr   r   r   
<listcomp>   s     z-format_simple_description.<locals>.<listcomp>

**Path Parameters:**
- ****: 
)r%   namerj   r"   )re   rf   rg   
desc_partspath_paramsparamr   r   r   format_simple_description   s   

rv   	responsesc              
      s  | g}|redd |D }|r5d}| | |D ]}|jrdnd}d|j d| d|jp,d	 }	| |	 qd
d |D }
|
red}| | |
D ]}|jrNdnd}d|j d| d|jp\d	 }	| |	 qG|r|jrd}| | |jrvdnd}| d|j |  |jrd|jv rdntt|jd}|r|j|i }t|t	rd|v r| d |d 
 D ]+\}}t|t	rd|v r||dg v }|rdnd}| d| d| d|d   q rd}d}h d}t fdd|D d} 
 }t|D ]\}}|s| | d}||krdnd}| d| d| d|jp"d	  |jrd|jv r2dntt|jd}|r|j|}| d| d t|t	r|ddkrd|v r|d }t|t	rd|v r| d  |d 
 D ]\}}t|t	rd|v r| d!| d"|d   qyn.d|v r| d# |d 
 D ]\}}t|t	rd|v r| d!| d"|d   q|rt|}|d$kr|dur| d% | t|d&d' qd|S )(aX  
    Formats the base description string with response, parameter, and request body information.

    Args:
        base_description (str): The initial description to be formatted.
        responses (dict[str, Any]): A dictionary of response information, keyed by status code.
        parameters (list[ParameterInfo] | None, optional): A list of parameter information,
            including path and query parameters. Each parameter includes details such as name,
            location, whether it is required, and a description.
        request_body (RequestBodyInfo | None, optional): Information about the request body,
            including its description, whether it is required, and its content schema.

    Returns:
        str: The formatted description string with additional details about responses, parameters,
        and the request body.
    c                 S      g | ]	}|j d kr|qS rh   ri   rk   r   r   r   rm          z5format_description_with_responses.<locals>.<listcomp>rn   z (Required)r   ro   z**z: zNo description.c                 S   rx   )r   ry   rk   r   r   r   rm     rz   z

**Query Parameters:**z

**Request Body:**rq   zapplication/jsonNr?   z

**Request Properties:**rj   r@   z

**Responses:**F>   200201202204c                 3   s    | ]	}| v r|V  qd S r   r   )r   srw   r   r   r   5  s    z4format_description_with_responses.<locals>.<genexpr>Tz
 (Success)z  - Content-Type: ``r2   rB   r$   z"
  - **Response Item Properties:**z	
    - **rp   z
  - **Response Properties:**rN   z
  - **Example:**r\   r_   )r%   r@   rr   rj   content_schemanextiterrP   r   r#   r$   sortedrS   rd   r"   )re   rw   rf   rg   rs   rt   param_sectionru   required_marker
param_descquery_paramsreq_body_section
media_typer8   rW   prop_schemar@   req_markresponse_sectionadded_response_sectionsuccess_codessuccess_statusresponses_to_processstatus_code	resp_infostatus_markerrY   r=   r   r   r   !format_description_with_responses   s   
















r   )r0   r7   r   rd   rv   rS   )F)r\   )NN)__doc__r`   loggingtypingr   modelsr   r   r   	getLogger__name__r'   rO   r   r   r0   r#   r7   rS   r   rd   rv   r   __all__r   r   r   r   <module>   sj    

6

"S

&

 