12
12
from pydantic import BaseModel , Field
13
13
from pydantic .networks import AnyUrl
14
14
from pydantic_settings import BaseSettings , SettingsConfigDict
15
- from typing_extensions import TypeAlias
16
15
17
16
from mcp .server .fastmcp .exceptions import ResourceError
18
17
from mcp .server .fastmcp .prompts import Prompt , PromptManager
26
25
from mcp .server .stdio import stdio_server
27
26
from mcp .shared .context import RequestContext
28
27
from mcp .types import (
28
+ AnyFunction ,
29
29
EmbeddedResource ,
30
30
GetPromptResult ,
31
31
ImageContent ,
49
49
50
50
logger = get_logger (__name__ )
51
51
52
- _Function : TypeAlias = Callable [..., Any ]
53
-
54
52
55
53
class Settings (BaseSettings ):
56
54
"""FastMCP server settings.
@@ -217,7 +215,7 @@ async def read_resource(self, uri: AnyUrl | str) -> ReadResourceContents:
217
215
218
216
def add_tool (
219
217
self ,
220
- fn : _Function ,
218
+ fn : AnyFunction ,
221
219
name : str | None = None ,
222
220
description : str | None = None ,
223
221
) -> None :
@@ -235,7 +233,7 @@ def add_tool(
235
233
236
234
def tool (
237
235
self , name : str | None = None , description : str | None = None
238
- ) -> Callable [[_Function ], _Function ]:
236
+ ) -> Callable [[AnyFunction ], AnyFunction ]:
239
237
"""Decorator to register a tool.
240
238
241
239
Tools can optionally request a Context object by adding a parameter with the
@@ -268,7 +266,7 @@ async def async_tool(x: int, context: Context) -> str:
268
266
"Did you forget to call it? Use @tool() instead of @tool"
269
267
)
270
268
271
- def decorator (fn : _Function ) -> _Function :
269
+ def decorator (fn : AnyFunction ) -> AnyFunction :
272
270
self .add_tool (fn , name = name , description = description )
273
271
return fn
274
272
@@ -289,7 +287,7 @@ def resource(
289
287
name : str | None = None ,
290
288
description : str | None = None ,
291
289
mime_type : str | None = None ,
292
- ) -> Callable [[_Function ], _Function ]:
290
+ ) -> Callable [[AnyFunction ], AnyFunction ]:
293
291
"""Decorator to register a function as a resource.
294
292
295
293
The function will be called when the resource is read to generate its content.
@@ -333,7 +331,7 @@ async def get_weather(city: str) -> str:
333
331
"Did you forget to call it? Use @resource('uri') instead of @resource"
334
332
)
335
333
336
- def decorator (fn : _Function ) -> _Function :
334
+ def decorator (fn : AnyFunction ) -> AnyFunction :
337
335
# Check if this should be a template
338
336
has_uri_params = "{" in uri and "}" in uri
339
337
has_func_params = bool (inspect .signature (fn ).parameters )
@@ -381,7 +379,7 @@ def add_prompt(self, prompt: Prompt) -> None:
381
379
382
380
def prompt (
383
381
self , name : str | None = None , description : str | None = None
384
- ) -> Callable [[_Function ], _Function ]:
382
+ ) -> Callable [[AnyFunction ], AnyFunction ]:
385
383
"""Decorator to register a prompt.
386
384
387
385
Args:
@@ -422,7 +420,7 @@ async def analyze_file(path: str) -> list[Message]:
422
420
"Did you forget to call it? Use @prompt() instead of @prompt"
423
421
)
424
422
425
- def decorator (func : _Function ) -> _Function :
423
+ def decorator (func : AnyFunction ) -> AnyFunction :
426
424
prompt = Prompt .from_function (func , name = name , description = description )
427
425
self .add_prompt (prompt )
428
426
return func
0 commit comments