
    )`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dS )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                 <    | dS | j         dS | j         j        dS dS )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
    NFT)samplingtools)r
   s    i/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mcp/server/validation.pycheck_sampling_tools_capabilityr      s3     u#u!)u4    r   tool_choicec                 v    ||4t          |           s#t          t          t          d                    dS dS )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    	 	 43r   messagesc                    | sdS | d         j         }t          d |D                       }t          |           dk    r| d         j         nd}|ot          d |D                       }|rJt          d |D                       rt          d          |t          d	          |st          d
          |r/|r/d |D             }d |D             }||k    rt          d          dS dS dS )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    V  dS tool_resultNtype.0cs     r   	<genexpr>z4validate_tool_use_result_messages.<locals>.<genexpr>U   s)      IIq16]2IIIIIIr      c              3   ,   K   | ]}|j         d k    V  dS )tool_useNr   r   s     r   r"   z4validate_tool_use_result_messages.<locals>.<genexpr>X   s)      4d4daQVz5I4d4d4d4d4d4dr   c              3   ,   K   | ]}|j         d k    V  dS r   r   r   s     r   r"   z4validate_tool_use_result_messages.<locals>.<genexpr>]   s)      ==1qv&======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 messagec                 2    h | ]}|j         d k    |j        S )r&   )r   idr   s     r   	<setcomp>z4validate_tool_use_result_messages.<locals>.<setcomp>e   s&    OOO!&J:N:N:N:N:Nr   c                 2    h | ]}|j         d k    |j        S )r   )r   	toolUseIdr   s     r   r*   z4validate_tool_use_result_messages.<locals>.<setcomp>f   s&    XXX1-@W@W1;@W@W@Wr   zPids of tool_result blocks and tool_use blocks from previous message do not match)content_as_listanylen
ValueError)r   last_contenthas_tool_resultsprevious_contenthas_previous_tool_usetool_use_idstool_result_idss          r   !validate_tool_use_result_messagesr7   @   sY   "  B</LIILIIIII7:8}}7I7Ix|33t,d4d4dSc4d4d4d1d1d e ======= 	ighhh#Z[[[$ 	ecddd q!1 qOO&6OOOXXXXX?**oppp	q q q q +*r   )__doc__mcp.shared.exceptionsr   	mcp.typesr   r   r   r   r   r	   boolr   listr   r7    r   r   <module>r>      s    + * * * * *               1Cd1J t    &#d*: d" 
	   4(q_0E (q$ (q (q (q (q (q (qr   