
    ~h?                        d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d dlmZmZmZ d dlZd dlmZ d dlmZ erd dlmZ e
dd	       Zdd
Ze
dd	 	 	 	 	 dd       Zy)    )annotationsN)Callable	Generator)contextmanager)TYPE_CHECKINGAnyLiteral)settings)find_available_port)FastMCPc            	   +    K   t        j                  t        j                               }	 | j	                         D ]5  \  }}t        t        |      st        d| d      t        t        ||       7 d | D ]'  }t        t        |      st        t        |||          ) y# | D ]'  }t        t        |      st        t        |||          ) w xY ww)a  
    Temporarily override ControlFlow setting values.

    Args:
        **kwargs: The settings to override, including nested settings.

    Example:
        Temporarily override a setting:
        ```python
        import fastmcp
        from fastmcp.utilities.tests import temporary_settings

        with temporary_settings(log_level='DEBUG'):
            assert fastmcp.settings.log_level == 'DEBUG'
        assert fastmcp.settings.log_level == 'INFO'
        ```
    zSetting z does not exist.N)copydeepcopyr
   
model_dumpitemshasattrAttributeErrorsetattr)kwargsold_settingsattrvalues       S/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/fastmcp/utilities/tests.pytemporary_settingsr      s     & ==!4!4!67L<!<<> 	+KD%8T*$xv5E%FGGHdE*	+ 	  	<Dx&$T(:;	<F 	<Dx&$T(:;	<s)   (CAB$ 7CC$C;CCc                    |dk(  r| j                  d      }nt        d|       t        j                  t        j                  |d|d            }|j                          y )Nsse)	transportzInvalid transport: 	127.0.0.1error)apphostport	log_level)config)http_app
ValueErroruvicornServerConfigrun)
mcp_serverr   r"   r    uvicorn_servers        r   _run_serverr-   8   sd    E!!E!2.yk:;;^^~~	
N     T)provide_host_and_portc             /  D  K   d}t               }|r|||dz  }t        j                  | ||d      }|j                          d}d}||k  ra|j	                         rQ	 t        j
                  t
        j                  t
        j                        5 }	|	j                  ||f       	 ddd       nt        d| d      d| d|  |j                          |j                  d       |j	                         r>|j                          |j                  d       |j	                         rt        d      yy# 1 sw Y   nxY wnD# t        $ r8 |dk  rt        j                  d	       nt        j                  d
       |dz  }Y nw xY w||k  s|j	                         rDw)a]  
    Context manager that runs a FastMCP server in a separate process and
    returns the server URL. When the context manager is exited, the server process is killed.

    Args:
        server_fn: The function that runs a FastMCP server. FastMCP servers are
            not pickleable, so we need a function that creates and runs one.
        *args: Arguments to pass to the server function.
        provide_host_and_port: Whether to provide the host and port to the server function as kwargs.
        **kwargs: Keyword arguments to pass to the server function.

    Returns:
        The server URL.
    r   )r!   r"   T)targetargsr   daemon
   r   N   g{Gz?g?   zServer failed to start after z	 attemptszhttp://:   )timeout   z2Server process failed to terminate even after kill)r   multiprocessingProcessstartis_alivesocketAF_INETSOCK_STREAMconnectConnectionRefusedErrortimesleepRuntimeError	terminatejoinkill)
	server_fnr/   r2   r   r!   r"   procmax_attemptsattemptss
             r   run_server_in_processrO   I   s    * D D4..""tF4D 	JJL LG
L
 T]]_		v~~v/A/AB a		4,'  :<.	RSSD64&
!!NNIIaI}}				!	==?STT 	 !   & 	{

4 

3qLG	 L
 T]]_sO   AF 2E D6#E +BF 6D?;E F >FF FF F )r   r   )r+   r   r   zLiteral['sse']r"   intreturnNone)rJ   zCallable[..., None]r/   boolrQ   zGenerator[str, None, None])
__future__r   r   r;   r?   rD   collections.abcr   r   
contextlibr   typingr   r   r	   r'   fastmcpr
   fastmcp.utilities.httpr   fastmcp.server.serverr   r   r-   rO    r.   r   <module>r\      s    "     / % . .   6-  <  <F"  #'9U"9U  9U
  9U 9Ur.   