
    Ki<                     >   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j                  e
      Z	 ddededed	eez  fd
Zdeded	eeef   fdZdedz  d	efdZddeded	efdZ	 	 ddedee   dz  dedz  d	efdZ	 	 ddedeeef   dee   dz  dedz  d	ef
dZg dZy)z6Parameter formatting functions for OpenAPI operations.    N)Any   )
JsonSchemaParameterInfoRequestBodyInfovaluesparameter_nameis_query_parameterreturnc           	         t        d | D              rdj                  d | D              S 	 g }| D ]}  }t        |t              rQg }|j	                         D ]  \  }}|j                  | d|         |j                  dj                  |             d|j                  t        |              dj                  |      S # t        $ r}|rdnd}	t        j                  d|	 d	| d
|        |r| cY d}~S t        |       j                  dd      j                  dd      j                  dd      j                  dd      }
|
cY d}~S d}~ww xY 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              3   f   K   | ])  }t        |t        t        z  t        z  t        z         + y wN)
isinstancestrintfloatbool).0items     p/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/utilities/openapi/formatters.py	<genexpr>z)format_array_parameter.<locals>.<genexpr>   s%     
I$:dC#I-45
Is   /1,c              3   2   K   | ]  }t        |        y wr   )r   )r   vs     r   r   z)format_array_parameter.<locals>.<genexpr>   s     /1A/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_values              r   format_array_parameterr3      sI    
I&
IIxx////  	2D$%
 JJL 2DAq%%1QCj12&&sxx
';<&&s4y1	2 xx((  2W
-j\nEUUXYZX[\	
 M Fb!b!b!b!  %s+   BC 	E
'E	1E7AE	E	Eparam_valuec                     t        | t              s&t        j                  d| dt	        |               i S i }| j                         D ]  \  }}| d| d}t        |      ||<    |S )a  
    Format a dictionary parameter for deep-object 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Deep-object style parameter 'z' expected dict, got r   r!   )r   r&   r*   r+   typer'   r   )r4   r	   resultkeyvaluebracketed_keys         r   format_deep_object_parameterr;   B   s    & k4(+N+;;PQUVaQbPcd	
 	F!'') +
U)*!C52 #E
}+
 M    schemac                 ^   | rt        | t              sy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 r| d   S | j                  d      }|dk(  ri }| j                  d	i       }t        |t              rvt	        | j                  d
g             }t        |j                               dd }|D ]  }||v st        ||         ||<    |D ]  }||vs||v st        ||         ||<    |r|S ddiS |dk(  r6| j                  d      }t        |t              rt        |      }	|	|	gS g S dgS |dk(  r0| j                  d      }
|
dk(  ry|
dk(  ry|
dk(  ry|
dk(  ry|
dk(  ryy|dk(  ry|d k(  ry!|d"k(  ry#|d$k(  ryy%)&zy
    Generate a simple example value from a JSON schema dictionary.
    Very basic implementation focusing on types.
    unknowndefaultenumr   examplesexampler6   object
propertiesrequiredN   r8   r9   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)r=   schema_typer7   rE   required_propsprops_to_include	prop_namereq_propitems_schemaitem_exampleformat_types              r   rY   rY   d   s=   
 FD1 Fi  Jvf~t<f~a   	fvj)40:j!!$$Fi  **V$KhZZb1
j$' J!;<N#JOO$56  . 	
*(D"9-)F9% + 6)h*.D'C"8,(F8$
  v5eW%55		zz'*lD)7EL%1%=L>E2E		 jj*+%)& '!%& 9& !			!		 			!		 r<   dataindentc                 h    	 t        j                  | |      }d| dS # t        $ r	 d|  dcY S w xY w)z8Formats Python data as a JSON string block for Markdown.rc   z```json
z
```z!```
Could not serialize to JSON: )jsondumps	TypeError)rb   rc   json_strs      r   format_json_for_descriptionrj      sG    @::d628*E** @3D6??@s    11base_description
parametersrequest_bodyc                    | g}|rq|D cg c]!  }|j                   dk(  s|j                  s |# }}|rC|j                  d       |D ]-  }|j                  d|j                   d|j                          / dj	                  |      S c c}w )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.
    r   

**Path Parameters:**
- ****: 
)locationdescriptionr(   namer%   )rk   rl   rm   
desc_partsppath_paramsparams          r   format_simple_descriptionrz      s    " ##J ",WQ

f0DqWW89$ P!!F5::,d5;L;L:M"NOP 99Z   Xs   BBB	responsesc           
      0	   | g}|r|D cg c]  }|j                   dk(  s| }}|r^d}|j                  |       |D ]F  }|j                  rdnd}	d|j                   d|	 d|j                  xs d }
