
    Vi1                         S r SSKJr  SSKJrJrJrJrJrJ	r	  S\S-  S\
4S jrS\S-  S\\   S-  S	\	S-  SS4S
 jrS\\   SS4S jrg)z
Shared validation functions for server requests.

This module provides validation logic for sampling and elicitation requests
that is shared across normal and task-augmented code paths.
    )McpError)INVALID_PARAMSClientCapabilities	ErrorDataSamplingMessageTool
ToolChoiceclient_capsNreturnc                 X    U c  gU R                   c  gU R                   R                  c  gg)z
Check if the client supports sampling tools capability.

Args:
    client_caps: The client's declared capabilities

Returns:
    True if client supports sampling.tools, False otherwise
FT)samplingtools)r
   s    h/home/james-whalen/.local/share/pipx/venvs/semgrep/lib/python3.13/site-packages/mcp/server/validation.pycheck_sampling_tools_capabilityr      s4     #!!)    r   tool_choicec                 `    Uc  Ub(  [        U 5      (       d  [        [        [        SS95      egg)a<  
Validate that the client supports sampling tools if tools are being used.

Args:
    client_caps: The client's declared capabilities
    tools: The tools list, if provided
    tool_choice: The tool choice setting, if provided

Raises:
    McpError: If tools/tool_choice are provided but client doesn't support them
Nz1Client does not support sampling tools capability)codemessage)r   r   r   r   )r
   r   r   s      r   validate_sampling_toolsr   &   s?      K3.{;;'O  < 4r   messagesc                 f   U (       d  gU S   R                   n[        S U 5       5      n[        U 5      S:  a  U S   R                   OSnU=(       a    [        S U 5       5      nU(       aB  [        S U 5       5      (       a  [        S5      eUc  [        S	5      eU(       d  [        S
5      eU(       as  U(       ak  U Vs1 s H   oUR                  S:X  d  M  UR
                  iM"     nnU Vs1 s H   oUR                  S:X  d  M  UR                  iM"     nnXg:w  a  [        S5      egggs  snf s  snf )a  
Validate tool_use/tool_result message structure per SEP-1577.

This validation ensures:
1. Messages with tool_result content contain ONLY tool_result content
2. tool_result messages are preceded by a message with tool_use
3. tool_result IDs match the tool_use IDs from the previous message

See: https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1577

Args:
    messages: The list of sampling messages to validate

Raises:
    ValueError: If the message structure is invalid
Nc              3   >   #    U  H  oR                   S :H  v   M     g7ftool_resultNtype.0cs     r   	<genexpr>4validate_tool_use_result_messages.<locals>.<genexpr>U   s     ILq66]2L      c              3   >   #    U  H  oR                   S :H  v   M     g7f)tool_useNr   r   s     r   r"   r#   X   s     4dScaVVz5IScr$   c              3   >   #    U  H  oR                   S :g  v   M     g7fr   r   r   s     r   r"   r#   ]   s     =1vv&r$   zHThe last message must contain only tool_result content if any is presentz;tool_result requires a previous message containing tool_usezDtool_result blocks do not match any tool_use in the previous messager(   r   zPids of tool_result blocks and tool_use blocks from previous message do not match)content_as_listanylen
ValueErrorr   id	toolUseId)r   last_contenthas_tool_resultsprevious_contenthas_previous_tool_user!   tool_use_idstool_result_idss           r   !validate_tool_use_result_messagesr6   @   s   " B<//LILII7:8}7Ix|33t,d4dSc4d1d ====ghh#Z[[$cdd!1&6O&6&&J:N&6O0<X1-@W;1;;X*opp + "2OXs    D)D)-D.D.)__doc__mcp.shared.exceptionsr   	mcp.typesr   r   r   r   r   r	   boolr   listr   r6    r   r   <module>r=      s    + 1Cd1J t &#d*: d" 
	4(q_0E (q$ (qr   