
    j
i                    J   d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZmZ 	 ddlZd	Zn# e$ r d
ZY nw xY werddlmZmZ erddlmZ eez  ez  Zneez  Z G d ded
          Ze G d d                      Ze
 G d de	                      ZdS )a[  Interceptor interfaces and types for MCP client tool call lifecycle management.

This module provides an interceptor interface for wrapping and controlling
MCP tool call execution with a handler callback pattern.

In the future, we might add more interceptors for other parts of the
request / result lifecycle, for example to support elicitation.
    )annotations)	dataclassreplace)TYPE_CHECKINGAnyProtocolruntime_checkable)ToolMessage)CallToolResult)NotRequired	TypedDictUnpackNTF)	AwaitableCallable)Commandc                  2    e Zd ZU dZded<   ded<   ded<   dS )	_MCPToolCallRequestOverrideszPossible overrides for MCPToolCallRequest.override() method.

    Only includes modifiable request fields, not context fields like
    server_name and runtime which are read-only.
    zNotRequired[str]namezNotRequired[dict[str, Any]]argsz"NotRequired[dict[str, Any] | None]headersN)__name__
__module____qualname____doc____annotations__     C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langchain_mcp_adapters/interceptors.pyr   r   '   sB           %%%%//////r   r   )totalc                  V    e Zd ZU dZded<   ded<   ded<   dZded	<   dZd
ed<   ddZdS )MCPToolCallRequesta  Tool execution request passed to MCP tool call interceptors.

    This tool call request follows a similar pattern to LangChain's
    ToolCallRequest (flat namespace) rather than separating the call data
    and context into nested objects.

    Modifiable fields (override these to change behavior):
        name: Tool name to invoke.
        args: Tool arguments as key-value pairs.
        headers: HTTP headers for applicable transports (SSE, HTTP).

    Context fields (read-only, use for routing/logging):
        server_name: Name of the MCP server handling the tool.
        runtime: LangGraph runtime context (optional, None if outside graph).
    strr   zdict[str, Any]r   server_nameNzdict[str, Any] | Noner   zobject | Noneruntime	overrides$Unpack[_MCPToolCallRequestOverrides]returnc                    t          | fi |S )a  Replace the request with a new request with the given overrides.

        Returns a new `MCPToolCallRequest` instance with the specified
        attributes replaced. This follows an immutable pattern, leaving the
        original request unchanged.

        Args:
            **overrides: Keyword arguments for attributes to override.
                Supported keys:
                - name: Tool name
                - args: Tool arguments
                - headers: HTTP headers

        Returns:
            New MCPToolCallRequest instance with specified overrides
            applied.

        Note:
            Context fields (server_name, runtime) cannot be overridden as
            they are read-only.

        Examples:
            ```python
            # Modify tool arguments
            new_request = request.override(args={"value": 10})

            # Change tool name
            new_request = request.override(name="different_tool")
            ```
        )r   )selfr%   s     r   overridezMCPToolCallRequest.overrideK   s    B t))y)))r   )r%   r&   r'   r!   )r   r   r   r   r   r   r$   r*   r   r   r   r!   r!   3   sy            III%)G))))!G!!!!!* !* !* !* !* !*r   r!   c                      e Zd ZdZd
dZd	S )ToolCallInterceptora  Protocol for tool call interceptors using handler callback pattern.

    Interceptors wrap tool execution to enable request/response modification,
    retry logic, caching, rate limiting, and other cross-cutting concerns.
    Multiple interceptors compose in "onion" pattern (first is outermost).

    The handler can be called multiple times (retry), skipped (caching/short-circuit),
    or wrapped with error handling. Each handler call is independent.

    Similar to LangChain's middleware pattern but adapted for MCP remote tools.
    requestr!   handler<Callable[[MCPToolCallRequest], Awaitable[MCPToolCallResult]]r'   MCPToolCallResultc                
   K   dS )a  Intercept tool execution with control over handler invocation.

        Args:
            request: Tool call request containing name, args, headers, and context
                (server_name, runtime). Access context fields like request.server_name.
            handler: Async callable executing the tool. Can be called multiple
                times, skipped, or wrapped for error handling.

        Returns:
            Final MCPToolCallResult from tool execution or interceptor logic.
        Nr   )r)   r-   r.   s      r   __call__zToolCallInterceptor.__call__}   s        	r   N)r-   r!   r.   r/   r'   r0   )r   r   r   r   r2   r   r   r   r,   r,   o   s2        
 
     r   r,   )r   
__future__r   dataclassesr   r   typingr   r   r   r	   langchain_core.messagesr
   	mcp.typesr   typing_extensionsr   r   r   	langgraphLANGGRAPH_PRESENTImportErrorcollections.abcr   r   langgraph.typesr   r0   r   r!   r,   r   r   r   <module>r>      s    # " " " " " * * * * * * * * B B B B B B B B B B B B / / / / / / $ $ $ $ $ $ < < < < < < < < < <     433333333 5''''''&4w>&4	0 	0 	0 	0 	09E 	0 	0 	0 	0 8* 8* 8* 8* 8* 8* 8* 8*v     (     s   ; AA