|j                  |
       H |D cg c]  }|j                   d	k(  s| }}|r^d
}|j                  |       |D ]F  }|j                  rdnd}	d|j                   d|	 d|j                  xs d }
|j                  |
       H |r6|j                  r)d}|j                  |       |j                  rdnd}	|j                  d|j                   |	        |j
                  rd|j
                  v rdnt        t        |j
                        d      }|r|j
                  j                  |i       }t        |t              r}d|v ry|j                  d       |d   j                         D ]R  \  }}t        |t              sd|v s||j                  dg       v }|rdnd}|j                  d| d| d|d           T r<d}d}h d}t        fd|D        d      }j                         }t        |      D ]   \  }}|s|j                  |       d}||k(  rdnd}|j                  d| d| d|j                  xs d        |j
                  sZd|j
                  v rdnt        t        |j
                        d      }|s|j
                  j                  |      }|j                  d| d       t        |t              r|j                  d      dk(  rzd|v rv|d   }t        |t              rd|v r|j                  d       |d   j                         D ]5  \  }}t        |t              sd|v s|j                  d| d |d           7 n`d|v r\|j                  d!       |d   j                         D ]5  \  }}t        |t              sd|v s|j                  d| d |d           7 |st        |      }|d"k7  s||j                  d#       |j                  t        |d$%              dj                  |      S c c}w c c}w )&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.
    r   ro   z (Required)r    rp   z**z: zNo description.r   z

**Query Parameters:**z

**Request Body:**rr   zapplication/jsonNrE   z

**Request Properties:**rt   rF   z

**Responses:**F>   200201202204c              3   ,   K   | ]  }|v s|  y wr    )r   sr{   s     r   r   z4format_description_with_responses.<locals>.<genexpr>5  s     JQ1	>qJs   	Tz
 (Success)z  - Content-Type: ``r6   rH   r'   z"
  - **Response Item Properties:**z	
    - **rq   z
  - **Response Properties:**rT   z
  - **Example:**   re   )rs   r(   rF   ru   rt   content_schemanextiterrV   r   r&   r'   sortedrY   rj   r%   )rk   r{   rl   rm   rv   rw   rx   param_sectionry   required_marker
param_descquery_paramsreq_body_section
media_typer=   r]   prop_schemarF   req_markresponse_sectionadded_response_sectionsuccess_codessuccess_statusresponses_to_processstatus_code	resp_infostatus_markerr_   rC   s    `                           r   !format_description_with_responsesr      s   0 ##J ",EQ

f0DqEE6Mm,$ .38>>-r%ejj\O3DBuGXGXGm\mFno
!!*-. $.Gaw1FGG7Mm,% .38>>-r%ejj\O3DBuGXGXGm\mFno
!!*-. 002*++7+@+@-bB|7788IJK && &)D)DD #$|::;TB 
 %4488RHfd+0F%%&CD282F2L2L2N 	.	;&{D9 - <'0FJJz24N'NH8@}bH&--"(2hZr+mB\A] ^	 /!& 5J-JDQ  )0&,-A&B >	"K)!!"23)-&,7>,ILrMRb9N9N9cRc8de
 '' *Y-E-EE 'd9#;#;<dC  &5599*EF%%(;J<q&IJ "&$/!::f-8W=N+1'?L *< >$0L$@ * 1 12W X>J$0?""'%'	!*$:I{ )3;(E,9[,H(2(9(9.84TaHbGc,d)*	!* *V3&--.NO:@:N:T:T:V & 6	;$.{D$A(5(D$.$5$5*4YKtKP]D^C_(`%&& ">v"F"n49L&--.BC&-- ;GA Ny>	@ 99Z  y F Hs   RR
RR)r3   r;   r   rj   rz   rY   )F)r   )NN)__doc__rf   loggingtypingr   modelsr   r   r   	getLogger__name__r*   rU   r   r   r3   r&   r;   rY   r   rj   rz   r   __all__r   r<   r   <module>r      s_   <    > >			8	$ CH33"%3;?34Z3l'*	#s(^DPd): Ps Pf@c @3 @s @ .2+/!!]#d*! "D(! 		!L .2+/Y!Y!SY!
 ]#d*Y! "D(Y! 	Y!zr<   