Skip to main content

fastmcp.utilities.types

Common types used across FastMCP.

Functions

get_fn_name

get_cached_typeadapter

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

Check if cls is a subclass of base, even if cls is a type variable.

is_class_member_of_type

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

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

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

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:

Classes

FastMCPBaseModel

Base model for FastMCP models.

Image

Helper class for returning images from tools. Methods:

to_image_content

Convert to MCP ImageContent.

to_data_uri

Get image as a data URI.

Audio

Helper class for returning audio from tools. Methods:

to_audio_content

File

Helper class for returning file data from tools. Methods:

to_resource_content

ContextSamplingFallbackProtocol