mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 15:19:10 +02:00
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
168 lines
6.2 KiB
Text
168 lines
6.2 KiB
Text
---
|
|
title: types
|
|
sidebarTitle: types
|
|
---
|
|
|
|
# `fastmcp.utilities.types`
|
|
|
|
|
|
Common types used across FastMCP.
|
|
|
|
## Functions
|
|
|
|
### `get_fn_name` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/utilities/types.py#L39" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
get_fn_name(fn: Callable[..., Any]) -> str
|
|
```
|
|
|
|
### `get_cached_typeadapter` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/utilities/types.py#L50" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
get_cached_typeadapter(cls: T) -> TypeAdapter[T]
|
|
```
|
|
|
|
|
|
TypeAdapters are heavy objects, and in an application context we'd typically
|
|
create 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 a
|
|
cache to minimize the cost of creating them as much as possible.
|
|
|
|
|
|
### `issubclass_safe` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/utilities/types.py#L128" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
issubclass_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/fastmcp_slim/fastmcp/utilities/types.py#L138" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
is_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 not
|
|
considered 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/fastmcp_slim/fastmcp/utilities/types.py#L160" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
find_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/fastmcp_slim/fastmcp/utilities/types.py#L186" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
create_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/fastmcp_slim/fastmcp/utilities/types.py#L469" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
replace_type(type_, type_map: dict[type, type])
|
|
```
|
|
|
|
|
|
Given a (possibly generic, nested, or otherwise complex) type, replaces all
|
|
instances of keys in type_map with their corresponding values.
|
|
|
|
This is useful for transforming types when creating tools.
|
|
|
|
**Args:**
|
|
- `type_`: The type to transform.
|
|
- `type_map`: A mapping of types to replace (keys are replaced by values).
|
|
|
|
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/fastmcp_slim/fastmcp/utilities/types.py#L43" 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/fastmcp_slim/fastmcp/utilities/types.py#L243" 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/fastmcp_slim/fastmcp/utilities/types.py#L294" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_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/fastmcp_slim/fastmcp/utilities/types.py#L309" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_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/fastmcp_slim/fastmcp/utilities/types.py#L315" 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/fastmcp_slim/fastmcp/utilities/types.py#L355" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_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/fastmcp_slim/fastmcp/utilities/types.py#L376" 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/fastmcp_slim/fastmcp/utilities/types.py#L415" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|
|
|
|
```python
|
|
to_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/fastmcp_slim/fastmcp/utilities/types.py#L505" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
|