
    k7ia,                       S r SSKJr  SSKrSSKrSSKJr  SSKJrJ	r	J
r
JrJr  SSKJr  SSKrSSKJrJrJrJrJr  SSKJrJr  SS	KJr  SS
KJr  \(       a  SSKJrJrJ r J!r!  SSK"J#r#      SS jr$ " S S\5      r% " S S\5      r& " S S\%\&5      r' " S S\5      r( " S S\%\(5      r)\'\)-  r*\&\(-  r+\*\+-  r, " S S\5      r- " S S\-5      r.        SS jr/g)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FastMCPc                    [        U 5      n U R                  S5      (       d  [        SU  35      e[        U 5      nUR                  n[
        R                  " SU5      (       a  gg)z:
Infer the appropriate transport type from the given URL.
httpzInvalid URL: z/sse(/|\?|&|$)sse)str
startswith
ValueErrorr
   pathresearch)url
parsed_urlr    s      L/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/mcp_config.pyinfer_transport_type_from_urlr&   8   sZ     c(C>>&!!=.//#J??D 
yy"D))    c                     ^  \ rS rSr% Sr\" S5      rS\S'    \" SSS9rS	\S
'   \" SSS9r	S	\S'     S     SU 4S jjjr
SS jrSrU =r$ )_TransformingMCPServerMixinL   zAA 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_tagsc                   > SSK Jn  SSKJn  SSKJn  [        T	U ]  5       n[        XV5      nU" XbS9nUR                  " UUU R                  U R                  U R                  S9nX4$ )z_Turn the Transforming MCPServer into a FastMCP Server and also return the underlying transport.r   r   )Client)r   )	transportname)r3   backendtool_transformationsr.   r/   )fastmcpr   fastmcp.clientr1   fastmcp.client.transportsr   superto_transportr	   as_proxyr+   r.   r/   )
selfserver_nameclient_namer   r1   r   r2   clientwrapped_mcp_server	__class__s
            r%   #_to_server_and_underlying_transport?_TransformingMCPServerMixin._to_server_and_underlying_transport\   sn     	$)	
 &+W%9%;	4	*09*W$--!%****
 ",,r'   c                <    SSK Jn  U" U R                  5       S   S9$ )z2Get the transport for the transforming MCP server.r   )FastMCPTransport)mcp)r8   rE   rB   )r<   rE   s     r%   r:   (_TransformingMCPServerMixin.to_transportw   s    >D$L$L$Nq$QRRr'    )NN)r=   
str | Noner>   rI   returnz$tuple[FastMCP[Any], ClientTransport])rJ   r   )__name__
__module____qualname____firstlineno____doc__r   r+   __annotations__r.   r/   rB   r:   __static_attributes____classcell__)rA   s   @r%   r)   r)   L   s    K,1#JE)69$)7%L/ 
 %*7%L/  #'"&--  - 
.	- -6S Sr'   r)   c                      \ rS rSr% SrS\S'   \" \S9rS\S'   \" \	S9r
S\S	'   S
rS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   \" SS9rSS jrSrg)StdioMCPServer~   zMCP 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']r2   NzLiteral['stdio'] | NonetyperI   cwd
int | Nonetimeoutr-   icondict[str, Any] | Noneauthenticationallowextrac                p    SSK Jn  U" U R                  U R                  U R                  U R
                  S9$ )Nr   )r   )rV   rY   r[   r^   )r8   r   rV   rY   r[   r^   )r<   r   s     r%   r:   StdioMCPServer.to_transport   s.    <LL	
 	
r'   rH   )rJ   r   )rK   rL   rM   rN   rO   rP   r   listrY   dictr[   r2   r]   r^   r`   r-   ra   rc   r   model_configr:   rQ   rH   r'   r%   rT   rT   ~   s     L D1D)15C5 #*I)$(D
!( CGZ #K"D* -1N)0G,L
r'   rT   c                      \ rS rSrSrSrg)TransformingStdioMCPServer   z$A Stdio server with tool transforms.rH   NrK   rL   rM   rN   rO   rQ   rH   r'   r%   rm   rm      s    .r'   rm   c                      \ rS rSr% SrS\S'   SrS\S'   \" \S9r	S	\S
'   Sr
S\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   \" SSS9rSS jrSrg)RemoteMCPServer   zMCP 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'] | Noner2   rW   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`   rI   r-   ra   rb   rc   rd   T)rf   arbitrary_types_allowedc                @   SSK JnJn  U R                  c  [	        U R
                  5      nOU R                  nUS:X  a1  U" U R
                  U R                  U R                  U R                  S9$ U" U R
                  U R                  U R                  U R                  S9$ )Nr   )r   r   r   )rs   rt   ru   )	r8   r   r   r2   r&   r#   rs   rt   ru   )r<   r   r   r2   s       r%   r:   RemoteMCPServer.to_transport   s    S>>!5dhh?IIYY!%!6!6	  +YY!%!6!6	 r'   rH   )rJ   z&StreamableHttpTransport | SSETransport)rK   rL   rM   rN   rO   rP   r2   r   rj   rs   rt   ru   r`   r-   ra   rc   r   rk   r:   rQ   rH   r'   r%   rq   rq      s     
