Module fastmcp_extensions.registration

MCP capability registration utilities.

This module provides functions to register tools, prompts, and resources with a FastMCP app, filtered by mcp_module.

Functions

def register_mcp_prompts(app: FastMCP, mcp_module: str | None = None) ‑> None
Expand source code
def register_mcp_prompts(
    app: FastMCP,
    mcp_module: str | None = None,
) -> None:
    """Register prompt callables with the FastMCP app, filtered by mcp_module.

    Args:
        app: The FastMCP app instance
        mcp_module: The mcp_module to register for. If not provided, automatically
            derived from the caller's file stem.
    """
    if mcp_module is None:
        mcp_module = _get_caller_file_stem()

    def _register_fn(
        app: FastMCP,
        callable_fn: Callable[..., Any],
        annotations: dict[str, Any],
    ) -> None:
        app.prompt(
            name=annotations["name"],
            description=annotations["description"],
        )(callable_fn)

    _register_mcp_callables(
        app=app,
        mcp_module=mcp_module,
        resource_list=_REGISTERED_PROMPTS,
        register_fn=_register_fn,
    )

Register prompt callables with the FastMCP app, filtered by mcp_module.

Args

app
The FastMCP app instance
mcp_module
The mcp_module to register for. If not provided, automatically derived from the caller's file stem.
def register_mcp_resources(app: FastMCP, mcp_module: str | None = None) ‑> None
Expand source code
def register_mcp_resources(
    app: FastMCP,
    mcp_module: str | None = None,
) -> None:
    """Register resource callables with the FastMCP app, filtered by mcp_module.

    Args:
        app: The FastMCP app instance
        mcp_module: The mcp_module to register for. If not provided, automatically
            derived from the caller's file stem.
    """
    if mcp_module is None:
        mcp_module = _get_caller_file_stem()

    def _register_fn(
        app: FastMCP,
        callable_fn: Callable[..., Any],
        annotations: dict[str, Any],
    ) -> None:
        app.resource(
            annotations["uri"],
            description=annotations["description"],
            mime_type=annotations["mime_type"],
        )(callable_fn)

    _register_mcp_callables(
        app=app,
        mcp_module=mcp_module,
        resource_list=_REGISTERED_RESOURCES,
        register_fn=_register_fn,
    )

Register resource callables with the FastMCP app, filtered by mcp_module.

Args

app
The FastMCP app instance
mcp_module
The mcp_module to register for. If not provided, automatically derived from the caller's file stem.
def register_mcp_tools(app: FastMCP,
mcp_module: str | None = None,
*,
exclude_args: list[str] | None = None) ‑> None
Expand source code
def register_mcp_tools(
    app: FastMCP,
    mcp_module: str | None = None,
    *,
    exclude_args: list[str] | None = None,
) -> None:
    """Register tools with the FastMCP app, filtered by mcp_module.

    Args:
        app: The FastMCP app instance
        mcp_module: The mcp_module to register for. If not provided, automatically
            derived from the caller's file stem.
        exclude_args: Optional list of argument names to exclude from tool schema.
            This is useful for arguments that are injected by middleware.
    """
    if mcp_module is None:
        mcp_module = _get_caller_file_stem()

    def _register_fn(
        app: FastMCP,
        callable_fn: Callable[..., Any],
        annotations: dict[str, Any],
    ) -> None:
        tool_exclude_args: list[str] | None = None
        if exclude_args:
            params = set(inspect.signature(callable_fn).parameters.keys())
            excluded = [name for name in exclude_args if name in params]
            tool_exclude_args = excluded if excluded else None

        app.tool(
            callable_fn,
            annotations=annotations,
            exclude_args=tool_exclude_args,
        )

    _register_mcp_callables(
        app=app,
        mcp_module=mcp_module,
        resource_list=_REGISTERED_TOOLS,
        register_fn=_register_fn,
    )

Register tools with the FastMCP app, filtered by mcp_module.

Args

app
The FastMCP app instance
mcp_module
The mcp_module to register for. If not provided, automatically derived from the caller's file stem.
exclude_args
Optional list of argument names to exclude from tool schema. This is useful for arguments that are injected by middleware.

Classes

class PromptDef (name: str, description: str, func: Callable[..., list[dict[str, str]]])
Expand source code
@dataclass
class PromptDef:
    """Definition of a deferred MCP prompt."""

    name: str
    description: str
    func: Callable[..., list[dict[str, str]]]

Definition of a deferred MCP prompt.

Instance variables

var description : str
var func : Callable[..., list[dict[str, str]]]
var name : str
class ResourceDef (uri: str, description: str, mime_type: str, func: Callable[..., Any])
Expand source code
@dataclass
class ResourceDef:
    """Definition of a deferred MCP resource."""

    uri: str
    description: str
    mime_type: str
    func: Callable[..., Any]

Definition of a deferred MCP resource.

Instance variables

var description : str
var func : Callable[..., typing.Any]
var mime_type : str
var uri : str