PrefectHQ/fastmcp · error · TypeError

mcp_resource() got unexpected keyword argument(s): {sorted(u

Error message

mcp_resource() got unexpected keyword argument(s): {sorted(unknown)!r}. Valid keyword arguments are: {sorted(_RESOURCE_VALID_KWARGS)}

What it means

mcp_resource() validates its keyword arguments at decoration time against the live parameter set of Resource.from_function (excluding fn and uri). Any keyword not accepted by Resource.from_function triggers this TypeError immediately, giving fail-fast feedback before registration.

Source

Thrown at fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.py:110

    Accepts all parameters supported by ``Resource.from_function``.  Any new
    parameters added to ``Resource.from_function`` are automatically forwarded
    without requiring changes here.

    Args:
        uri: Resource URI (required).
        name: Resource name.  Defaults to the decorated method name.
        enabled: If ``False``, the resource is skipped during registration.
        **kwargs: Additional keyword arguments forwarded verbatim to
            ``Resource.from_function`` (e.g. ``description``, ``tags``,
            ``mime_type``, ``auth``, ``version``, …).

    Raises:
        TypeError: If an unrecognised keyword argument is supplied.  The error
            is raised immediately at decoration time rather than later.
    """
    unknown = set(kwargs) - _RESOURCE_VALID_KWARGS
    if unknown:
        raise TypeError(
            f"mcp_resource() got unexpected keyword argument(s): {sorted(unknown)!r}. "
            f"Valid keyword arguments are: {sorted(_RESOURCE_VALID_KWARGS)}"
        )

    def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
        call_args: dict[str, Any] = {
            "uri": uri,
            "name": name or get_fn_name(func),
            **kwargs,
        }
        if enabled is not None:
            call_args[_MIXIN_ENABLED_KEY] = enabled
        setattr(func, _MCP_REGISTRATION_RESOURCE_ATTR, call_args)
        return func

    return decorator

View on GitHub (pinned to 1f02114297)

Solutions

  1. Use the 'Valid keyword arguments are:' list in the message to correct or drop the offending kwarg(s)
  2. Compare against inspect.signature(Resource.from_function) for your fastmcp version
  3. Fix typos (e.g. mime_tyep -> mime_type)
  4. Remember `uri` is positional and `enabled` is mixin-only; don't pass uri again as a kwarg

Example fix

// before
@mcp_resource("config://app", mime_tyep="application/json")
def get_config(self) -> str: ...

// after
@mcp_resource("config://app", mime_type="application/json")
def get_config(self) -> str: ...
Defensive patterns

Strategy: validation

Validate before calling

import inspect
from fastmcp.resources.base import Resource
valid = set(inspect.signature(Resource.from_function).parameters) - {"fn", "uri"}
bad = set(my_kwargs) - valid
if bad:
    raise TypeError(f"Invalid mcp_resource kwargs: {bad}")

Type guard

def has_valid_resource_kwargs(kwargs: dict) -> bool:
    import inspect
    from fastmcp.resources.base import Resource
    valid = set(inspect.signature(Resource.from_function).parameters) - {"fn", "uri"}
    return set(kwargs) <= valid

Prevention

When it happens

Trigger: Decorating a class method with @mcp_resource(uri, ...) and passing a kwarg Resource.from_function rejects — misspelled options, kwargs removed/renamed in a newer fastmcp, or kwargs valid for mcp_tool but not for resources (e.g. `annotations=` or `timeout=` if the resource signature lacks them).

Common situations: Version upgrades that changed Resource.from_function's parameters; copying kwargs between mcp_tool/mcp_resource/mcp_prompt decorators; typos such as `mime_tyep=`.

Related errors


AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29). Data as JSON: /api/errors/9e2a7ba150a85837. Report an issue: GitHub.