
    "<i,                       d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZ ddlmZ ddlZddlmZmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZ erddlmZmZm Z m!Z! ddl"m#Z# d(dZ$ G d de          Z% G d de          Z& G d de%e&          Z' G d de          Z( G d de%e(          Z)e'e)z  Z*e&e(z  Z+e*e+z  Z, G d de          Z- G d de-          Z.d)d'Z/dS )*a  Canonical MCP Configuration Format.

This module defines the standard configuration format for Model Context Protocol (MCP) servers.
It provides a client-agnostic, extensible format that can be used across all MCP implementations.

The configuration format supports both stdio and remote (HTTP/SSE) transports, with comprehensive
field definitions for server metadata, authentication, and execution parameters.

Example configuration:
```json
{
    "mcpServers": {
        "my-server": {
            "command": "npx",
            "args": ["-y", "@my/mcp-server"],
            "env": {"API_KEY": "secret"},
            "timeout": 30000,
            "description": "My MCP server"
        }
    }
}
```
    )annotationsN)Path)TYPE_CHECKING	AnnotatedAnyLiteralcast)urlparse)AnyUrl	BaseModel
ConfigDictFieldmodel_validator)Selfoverride)ToolTransformConfig)FastMCPBaseModel)ClientTransportSSETransportStdioTransportStreamableHttpTransportFastMCPurlstr | AnyUrlreturnLiteral['http', 'sse']c                    t          |           } |                     d          st          d|            t          |           }|j        }t          j        d|          rdS dS )zB
    Infer the appropriate transport type from the given URL.
    httpzInvalid URL: z/sse(/|\?|&|$)sse)str
startswith
ValueErrorr
   pathresearch)r   
parsed_urlr$   s      /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/fastmcp/mcp_config.pyinfer_transport_type_from_urlr)   8   sm     c((C>>&!! 0...///#J?D 
y"D)) uv    c                       e Zd ZU dZ ed          Zded<   	  edd          Zded	<    edd
          Zded<   	 	 dd fdZ	ddZ
 xZS )_TransformingMCPServerMixinzAA mixin that enables wrapping an MCP Server with tool transforms..zdict[str, ToolTransformConfig]toolsNz!The tags to include in the proxy.)defaultdescriptionzset[str] | Noneinclude_tagsz!The tags to exclude in the proxy.exclude_tagsserver_name
str | Noneclient_namer   $tuple[FastMCP[Any], ClientTransport]c                    ddl m} ddlm} ddlm} t                                                      }t          ||          } |||          } |j	        ||| j
        | j        | j                  }||fS )z_Turn the Transforming MCPServer into a FastMCP Server and also return the underlying transport.r   r   )Client)r   )	transportname)r9   backendtool_transformationsr0   r1   )fastmcpr   fastmcp.clientr7   fastmcp.client.transportsr   superto_transportr	   as_proxyr-   r0   r1   )
selfr2   r4   r   r7   r   r8   clientwrapped_mcp_server	__class__s
            r(   #_to_server_and_underlying_transportz?_TransformingMCPServerMixin._to_server_and_underlying_transport\   s     	$#####))))))	
 	
 	
 	
 	
 	
 &+WW%9%9%;%;	)44	*0&9;*W*W*W-W-!%**
 
 
 "9,,r*   r   c                V    ddl m}  ||                                 d                   S )z2Get the transport for the transforming MCP server.r   )FastMCPTransport)mcp)r>   rH   rF   )rB   rH   s     r(   r@   z(_TransformingMCPServerMixin.to_transportw   s9    >>>>>>D$L$L$N$Nq$QRRRRr*   )NN)r2   r3   r4   r3   r   r5   )r   r   )__name__
__module____qualname____doc__r   r-   __annotations__r0   r1   rF   r@   __classcell__)rE   s   @r(   r,   r,   L   s         KK,1E#JJE66669$)E7% % %L    
 %*E7% % %L     #'"&- - - - - - -6S S S S S S S Sr*   r,   c                      e Zd ZU dZded<    ee          Zded<    ee          Z	ded<   d	Z
d
ed<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<    ed          ZddZdS )StdioMCPServerzMCP server configuration for stdio transport.

    This is the canonical configuration format for MCP servers using stdio transport.
    r!   commanddefault_factoryz	list[str]argsdict[str, Any]envstdiozLiteral['stdio']r8   NzLiteral['stdio'] | Nonetyper3   cwd
