{"record":{"id":"150caf05df9f6d1e","repo":"PrefectHQ/fastmcp","slug":"no-access-token-available-cannot-extract-claim","errorCode":null,"errorMessage":"No access token available. Cannot extract claim '{self.claim_name}'.","messagePattern":"No access token available\\. Cannot extract claim '(.+?)'\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/dependencies.py","lineNumber":1341,"sourceCode":"            return token.claims.get(\"sub\", \"unknown\")\n        ```\n    \"\"\"\n    return cast(AccessToken, _CurrentAccessToken())\n\n\n# --- Token Claim dependency ---\n\n\nclass _TokenClaim(Dependency[str]):\n    \"\"\"Dependency that extracts a specific claim from the access token.\"\"\"\n\n    def __init__(self, claim_name: str):\n        self.claim_name = claim_name\n\n    async def __aenter__(self) -> str:\n        token = get_access_token()\n        if token is None:\n            raise RuntimeError(\n                f\"No access token available. Cannot extract claim '{self.claim_name}'.\"\n            )\n        value = token.claims.get(self.claim_name)\n        if value is None:\n            raise RuntimeError(\n                f\"Claim '{self.claim_name}' not found in access token. \"\n                f\"Available claims: {list(token.claims.keys())}\"\n            )\n        return str(value)\n\n    async def __aexit__(\n        self,\n        exc_type: type[BaseException] | None,\n        exc_value: BaseException | None,\n        traceback: TracebackType | None,\n    ) -> None:\n        pass\n","sourceCodeStart":1323,"sourceCodeEnd":1359,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/dependencies.py#L1323-L1359","documentation":"The Claim dependency extracts a named claim from the current request's access token. Before reading claims it checks get_access_token(); when no token is present it raises this RuntimeError naming the claim it was asked for. It's thrown rather than returning None so callers get an explicit signal that authentication is absent, distinct from the claim simply being missing.","triggerScenarios":"Entering `with Claim(\"sub\") as sub:` on a request without authentication — no auth provider configured, missing/invalid Authorization header, or called outside a request context entirely.","commonSituations":"Local development over stdio (no auth); routes exposed without the auth middleware; unit tests without token fixtures; a client that failed token acquisition silently.","solutions":["Configure authentication on the server and ensure the client sends a valid Bearer token","Make the claim optional: call get_access_token() directly and handle None","Check whether the specific endpoint/transport is exempt from auth when it shouldn't be (or vice versa)","In tests, inject a mock AccessToken containing the claim"],"exampleFix":"// before\nasync def whoami(user_id: Claim) -> str:  # RuntimeError when unauthenticated\n    return user_id\n// after\nfrom fastmcp.server.dependencies import get_access_token\nasync def whoami() -> str:\n    token = get_access_token()\n    return str(token.claims.get(\"sub\")) if token else \"anonymous\"","handlingStrategy":"try-catch","validationCode":"from fastmcp.server.dependencies import get_access_token\ntoken = get_access_token()\nif token is None or claim_name not in token.claims:\n    ...  # handle missing token/claim before using Claim","typeGuard":"from fastmcp.server.dependencies import get_access_token\n\ndef has_claim(claim_name: str) -> bool:\n    token = get_access_token()\n    return token is not None and claim_name in token.claims","tryCatchPattern":"try:\n    async with Claim('sub') as sub:\n        return sub\nexcept RuntimeError:\n    return 'anonymous'","preventionTips":["Check token presence with get_access_token() before Claim for optional claims","Ensure the auth provider includes required claims in the ACCESS token (not just the ID token)","Use Claim only on authenticated transports/routes","Add token-fixture tests for claim-dependent tools"],"tags":["auth","claims","access-token","fastmcp"],"backgroundTag":"missing-access-token","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}