1616
1717import asyncio
1818import base64
19+ from collections .abc import Awaitable
1920import inspect
2021import logging
2122from typing import Any
2223from typing import Callable
23- from typing import Dict
24- from typing import List
25- from typing import Optional
2624from typing import Protocol
2725from typing import runtime_checkable
28- from typing import Union
2926import warnings
3027
3128from fastapi .openapi .models import APIKeyIn
@@ -104,9 +101,9 @@ def __call__(
104101 self ,
105102 tool_name : str ,
106103 * ,
107- callback_context : Optional [ CallbackContext ] = None ,
104+ callback_context : CallbackContext | None = None ,
108105 ** kwargs : Any ,
109- ) -> Optional [ ProgressFnT ] :
106+ ) -> ProgressFnT | None :
110107 """Create a progress callback for a specific tool.
111108
112109 Args:
@@ -139,15 +136,17 @@ def __init__(
139136 * ,
140137 mcp_tool : McpBaseTool ,
141138 mcp_session_manager : MCPSessionManager ,
142- auth_scheme : Optional [AuthScheme ] = None ,
143- auth_credential : Optional [AuthCredential ] = None ,
144- require_confirmation : Union [bool , Callable [..., bool ]] = False ,
145- header_provider : Optional [
146- Callable [[ReadonlyContext ], Dict [str , str ]]
147- ] = None ,
148- progress_callback : Optional [
149- Union [ProgressFnT , ProgressCallbackFactory ]
150- ] = None ,
139+ auth_scheme : AuthScheme | None = None ,
140+ auth_credential : AuthCredential | None = None ,
141+ require_confirmation : bool | Callable [..., bool ] = False ,
142+ header_provider : (
143+ Callable [
144+ [ReadonlyContext ],
145+ dict [str , str ] | Awaitable [dict [str , str ]],
146+ ]
147+ | None
148+ ) = None ,
149+ progress_callback : ProgressFnT | ProgressCallbackFactory | None = None ,
151150 ):
152151 """Initializes an McpTool.
153152
@@ -225,7 +224,7 @@ def raw_mcp_tool(self) -> McpBaseTool:
225224 return self ._mcp_tool
226225
227226 @property
228- def visibility (self ) -> List [str ]:
227+ def visibility (self ) -> list [str ]:
229228 """Returns the visibility if this MCP tool meta has one."""
230229 meta = getattr (self .raw_mcp_tool , "meta" , None )
231230 if not meta or not isinstance (meta , dict ):
@@ -238,7 +237,7 @@ def visibility(self) -> List[str]:
238237 return []
239238
240239 @property
241- def mcp_app_resource_uri (self ) -> Optional [ str ] :
240+ def mcp_app_resource_uri (self ) -> str | None :
242241 """Returns the MCP App UI resource URI if this tool has one.
243242
244243 MCP Apps declare a UI resource via `meta.ui.resourceUri` in the tool
@@ -379,7 +378,7 @@ async def run_async(
379378 @override
380379 async def _run_async_impl (
381380 self , * , args , tool_context : ToolContext , credential : AuthCredential
382- ) -> Dict [str , Any ]:
381+ ) -> dict [str , Any ]:
383382 """Runs the tool asynchronously.
384383
385384 Args:
@@ -396,8 +395,10 @@ async def _run_async_impl(
396395 dynamic_headers = self ._header_provider (
397396 ReadonlyContext (tool_context ._invocation_context ) # pylint: disable=protected-access
398397 )
398+ if inspect .isawaitable (dynamic_headers ):
399+ dynamic_headers = await dynamic_headers
399400
400- headers : Dict [str , str ] = {}
401+ headers : dict [str , str ] = {}
401402 if auth_headers :
402403 headers .update (auth_headers )
403404 if dynamic_headers :
@@ -406,7 +407,7 @@ async def _run_async_impl(
406407
407408 # Propagate trace context in the _meta field as sprcified by MCP protocol.
408409 # See https://agentclientprotocol.com/protocol/extensibility#the-meta-field
409- trace_carrier : Dict [str , str ] = {}
410+ trace_carrier : dict [str , str ] = {}
410411 propagate .get_global_textmap ().inject (carrier = trace_carrier )
411412 meta_trace_context = trace_carrier if trace_carrier else None
412413
@@ -468,15 +469,15 @@ async def _run_async_impl(
468469 )
469470 return result
470471
471- def _detect_error_in_response (self , response : Any ) -> Optional [ str ] :
472+ def _detect_error_in_response (self , response : Any ) -> str | None :
472473 """Telemetry hook: returns an error type if the response indicates an error."""
473474 if isinstance (response , dict ) and response .get ("isError" ):
474475 return "MCP_TOOL_ERROR"
475476 return None
476477
477478 def _resolve_progress_callback (
478479 self , tool_context : ToolContext
479- ) -> Optional [ ProgressFnT ] :
480+ ) -> ProgressFnT | None :
480481 """Resolve the progress callback for the current invocation.
481482
482483 If progress_callback is a ProgressCallbackFactory, call it to create
@@ -510,7 +511,7 @@ def _resolve_progress_callback(
510511
511512 async def _get_headers (
512513 self , tool_context : ToolContext , credential : AuthCredential
513- ) -> Optional [ dict [str , str ]] :
514+ ) -> dict [str , str ] | None :
514515 """Extracts authentication headers from credentials.
515516
516517 Args:
@@ -524,7 +525,7 @@ async def _get_headers(
524525 ValueError: If API key authentication is configured for non-header
525526 location.
526527 """
527- headers : Optional [ dict [str , str ]] = None
528+ headers : dict [str , str ] | None = None
528529 if credential :
529530 if credential .oauth2 :
530531 headers = {"Authorization" : f"Bearer { credential .oauth2 .access_token } " }
0 commit comments