int | Nonetimeoutr/   icondict[str, Any] | Noneauthenticationallowextrar   r   c                T    ddl m}  || j        | j        | j        | j                  S )Nr   )r   )rR   rU   rW   rZ   )r>   r   rR   rU   rW   rZ   )rB   r   s     r(   r@   zStdioMCPServer.to_transport   sA    <<<<<<~L	
 
 
 	
r*   )r   r   )rJ   rK   rL   rM   rN   r   listrU   dictrW   r8   rY   rZ   r\   r/   r]   r_   r   model_configr@    r*   r(   rQ   rQ   ~   s          LLL eD111D1111%555C5555 #*I))))$(D(((( CG #K""""D -1N0000:G,,,L
 
 
 
 
 
r*   rQ   c                      e Zd ZdZdS )TransformingStdioMCPServerz$A Stdio server with tool transforms.NrJ   rK   rL   rM   rg   r*   r(   ri   ri      s        ....r*   ri   c                      e Zd ZU dZded<   dZded<    ee          Zded	<   dZ	d
ed<   dZ
ded<   dZded<   dZded<   dZded<   dZded<    edd          ZddZdS )RemoteMCPServerzMCP server configuration for HTTP/SSE transport.

    This is the canonical configuration format for MCP servers using remote transports.
    r!   r   Nz0Literal['http', 'streamable-http', 'sse'] | Noner8   rS   zdict[str, str]headerszAnnotated[str | Literal['oauth'] | httpx.Auth | None, Field(description='Either a string representing a Bearer token, the literal "oauth" to use OAuth authentication, or an httpx.Auth instance for custom authentication.')]authz'datetime.timedelta | int | float | Nonesse_read_timeoutr[   r\   r3   r/   r]   r^   r_   r`   T)rb   arbitrary_types_allowedr   &StreamableHttpTransport | SSETransportc                    ddl m}m} | j        t	          | j                  }n| j        }|dk    r# || j        | j        | j        | j                  S  || j        | j        | j        | j                  S )Nr   )r   r   r    )rm   rn   ro   )	r>   r   r   r8   r)   r   rm   rn   ro   )rB   r   r   r8   s       r(   r@   zRemoteMCPServer.to_transport   s    SSSSSSSS>!5dh??III<Y!%!6	    +*Y!%!6	   r*   )r   rq   )rJ   rK   rL   rM   rN   r8   r   re   rm   rn   ro   r\   r/   r]   r_   r   rf   r@   rg   r*   r(   rl   rl      s	          HHH CGIFFFF#eD999G9999 	 	     AEDDDDG #K""""D -1N0000:t  L     r*   rl   c                      e Zd ZdZdS )TransformingRemoteMCPServerz%A Remote server with tool transforms.Nrj   rg   r*   r(   rt   rt      s        ////r*   rt   c                      e Zd ZU dZ ee          Zded<    ed          Z	 e
d          edd                        ZddZedd            ZddZd dZed!d            ZdS )"	MCPConfiga8  A configuration object for MCP Servers that conforms to the canonical MCP configuration format
    while adding additional fields for enabling FastMCP-specific features like tool transformations
    and filtering by tags.

    For an MCPConfig that is strictly canonical, see the `CanonicalMCPConfig` class.
    rS   zdict[str, MCPServerTypes]
