
    [ǻi1                         d Z ddlmZ ddlmZmZmZmZmZm	Z	 dedz  de
fdZdedz  dee   dz  d	e	dz  ddfd
Zdee   ddfdZy)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                 R    | y| j                   y| j                   j                  yy)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    S/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/mcp/server/validation.pycheck_sampling_tools_capabilityr      s4     #!!)    r   tool_choicec                 X    ||&t        |       st        t        t        d            yy)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                    | sy| d   j                   }t        d |D              }t        |       dk\  r| d   j                   nd}|xr t        d |D              }|r7t        d |D              rt        d      |t        d	      |st        d
      |rf|rc|D ch c]  }|j                  dk(  s|j
                    }}|D ch c]  }|j                  dk(  s|j                    }}||k7  rt        d      yyyc c}w c c}w )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   :   K   | ]  }|j                   d k(    ywtool_resultNtype.0cs     r   	<genexpr>z4validate_tool_use_result_messages.<locals>.<genexpr>U   s     Iq166]2I      c              3   :   K   | ]  }|j                   d k(    yw)tool_useNr   r   s     r   r"   z4validate_tool_use_result_messages.<locals>.<genexpr>X   s     4daQVVz5I4dr#   c              3   :   K   | ]  }|j                   d k7    ywr   r   r   s     r   r"   z4validate_tool_use_result_messages.<locals>.<genexpr>]   s     =1qvv&=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_messagesr5   @   s   " B<//LILII7:8}7Ix|33t,d4dSc4d1d ===ghh#Z[[$cdd!1&6O!&&J:NOO0<X1-@W1;;XX?*opp + "2OXs   C<0C<DD)__doc__mcp.shared.exceptionsr   	mcp.typesr   r   r   r   r   r	   boolr   listr   r5    r   r   <module>r<      s    + 1Cd1J t &#d*: d" 
	4(q_0E (q$ (qr   