Xj1dZddlmZddlmZmZmZmZmZm Z dedzde fdZ dedzde edzd e dzddfd Z d e eddfd Zy) 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_PARAMSClientCapabilities ErrorDataSamplingMessageTool ToolChoice client_capsNreturncR|y|jy|jjyy)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)samplingtools)r s i/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/mcp/server/validation.pycheck_sampling_tools_capabilityrs4#!!) r tool_choicecX||&t|stttdyy)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)codemessage)rrrr)r rrs rvalidate_sampling_toolsr&s<  K3.{;'O <4rmessagesc |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)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 Nc3:K|]}|jdk(yw tool_resultNtype.0cs r z4validate_tool_use_result_messages..UsIq166]2Ic3:K|]}|jdk(yw)tool_useNrrs rr"z4validate_tool_use_result_messages..Xs4daQVVz5I4dr#c3:K|]}|jdk7ywrrrs rr"z4validate_tool_use_result_messages..]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'rzPids of tool_result blocks and tool_use blocks from previous message do not match)content_as_listanylen ValueErrorrid toolUseId)r last_contenthas_tool_resultsprevious_contenthas_previous_tool_user! tool_use_idstool_result_idss r!validate_tool_use_result_messagesr5@s" B<//LILII7:8}7Ix|33t,d4dSc4d1d = = =gh h  #Z[ [$cd d!1&6O!&&J:NO O0<X1-@W1;;XX ? *op p +"2OXsC<0C<DD)__doc__mcp.shared.exceptionsr mcp.typesrrrrrr boolrlistrr5rrr<s+1Cd1Jt&#d* : d" 4(q_0E(q$(qr