
    kKj                     J   d 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
mZmZmZ ddlmZ ddlmZmZ d	eeef   fd
Zd	eeef   fdZd	edz  fdZe	 	 	 ddededededededz  dedz  dedz  d	ee
ddf   fd       Ze	 ddededededz  d	ee
ddf   f
d       Zg dZy)zServer-side telemetry helpers.    )	Generator)contextmanager)request_ctx)Context)SpanSpanKindStatus
StatusCode)	ToolError)extract_trace_context
get_tracerreturnc                      ddl m}  i }	  |        }|rE|j                  r|j                  |d<   |j                  rdj	                  |j                        |d<   |S # t
        $ r Y |S w xY w)z>Get auth attributes for the current request, if authenticated.r   )get_access_tokenz
enduser.id zenduser.scope)fastmcp.server.dependenciesr   	client_idscopesjoinRuntimeError)r   attrstokens      i/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/server/telemetry.pyget_auth_span_attributesr      sm    <E "&+ool#||),%,,)?o& L  Ls   AA 	A'&A'c                      ddl m}  i }	  |        }|j                  |j                  |j                  |d<   |S # t        $ r Y |S w xY w)z/Get session attributes for the current request.r   )get_contextzmcp.session.id)r   r   request_context
session_idr   )r   r   ctxs      r   get_session_span_attributesr       sZ    7Em*s~~/I&)nnE"# L  Ls   .: 	AANc                      	 t        j                         } | r6t        | d      r*| j                  rt	        t        | j                              S y# t        $ r Y yw xY w)zCGet parent trace context from request meta for distributed tracing.metaN)r   gethasattrr"   r   dictLookupError)req_ctxs    r   _get_parent_trace_contextr(   -   sU    //#ww/GLL(gll);<<   s   AA 	AAnamemethodserver_namecomponent_typecomponent_keyresource_uri	tool_nameprompt_namec           
   #     K   t               }|j                  | t               t        j                        5 }	|	j                         rA||||dt               t               }
|||
d<   |||
d<   |||
d<   |	j                  |
       	 |	 	 ddd       y# t        $ r}|	j                         r|t        |t              rdnt        |      j                  }|	j                  d|       |	j                  |       |	j!                  t#        t$        j&                  t)        |                    d}~ww xY w# 1 sw Y   yxY ww)	zCreate a SERVER span with standard MCP attributes and auth context.

    Automatically records any exception on the span and sets error status.
    )contextkind)mcp.method.namezfastmcp.server.namezfastmcp.component.typefastmcp.component.keyNzmcp.resource.urizgen_ai.tool.namezgen_ai.prompt.name
tool_error
error.type)r   start_as_current_spanr(   r   SERVERis_recordingr   r    set_attributes	Exception
isinstance
_ToolErrortype__qualname__set_attributerecord_exception
set_statusr	   r
   ERRORstr)r)   r*   r+   r,   r-   r.   r/   r0   tracerspanr   e
error_types                r   server_spanrJ   8   sN     \F		%	%)+__ 
& 
 
 $*'2*8)6	% +,	% ./	%E ',8()$,5()&.9*+&
	J1
 
2  	  "$.q*$=L47CWCW  ""<<%%a(z'7'7Q @A	3
 
s<   5EAD6
B	E	D3!BD..D33D66D?;Eprovider_typec           
   #     K   t               }|j                  d|        5 }|j                         r||d}|||d<   |j                  |       	 | 	 ddd       y# t        $ r}|j                         r|t        |t              rdnt        |      j                  }|j                  d|       |j                  |       |j                  t        t        j                  t        |                    d}~ww xY w# 1 sw Y   yxY ww)zCreate an INTERNAL span for provider delegation.

    Used by FastMCPProvider when delegating to mounted servers.
    Automatically records any exception on the span and sets error status.
    z	delegate )zfastmcp.provider.typer5   Nr4   r6   r7   )r   r8   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   r	   r
   rD   rE   )	r)   rK   r-   r*   rF   rG   r   rH   rI   s	            r   delegate_spanrM   l   s      \F		%	%	$&8	9T)6)6%E !+1'(&
	J 
:	9  	  "$.q*$=L47CWCW  ""<<%%a(z'7'7Q @A	 
:	9s;   D.C<A	D	C9'BC44C99C<<DD)rM   r   r    rJ   )NNN)N)__doc__collections.abcr   
contextlibr   mcp.server.lowlevel.serverr   opentelemetry.contextr   opentelemetry.tracer   r   r	   r
   fastmcp.exceptionsr   r>   fastmcp.telemetryr   r   r%   rE   r   r    r(   rJ   rM   __all__     r   <module>rY      sE   $ % % 2 ) B B 6 ?$sCx. "T#s(^ 7T>    $ "0
00 0 	0
 0 *0 Tz0 t0 tT4 0 0f 
 	
  $J	
 tT4  DrX   