o
    c۷i1                     @   s   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B de
fddZdedB d	ee dB d
e	dB ddfddZdee ddfd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                 C   s.   | du rdS | j du rdS | j jdu r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	    r   K/home/ubuntu/vllm_env/lib/python3.10/site-packages/mcp/server/validation.pycheck_sampling_tools_capability   s   

r   r   tool_choicec                 C   s0   |dus|durt | stttd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   r   r   r   validate_sampling_tools&   s   r   messagesc                 C   s   | sdS | d j }tdd |D }t| dkr| d j nd}|o)tdd |D }|rGtdd |D r9td	|du rAtd
|sGtd|ra|rcdd |D }dd |D }||kret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                 s       | ]}|j d kV  qdS tool_resultNtype.0cr   r   r   	<genexpr>U       z4validate_tool_use_result_messages.<locals>.<genexpr>   c                 s   r   )tool_useNr   r   r   r   r   r   X   r   c                 s   s    | ]}|j d kV  qdS r   r   r   r   r   r   r   ]   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                 S      h | ]
}|j d kr|jqS )r"   )r   idr   r   r   r   	<setcomp>e       z4validate_tool_use_result_messages.<locals>.<setcomp>c                 S   r#   )r   )r   	toolUseIdr   r   r   r   r%   f   r&   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_idsr   r   r   !validate_tool_use_result_messages@   s(   
r2   )__doc__mcp.shared.exceptionsr   	mcp.typesr   r   r   r   r   r   boolr   listr   r2   r   r   r   r   <module>   s     


