{"record":{"id":"a5e78fd6a8b2b097","repo":"PrefectHQ/fastmcp","slug":"expected-fastmcp-server-auth-auth-accesstoken-got","errorCode":null,"errorMessage":"Expected fastmcp.server.auth.auth.AccessToken, got {type(access_token).__name__}. Ensure the SDK is using the correct AccessToken type.","messagePattern":"Expected fastmcp\\.server\\.auth\\.auth\\.AccessToken, got (.+?)\\. Ensure the SDK is using the correct AccessToken type\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/dependencies.py","lineNumber":655,"sourceCode":"\n    # If the object is not a FastMCP AccessToken, convert it to one if the\n    # fields are compatible (e.g. `claims` is not present in the SDK's AccessToken).\n    # This is a workaround for the case where the SDK or auth provider returns a different type\n    # If it fails, it will raise a TypeError\n    try:\n        access_token_as_dict = access_token.model_dump()\n        return AccessToken(\n            token=access_token_as_dict[\"token\"],\n            client_id=access_token_as_dict[\"client_id\"],\n            scopes=access_token_as_dict[\"scopes\"],\n            # Optional fields\n            expires_at=access_token_as_dict.get(\"expires_at\"),\n            resource=access_token_as_dict.get(\"resource\"),\n            subject=access_token_as_dict.get(\"subject\"),\n            claims=access_token_as_dict.get(\"claims\") or {},\n        )\n    except Exception as e:\n        raise TypeError(\n            f\"Expected fastmcp.server.auth.auth.AccessToken, got {type(access_token).__name__}. \"\n            \"Ensure the SDK is using the correct AccessToken type.\"\n        ) from e\n\n\n# --- Schema generation helper ---\n\n\n@lru_cache(maxsize=5000)\ndef without_injected_parameters(\n    fn: Callable[..., Any], *, run_in_thread: bool = True\n) -> Callable[..., Any]:\n    \"\"\"Create a wrapper function without injected parameters.\n\n    Returns a wrapper that excludes Context and Docket dependency parameters,\n    making it safe to use with Pydantic TypeAdapter for schema generation and\n    validation. The wrapper internally handles all dependency resolution and\n    Context injection when called.","sourceCodeStart":637,"sourceCodeEnd":673,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/dependencies.py#L637-L673","documentation":"get_access_token() converts the access token found in the request context into fastmcp's own AccessToken type. This TypeError is thrown when conversion fails because the object in context is not the expected AccessToken shape — typically an AccessToken class imported from the mcp SDK rather than fastmcp.server.auth.auth, whose constructor fields differ. The message explicitly tells you which type was found.","triggerScenarios":"Calling get_access_token() when middleware/auth code stored an mcp.server.auth.types.AccessToken (or other token type) in the request context instead of fastmcp's AccessToken; custom OAuth proxies passing raw SDK tokens.","commonSituations":"Copy-pasting OAuth examples from the MCP SDK docs into FastMCP auth providers; upgrading fastmcp where the token type was unified; custom TokenVerifier returning the SDK token type.","solutions":["Construct fastmcp.server.auth.auth.AccessToken (token, client_id, expires_at, resource, subject, claims) from the SDK token before storing it in context.","In custom TokenVerifier implementations, return the FastMCP AccessToken type, not the mcp SDK type.","Update the auth integration so it uses FastMCP's token model; the wrapper is a thin shim around the same fields."],"exampleFix":"// before\nfrom mcp.server.auth.types import AccessToken\nreturn AccessToken(token=raw, client_id=\"x\")\n// after\nfrom fastmcp.server.auth.auth import AccessToken\nreturn AccessToken(token=raw, client_id=\"x\", scopes=[], expires_at=None, resource=None, subject=None, claims={})","handlingStrategy":"type-guard","validationCode":"from fastmcp.server.auth.auth import AccessToken\nassert isinstance(token, AccessToken), f\"wrong token type: {type(token).__name__}\"","typeGuard":"from fastmcp.server.auth.auth import AccessToken\ndef is_fastmcp_token(t) -> bool:\n    return isinstance(t, AccessToken)","tryCatchPattern":"try:\n    token = get_access_token()\nexcept TypeError as e:\n    if \"Expected fastmcp.server.auth.auth.AccessToken\" in str(e):\n        token = None  # fall back to unauthenticated handling\n    else:\n        raise","preventionTips":["Import AccessToken from fastmcp.server.auth.auth, never mcp.server.auth.types","Have custom TokenVerifiers return FastMCP's AccessToken","Check auth middleware code when upgrading fastmcp versions"],"tags":["auth","type-mismatch","oauth"],"backgroundTag":"access-token-type-mismatch","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}