H CGI?F#D9G^9 	 	   AE=DGZ #K"D* -1N)0tLr'   rq   c                      \ rS rSrSrSrg)TransformingRemoteMCPServer   z%A Remote server with tool transforms.rH   Nro   rH   r'   r%   rz   rz      s    /r'   rz   c                      \ rS rSr% Sr\" \S9rS\S'   \	" SS9r
\" SS	9\SS
 j5       5       rSS jr\SS j5       rSS jrSS jr\SS j5       rSrg)	MCPConfig   a(  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.
rW   zdict[str, MCPServerTypes]
mcpServersrd   re   before)modec                h    SU;  a+  [        S UR                  5        5       5      nU(       a  SU0$ U$ )zMIf there's no mcpServers key but there are server configs at root, wrap them.r   c              3  r   #    U  H-  n[        U[        5      =(       a    S U;   =(       d    SU;   v   M/     g7f)rV   r#   N)
isinstancerj   ).0vs     r%   	<genexpr>1MCPConfig.wrap_servers_at_root.<locals>.<genexpr>  s3      (A 1d#Fa)E5A:F(s   57)anyvalues)clsr   has_serverss      r%   wrap_servers_at_rootMCPConfig.wrap_servers_at_root   s>     v%  K $f--r'   c                     X R                   U'   gz,Add or update a server in the configuration.Nr   r<   r3   servers      r%   
add_serverMCPConfig.add_server  s     &r'   c                $    U R                  U5      $ )z/Parse MCP configuration from dictionary format.)model_validate)r   configs     r%   	from_dictMCPConfig.from_dict  s     !!&))r'   c                     U R                  SS9$ )z>Convert MCPConfig to dictionary format, preserving all fields.Texclude_none)
model_dump)r<   s    r%   to_dictMCPConfig.to_dict  s    D11r'   c                t    UR                   R                  SSS9  UR                  U R                  SS95        g)z!Write configuration to JSON file.T)parentsexist_ok   )indentN)parentmkdir
write_textmodel_dump_json)r<   	file_paths     r%   write_to_fileMCPConfig.write_to_file  s7    td;T111;<r'   c                    UR                  5       (       a6  UR                  5       R                  5       =n(       a  U R                  U5      $ [	        SU 35      e)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_fileMCPConfig.from_file   sS     i.A.A.C.I.I.K#K7#K**733A)MNNr'   rH   N)r   rZ   rJ   rZ   )r3   r   r   MCPServerTypesrJ   None)r   rZ   rJ   r   )rJ   rZ   )r   r   rJ   r   )r   r   rJ   r   )rK   rL   rM   rN   rO   r   rj   r   rP   r   rk   r   classmethodr   r   r   r   r   r   rQ   rH   r'   r%   r}   r}      s{     -2$,GJ)GG,L(#  $' * *2=
 O Or'   r}   c                  D    \ rS rSr% Sr\" \S9rS\S'   \	S	S j5       r
Srg)
CanonicalMCPConfigi)  zCanonical 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.
rW   z"dict[str, CanonicalMCPServerTypes]r   c                     X R                   U'   gr   r   r   s      r%   r   CanonicalMCPConfig.add_server2  s     !'r'   rH   N)r3   r   r   CanonicalMCPServerTypesrJ   r   )rK   rL   rM   rN   rO   r   rj   r   rP   r   r   rQ   rH   r'   r%   r   r   )  s+     6;45PJ2P' 'r'   r   c                B   [         R                  U 5      nUR                  R                  U5      =n(       aF  UR	                  5       nUR	                  SS9nUR                  0 UEUE5      nUR                  X5        OUR                  X5        UR                  U 5        g)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)r}   r   r   getr   r   r   r   )r   r=   server_configr   existing_serverexisting_dictnew_dictmerged_configs           r%   update_config_filer   8  s       +F !++//<<<'224 +++> &445R5R5RS+5+5
#r'   )r#   zstr | AnyUrlrJ   zLiteral['http', 'sse'])r   r   r=   r   r   r   rJ   r   )0rO   
__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   r8   r   r   r   r   fastmcp.server.serverr   r&   r)   rT   rm   rq   rz   TransformingMCPServerTypesr   r   r}   r   r   rH   r'   r%   <module>r      s  0 #  	  ? ? !   - < 4  .	(/S"2 /Sd&
Y &
R/!<n /:i :z0"= 0 8:UU (?: +.EE3O	 3Ol' '$$$ +$ 
	$r'   