Skip to content

Commit 05bad54

Browse files
committed
centralize type
1 parent dc1031d commit 05bad54

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/mcp/server/fastmcp/server.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pydantic import BaseModel, Field
1313
from pydantic.networks import AnyUrl
1414
from pydantic_settings import BaseSettings, SettingsConfigDict
15-
from typing_extensions import TypeAlias
1615

1716
from mcp.server.fastmcp.exceptions import ResourceError
1817
from mcp.server.fastmcp.prompts import Prompt, PromptManager
@@ -26,6 +25,7 @@
2625
from mcp.server.stdio import stdio_server
2726
from mcp.shared.context import RequestContext
2827
from mcp.types import (
28+
AnyFunction,
2929
EmbeddedResource,
3030
GetPromptResult,
3131
ImageContent,
@@ -49,8 +49,6 @@
4949

5050
logger = get_logger(__name__)
5151

52-
_Function: TypeAlias = Callable[..., Any]
53-
5452

5553
class Settings(BaseSettings):
5654
"""FastMCP server settings.
@@ -217,7 +215,7 @@ async def read_resource(self, uri: AnyUrl | str) -> ReadResourceContents:
217215

218216
def add_tool(
219217
self,
220-
fn: _Function,
218+
fn: AnyFunction,
221219
name: str | None = None,
222220
description: str | None = None,
223221
) -> None:
@@ -235,7 +233,7 @@ def add_tool(
235233

236234
def tool(
237235
self, name: str | None = None, description: str | None = None
238-
) -> Callable[[_Function], _Function]:
236+
) -> Callable[[AnyFunction], AnyFunction]:
239237
"""Decorator to register a tool.
240238
241239
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:
268266
"Did you forget to call it? Use @tool() instead of @tool"
269267
)
270268

271-
def decorator(fn: _Function) -> _Function:
269+
def decorator(fn: AnyFunction) -> AnyFunction:
272270
self.add_tool(fn, name=name, description=description)
273271
return fn
274272

@@ -289,7 +287,7 @@ def resource(
289287
name: str | None = None,
290288
description: str | None = None,
291289
mime_type: str | None = None,
292-
) -> Callable[[_Function], _Function]:
290+
) -> Callable[[AnyFunction], AnyFunction]:
293291
"""Decorator to register a function as a resource.
294292
295293
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:
333331
"Did you forget to call it? Use @resource('uri') instead of @resource"
334332
)
335333

336-
def decorator(fn: _Function) -> _Function:
334+
def decorator(fn: AnyFunction) -> AnyFunction:
337335
# Check if this should be a template
338336
has_uri_params = "{" in uri and "}" in uri
339337
has_func_params = bool(inspect.signature(fn).parameters)
@@ -381,7 +379,7 @@ def add_prompt(self, prompt: Prompt) -> None:
381379

382380
def prompt(
383381
self, name: str | None = None, description: str | None = None
384-
) -> Callable[[_Function], _Function]:
382+
) -> Callable[[AnyFunction], AnyFunction]:
385383
"""Decorator to register a prompt.
386384
387385
Args:
@@ -422,7 +420,7 @@ async def analyze_file(path: str) -> list[Message]:
422420
"Did you forget to call it? Use @prompt() instead of @prompt"
423421
)
424422

425-
def decorator(func: _Function) -> _Function:
423+
def decorator(func: AnyFunction) -> AnyFunction:
426424
prompt = Prompt.from_function(func, name=name, description=description)
427425
self.add_prompt(prompt)
428426
return func

src/mcp/types.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, Any, Generic, Literal, TypeVar
1+
from typing import Annotated, Any, Callable, Generic, Literal, TypeAlias, TypeVar
22

33
from pydantic import BaseModel, ConfigDict, Field, FileUrl, RootModel
44
from pydantic.networks import AnyUrl
@@ -27,6 +27,7 @@
2727
Cursor = str
2828
Role = Literal["user", "assistant"]
2929
RequestId = str | int
30+
AnyFunction: TypeAlias = Callable[..., Any]
3031

3132

3233
class RequestParams(BaseModel):

0 commit comments

Comments
 (0)