[PR #6424] feat:(mcp)handle the deletion of invalid and rotated tokens #6656

Open
opened 2026-03-13 13:06:59 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6424
Author: @Ridhim-RR
Created: 11/30/2025
Status: 🔄 Open

Base: canaryHead: feat-mcp-token-deletion


📝 Commits (4)

  • a513fd8 feat:(mcp)handle the deletion of invalid and rotated tokens
  • e509228 chore(type): type issue resolved
  • 49a7c30 chore(lint): linting issue resolved
  • c6b68c2 Merge branch 'canary' into feat-mcp-token-deletion

📊 Changes

2 files changed (+19 additions, -4 deletions)

View changed files

📝 packages/better-auth/src/db/internal-adapter.ts (+1 -4)
📝 packages/better-auth/src/plugins/mcp/index.ts (+18 -0)

📄 Description

Closes #6423

Fix: Remove Expired and Rotated OAuth Tokens in MCP Refresh Flow

This PR fixes an issue in the mcp/token endpoint where expired or rotated OAuth tokens were not being deleted from the database.

What was happening

  • When a refresh token was expired, the code returned an invalid_grant error but never deleted the expired token.
  • When a new access token + refresh token pair was created, the previous token record was left behind, causing unnecessary token accumulation and potential security concerns.

Changes:

1.Delete expired refresh tokens

if (token.refreshTokenExpiresAt < new Date()) {
							await ctx.context.adapter.delete({
								model: "oauthAccessToken",
								where: [
									{
										field: "refreshToken",
										value: refresh_token.toString(),
									},
								],
							});
							throw new APIError("UNAUTHORIZED", {
								error_description: "refresh token expired",
								error: "invalid_grant",
							});
						}

2. After generating a new token pair, delete the previous token

await ctx.context.adapter.create({
    model: modelName.oauthAccessToken,
    data: {
        accessToken,
        refreshToken: newRefreshToken,
        accessTokenExpiresAt,
        refreshTokenExpiresAt,
        clientId: client_id.toString(),
        userId: token.userId,
        scopes: token.scopes,
        createdAt: new Date(),
        updatedAt: new Date(),
    },
});

await ctx.context.adapter.delete({
    model: "oauthAccessToken",
    where: [
        {
            field: "refreshToken",
            value: refresh_token.toString(),
        },
    ],
});

Summary by cubic

Delete expired and rotated OAuth tokens in the MCP refresh flow to prevent token buildup and reduce security risk. The mcp/token endpoint now cleans up invalid tokens during refresh.

  • Bug Fixes
    • Delete expired refresh tokens before returning invalid_grant.
    • Remove the previous token record after issuing a new access/refresh pair.

Written for commit c6b68c28ba. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/6424 **Author:** [@Ridhim-RR](https://github.com/Ridhim-RR) **Created:** 11/30/2025 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `feat-mcp-token-deletion` --- ### 📝 Commits (4) - [`a513fd8`](https://github.com/better-auth/better-auth/commit/a513fd89d93538f46f3ae75b0b3481e4217fc604) feat:(mcp)handle the deletion of invalid and rotated tokens - [`e509228`](https://github.com/better-auth/better-auth/commit/e509228e0eaf6de3a3cd36301f865a4bd0ac28db) chore(type): type issue resolved - [`49a7c30`](https://github.com/better-auth/better-auth/commit/49a7c300f70af022c3e5078d070ebf8125118621) chore(lint): linting issue resolved - [`c6b68c2`](https://github.com/better-auth/better-auth/commit/c6b68c28ba2805df660e780f68ec1a5d7e9fa087) Merge branch 'canary' into feat-mcp-token-deletion ### 📊 Changes **2 files changed** (+19 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/internal-adapter.ts` (+1 -4) 📝 `packages/better-auth/src/plugins/mcp/index.ts` (+18 -0) </details> ### 📄 Description Closes #6423 **Fix: Remove Expired and Rotated OAuth Tokens in MCP Refresh Flow** This PR fixes an issue in the mcp/token endpoint where expired or rotated OAuth tokens were not being deleted from the database. **What was happening** - When a refresh token was expired, the code returned an invalid_grant error but never deleted the expired token. - When a new access token + refresh token pair was created, the previous token record was left behind, causing unnecessary token accumulation and potential security concerns. **Changes:** **1.Delete expired refresh tokens** ``` if (token.refreshTokenExpiresAt < new Date()) { await ctx.context.adapter.delete({ model: "oauthAccessToken", where: [ { field: "refreshToken", value: refresh_token.toString(), }, ], }); throw new APIError("UNAUTHORIZED", { error_description: "refresh token expired", error: "invalid_grant", }); } ``` **2. After generating a new token pair, delete the previous token** ``` await ctx.context.adapter.create({ model: modelName.oauthAccessToken, data: { accessToken, refreshToken: newRefreshToken, accessTokenExpiresAt, refreshTokenExpiresAt, clientId: client_id.toString(), userId: token.userId, scopes: token.scopes, createdAt: new Date(), updatedAt: new Date(), }, }); await ctx.context.adapter.delete({ model: "oauthAccessToken", where: [ { field: "refreshToken", value: refresh_token.toString(), }, ], }); ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Delete expired and rotated OAuth tokens in the MCP refresh flow to prevent token buildup and reduce security risk. The mcp/token endpoint now cleans up invalid tokens during refresh. - **Bug Fixes** - Delete expired refresh tokens before returning invalid_grant. - Remove the previous token record after issuing a new access/refresh pair. <sup>Written for commit c6b68c28ba2805df660e780f68ec1a5d7e9fa087. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:06:59 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#6656