Skip to content

Commit 47e00b0

Browse files
committed
Implementing invalidateCredentials
1 parent b096ad8 commit 47e00b0

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/lib/node-oauth-client-provider.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
OAuthTokensSchema,
88
} from '@modelcontextprotocol/sdk/shared/auth.js'
99
import type { OAuthProviderOptions, StaticOAuthClientMetadata } from './types'
10-
import { readJsonFile, writeJsonFile, readTextFile, writeTextFile } from './mcp-auth-config'
10+
import { readJsonFile, writeJsonFile, readTextFile, writeTextFile, deleteConfigFile } from './mcp-auth-config'
1111
import { StaticOAuthClientInformationFull } from './types'
1212
import { getServerUrlHash, log, debugLog, DEBUG, MCP_REMOTE_VERSION } from './utils'
1313

@@ -193,4 +193,41 @@ export class NodeOAuthClientProvider implements OAuthClientProvider {
193193
if (DEBUG) debugLog('Code verifier found:', !!verifier)
194194
return verifier
195195
}
196+
197+
/**
198+
* Invalidates the specified credentials
199+
* @param scope The scope of credentials to invalidate
200+
*/
201+
async invalidateCredentials(scope: 'all' | 'client' | 'tokens' | 'verifier'): Promise<void> {
202+
if (DEBUG) debugLog(`Invalidating credentials: ${scope}`)
203+
204+
switch (scope) {
205+
case 'all':
206+
await Promise.all([
207+
deleteConfigFile(this.serverUrlHash, 'client_info.json'),
208+
deleteConfigFile(this.serverUrlHash, 'tokens.json'),
209+
deleteConfigFile(this.serverUrlHash, 'code_verifier.txt'),
210+
])
211+
if (DEBUG) debugLog('All credentials invalidated')
212+
break
213+
214+
case 'client':
215+
await deleteConfigFile(this.serverUrlHash, 'client_info.json')
216+
if (DEBUG) debugLog('Client information invalidated')
217+
break
218+
219+
case 'tokens':
220+
await deleteConfigFile(this.serverUrlHash, 'tokens.json')
221+
if (DEBUG) debugLog('OAuth tokens invalidated')
222+
break
223+
224+
case 'verifier':
225+
await deleteConfigFile(this.serverUrlHash, 'code_verifier.txt')
226+
if (DEBUG) debugLog('Code verifier invalidated')
227+
break
228+
229+
default:
230+
throw new Error(`Unknown credential scope: ${scope}`)
231+
}
232+
}
196233
}

src/lib/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js'
33
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
44
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
55
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
6+
import { OAuthError } from '@modelcontextprotocol/sdk/server/auth/errors.js'
67
import { OAuthClientInformationFull, OAuthClientInformationFullSchema } from '@modelcontextprotocol/sdk/shared/auth.js'
78
import { OAuthCallbackServerOptions, StaticOAuthClientInformationFull, StaticOAuthClientMetadata } from './types'
89
import { getConfigDir, getConfigFilePath, readJsonFile } from './mcp-auth-config'
@@ -12,7 +13,6 @@ import crypto from 'crypto'
1213
import fs from 'fs'
1314
import { readFile, rm } from 'fs/promises'
1415
import path from 'path'
15-
import os from 'os'
1616

1717
// Global type declaration for typescript
1818
declare global {
@@ -293,7 +293,7 @@ export async function connectToRemoteServer(
293293
log('Authentication required. Initializing auth...')
294294
if (DEBUG) {
295295
debugLog('Authentication error detected', {
296-
errorType,
296+
errorCode: error instanceof OAuthError ? error.errorCode : undefined,
297297
errorMessage: error.message,
298298
stack: error.stack,
299299
})

0 commit comments

Comments
 (0)