xjQdZddlmZmZmZmZmZddlmZdedzde fdZ de edzd edzde fd Z dedzde edzd edzddfd Z d e eddfd Zy)zShared validation functions for server requests. This module provides validation logic for sampling and elicitation requests. )INVALID_PARAMSClientCapabilitiesSamplingMessageTool ToolChoice)MCPError client_capsNreturncR|y|jy|jjyy)zCheck 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)samplingtools)r s i/mnt/ssd/data/Dropbox/adrian/sandbox/mcp-query/venv/lib/python3.12/site-packages/mcp/server/validation.pycheck_sampling_tools_capabilityr s4#!!) r tool_choicec|duxs|duS)zWWhether a sampling request is tools-mode: `sampling.tools` gated, array-capable answer.N)r rs rwants_sampling_toolsrs   7 4 77rcVt||rt|sttdyy)aWValidate 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 z1Client does not support sampling tools capability)codemessageN)rrrr)r r rs rvalidate_sampling_toolsr"s.E;/.{;8kl l<0rmessagesc |sy|dj}td|D}t|dk\r|djnd}|xrtd|D}|r7td|Dr td| td |s td |rf|rc|Dchc]}|jd k(s|j  }}|Dchc]}|jd k(s|j  }}||k7r td yyycc}wcc}w)aValidate 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 Nc3:K|]}|jdk(yw tool_resultNtype.0cs r z4validate_tool_use_result_messages..JsIq166]2Ic3:K|]}|jdk(yw)tool_useNrr!s rr$z4validate_tool_use_result_messages..Ms4daQVVz5I4dr%c3:K|]}|jdk7ywrrr!s rr$z4validate_tool_use_result_messages..Rs=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)rzPids of tool_result blocks and tool_use blocks from previous message do not match)content_as_listanylen ValueErrorr id tool_use_id)r last_contenthas_tool_resultsprevious_contenthas_previous_tool_user# tool_use_idstool_result_idss r!validate_tool_use_result_messagesr76s B<//LILII7:8}7Ix|33t,d4dSc4d1d = = =gh h  #Z[ [$cd d!1&6O!&&J:NO O2>ZQ!&&MBY1==ZZ ? *op p +"2OZsC<0C<DD)__doc__ mcp_typesrrrrrmcp.shared.exceptionsrboolrlistrrr7rrrr=s \[*1Cd1Jt$8T T 18 T@Q8VZ8 m#d*m : md"m m('q_0E'q$'qr