mcpServersr`   ra   before)modevaluesrV   r   c                p    d|vr1t          d |                                D                       }|rd|iS |S )zMIf there's no mcpServers key but there are server configs at root, wrap them.rw   c              3  P   K   | ]!}t          |t                    od |v pd|v V  "dS )rR   r   N)
isinstancere   ).0vs     r(   	<genexpr>z1MCPConfig.wrap_servers_at_root.<locals>.<genexpr>  sR         1d##Fa)E5A:     r*   )anyrz   )clsrz   has_serverss      r(   wrap_servers_at_rootzMCPConfig.wrap_servers_at_root   s\     v%%      K  .$f--r*   r9   r!   serverMCPServerTypesNonec                    || j         |<   dS z,Add or update a server in the configuration.Nrw   rB   r9   r   s      r(   
add_serverzMCPConfig.add_server  s     &r*   configr   c                ,    |                      |          S )z/Parse MCP configuration from dictionary format.)model_validate)r   r   s     r(   	from_dictzMCPConfig.from_dict  s     !!&)))r*   c                .    |                      d          S )z>Convert MCPConfig to dictionary format, preserving all fields.Texclude_none)
model_dump)rB   s    r(   to_dictzMCPConfig.to_dict  s    D111r*   	file_pathr   c                    |j                             dd           |                    |                     d                     dS )z!Write configuration to JSON file.T)parentsexist_ok   )indentN)parentmkdir
write_textmodel_dump_json)rB   r   s     r(   write_to_filezMCPConfig.write_to_file  sI    td;;;T111;;<<<<<r*   c                    |                                 r=|                                                                x}r|                     |          S t	          d|           )z"Load configuration from JSON file.z&No MCP servers defined in the config: )exists	read_textstripmodel_validate_jsonr#   )r   r   contents      r(   	from_filezMCPConfig.from_file   sg      	4i.A.A.C.C.I.I.K.K#K7 	4**7333M)MMNNNr*   N)rz   rV   r   rV   )r9   r!   r   r   r   r   )r   rV   r   r   )r   rV   )r   r   r   r   )r   r   r   r   )rJ   rK   rL   rM   r   re   rw   rN   r   rf   r   classmethodr   r   r   r   r   r   rg   r*   r(   rv   rv      s
          -2E$,G,G,GJGGGG:G,,,L_(###   [ $#' ' ' ' * * * [*2 2 2 2= = = =
 O O O [O O Or*   rv   c                  N    e Zd ZU dZ ee          Zded<   edd            Z	dS )CanonicalMCPConfigzCanonical MCP configuration format.

    This defines the standard configuration format for Model Context Protocol servers.
    The format is designed to be client-agnostic and extensible for future use cases.
    rS   z"dict[str, CanonicalMCPServerTypes]rw   r9   r!   r   CanonicalMCPServerTypesr   r   c                    || j         |<   dS r   r   r   s      r(   r   zCanonicalMCPConfig.add_server2  s     !'r*   N)r9   r!   r   r   r   r   )
rJ   rK   rL   rM   r   re   rw   rN   r   r   rg   r*   r(   r   r   )  s^           6;U45P5P5PJPPPP' ' ' X' ' 'r*   r   r   r   r2   r!   server_configr   r   c                |   t                               |           }|j                            |          x}rZ|                                }|                    d          }|                    i ||          }|                    ||           n|                    ||           |                    |            dS )zUpdate an MCP configuration file from a server object, preserving existing fields.

    This is used for updating the mcpServer configurations of third-party tools so we do not
    worry about transforming server objects here.Tr   N)rv   r   rw   getr   r   r   r   )r   r2   r   r   existing_serverexisting_dictnew_dictmerged_configs           r(   update_config_filer   8  s       ++F !+//<<< 6'2244 +++>> &445R5R5RSS+}5555+}555
#####r*   )r   r   r   r   )r   r   r2   r!   r   r   r   r   )0rM   
__future__r   datetimer%   pathlibr   typingr   r   r   r   r	   urllib.parser
   httpxpydanticr   r   r   r   r   typing_extensionsr   r   fastmcp.tools.tool_transformr   fastmcp.utilities.typesr   r>   r   r   r   r   fastmcp.server.serverr   r)   r,   rQ   ri   rl   rt   TransformingMCPServerTypesr   r   rv   r   r   rg   r*   r(   <module>r      s   0 # " " " " "  				       ? ? ? ? ? ? ? ? ? ? ? ? ? ? ! ! ! ! ! !               - , , , , , , , < < < < < < 4 4 4 4 4 4 .            .-----   (/S /S /S /S /S"2 /S /S /Sd&
 &
 &
 &
 &
Y &
 &
 &
R/ / / / /!<n / / /: : : : :i : : :z0 0 0 0 0"= 0 0 0 8:UU (?: +.EE3O 3O 3O 3O 3O	 3O 3O 3Ol' ' ' ' ' ' ' '$ $ $ $ $ $r*   