o
    i                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlm	Z
 d dlmZ d dlmZ d dlm	Z	 d dlmZ er<d d	lmZ g d
ZdddZdddZddddZdddZdS )     )annotationsN)TYPE_CHECKING)get_access_token)AccessToken)request_ctx)Request)_current_http_request)Context)r   r   get_contextget_http_headersget_http_requestreturnr	   c                  C  s(   ddl m}  |  }|d u rtd|S )Nr   )_current_contextzNo active context found.)fastmcp.server.contextr   getRuntimeError)r   context r   a/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/server/dependencies.pyr
   !   s
   r
   r   c                  C  s\   d } t t t j} W d    n1 sw   Y  | d u r$t } | d u r,td| S )NzNo active HTTP request found.)
contextlibsuppressLookupErrorr   r   requestr   r   )r   r   r   r   r   -   s   r   Finclude_allbooldict[str, str]c                 C  s   | rt  }nh d}tdd |D stdi }zt }|j D ]\}}| }||vr4t|||< q"|W S  tyB   i  Y S w )al  
    Extract headers from the current HTTP request if available.

    Never raises an exception, even if there is no active HTTP request (in which case
    an empty dict is returned).

    By default, strips problematic headers like `content-length` that cause issues if forwarded to downstream clients.
    If `include_all` is True, all headers are returned.
    >   
keep-alivecontent-lengthmcp-session-idproxy-connectiontransfer-encodingproxy-authenticateproxy-authorizationtehostacceptexpectupgrade
connectionc                 s  s    | ]	}|  |kV  qd S )N)lower).0hr   r   r   	<genexpr>\   s    z#get_http_headers.<locals>.<genexpr>z"Excluded headers must be lowercase)	setall
ValueErrorr   headersitemsr)   strr   )r   exclude_headersr0   r   namevalue
lower_namer   r   r   r   =   s"   
r   AccessToken | Nonec               
   C  s   t  } | du st| tr| S z|  }t|d |d |d |d|d|ddW S  tyD } ztd	t| j d
|d}~ww )z
    Get the FastMCP access token from the current context.

    Returns:
        The access token if an authenticated user is available, None otherwise.
    Ntoken	client_idscopes
expires_atresource_ownerclaims)r8   r9   r:   r;   r<   r=   z3Expected fastmcp.server.auth.auth.AccessToken, got z7. Ensure the SDK is using the correct AccessToken type.)	_sdk_get_access_token
isinstancer   
model_dumpr   	Exception	TypeErrortype__name__)access_tokenaccess_token_as_dicter   r   r   r   n   s*   	r   )r   r	   )r   r   )F)r   r   r   r   )r   r7   )
__future__r   r   typingr   'mcp.server.auth.middleware.auth_contextr   r>   mcp.server.auth.providerr   _SDKAccessTokenmcp.server.lowlevel.serverr   starlette.requestsr   fastmcp.server.authfastmcp.server.httpr   r   r	   __all__r
   r   r   r   r   r   r   <module>   s     

1