Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170---title: typessidebarTitle: types---
# `fastmcp.utilities.types`
Common types used across FastMCP.
## Functions
### `get_fn_name` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L34" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonget_fn_name(fn: Callable[..., Any]) -> str```
### `get_cached_typeadapter` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L45" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonget_cached_typeadapter(cls: T) -> TypeAdapter[T]```
TypeAdapters are heavy objects, and in an application context we'd typicallycreate them once in a global scope and reuse them as often as possible.However, this isn't feasible for user-generated functions. Instead, we use acache to minimize the cost of creating them as much as possible.
### `issubclass_safe` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L123" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonissubclass_safe(cls: type, base: type) -> bool```
Check if cls is a subclass of base, even if cls is a type variable.
### `is_class_member_of_type` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L133" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonis_class_member_of_type(cls: Any, base: type) -> bool```
Check if cls is a member of base, even if cls is a type variable.
Base can be a type, a UnionType, or an Annotated type. Generic types are notconsidered members (e.g. T is not a member of list\[T]).
### `find_kwarg_by_type` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L155" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonfind_kwarg_by_type(fn: Callable, kwarg_type: type) -> str | None```
Find the name of the kwarg that is of type kwarg_type.
Includes union types that contain the kwarg_type, as well as Annotated types.
### `create_function_without_params` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L181" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncreate_function_without_params(fn: Callable[..., Any], exclude_params: list[str]) -> Callable[..., Any]```
Create a new function with the same code but without the specified parameters in annotations.
This is used to exclude parameters from type adapter processing when they can't be serialized.The excluded parameters are removed from the function's __annotations__ dictionary.
### `replace_type` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L454" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonreplace_type(type_, type_map: dict[type, type])```
Given a (possibly generic, nested, or otherwise complex) type, replaces allinstances of old_type with new_type.
This is useful for transforming types when creating tools.
**Args:**- `type_`: The type to replace instances of old_type with new_type.- `old_type`: The type to replace.- `new_type`: The type to replace old_type with.
Examples:```python>>> replace_type(list[int | bool], {int: str})list[str | bool]
>>> replace_type(list[list[int]], {int: str})list[list[str]]```
## Classes
### `FastMCPBaseModel` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L38" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Base model for FastMCP models.
### `Image` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L238" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Helper class for returning images from tools.
**Methods:**
#### `to_image_content` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L289" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonto_image_content(self, mime_type: str | None = None, annotations: Annotations | None = None) -> mcp.types.ImageContent```
Convert to MCP ImageContent.
#### `to_data_uri` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L304" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonto_data_uri(self, mime_type: str | None = None) -> str```
Get image as a data URI.
### `Audio` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L310" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Helper class for returning audio from tools.
**Methods:**
#### `to_audio_content` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L347" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonto_audio_content(self, mime_type: str | None = None, annotations: Annotations | None = None) -> mcp.types.AudioContent```
### `File` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L368" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Helper class for returning file data from tools.
**Methods:**
#### `to_resource_content` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L407" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonto_resource_content(self, mime_type: str | None = None, annotations: Annotations | None = None) -> mcp.types.EmbeddedResource```
### `ContextSamplingFallbackProtocol` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/utilities/types.py#L491" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>