@@ -48,12 +48,7 @@ export class McpOAuthProvider implements OAuthClientProvider {
4848
4949 async clientInformation ( ) : Promise < OAuthClientInformation | undefined > {
5050 // Check config first (pre-registered client)
51- if ( this . config . clientId ) {
52- return {
53- client_id : this . config . clientId ,
54- client_secret : this . config . clientSecret ,
55- }
56- }
51+ if ( this . config . clientId ) return this . configuredClientInformation ( this . config . clientId )
5752
5853 // Check stored client info (from dynamic registration)
5954 // Use getForUrl to validate credentials are for the current server URL
@@ -67,6 +62,7 @@ export class McpOAuthProvider implements OAuthClientProvider {
6762 return {
6863 client_id : entry . clientInfo . clientId ,
6964 client_secret : entry . clientInfo . clientSecret ,
65+ issuer : entry . clientInfo . issuer ,
7066 }
7167 }
7268
@@ -75,13 +71,16 @@ export class McpOAuthProvider implements OAuthClientProvider {
7571 }
7672
7773 async saveClientInformation ( info : OAuthClientInformationFull ) : Promise < void > {
74+ if ( this . config . clientId ) return this . bindConfiguredClient ( this . config . clientId , info )
75+
7876 await McpAuth . updateClientInfo (
7977 this . mcpName ,
8078 {
8179 clientId : info . client_id ,
8280 clientSecret : info . client_secret ,
8381 clientIdIssuedAt : info . client_id_issued_at ,
8482 clientSecretExpiresAt : info . client_secret_expires_at ,
83+ issuer : info . issuer ,
8584 } ,
8685 this . serverUrl ,
8786 )
@@ -91,6 +90,46 @@ export class McpOAuthProvider implements OAuthClientProvider {
9190 } )
9291 }
9392
93+ /**
94+ * Configured credentials, stamped with the authorization server they were first accepted by.
95+ * The SDK refuses to present a stamped client secret to any other authorization server.
96+ */
97+ private async configuredClientInformation ( clientId : string ) : Promise < OAuthClientInformation > {
98+ return {
99+ client_id : clientId ,
100+ client_secret : this . config . clientSecret ,
101+ issuer : ( await this . configuredBinding ( clientId ) ) ?. issuer ,
102+ }
103+ }
104+
105+ /**
106+ * The stored binding of the configured client. It pins the authorization server, not the MCP
107+ * server URL, so it is read regardless of the URL the entry was saved for.
108+ */
109+ private async configuredBinding ( clientId : string ) : Promise < McpAuth . ClientInfo | undefined > {
110+ const clientInfo = ( await McpAuth . get ( this . mcpName ) ) ?. clientInfo
111+ if ( clientInfo ?. clientId !== clientId ) return undefined
112+ return { clientId, issuer : clientInfo . issuer }
113+ }
114+
115+ /**
116+ * Stores only the authorization-server binding of configured credentials; the secret stays
117+ * in config. A different client_id means the SDK registered with another authorization server
118+ * because the configured client is bound elsewhere. Abort instead of starting an authorization
119+ * whose code exchange would be refused.
120+ */
121+ private async bindConfiguredClient ( clientId : string , info : OAuthClientInformationFull ) : Promise < void > {
122+ if ( info . client_id !== clientId ) {
123+ const bound = ( await this . configuredBinding ( clientId ) ) ?. issuer
124+ throw new Error (
125+ `MCP server "${ this . mcpName } " advertises authorization server ${ info . issuer } , but its configured OAuth client ` +
126+ `is bound to ${ bound } . Run \`aictrl mcp logout ${ this . mcpName } \` if the authorization server has moved.` ,
127+ )
128+ }
129+ await McpAuth . updateClientInfo ( this . mcpName , { clientId, issuer : info . issuer } , this . serverUrl )
130+ log . info ( "bound configured client to authorization server" , { mcpName : this . mcpName , issuer : info . issuer } )
131+ }
132+
94133 async tokens ( ) : Promise < OAuthTokens | undefined > {
95134 // Use getForUrl to validate tokens are for the current server URL
96135 const entry = await McpAuth . getForUrl ( this . mcpName , this . serverUrl )
@@ -104,6 +143,7 @@ export class McpOAuthProvider implements OAuthClientProvider {
104143 ? Math . max ( 0 , Math . floor ( entry . tokens . expiresAt - Date . now ( ) / 1000 ) )
105144 : undefined ,
106145 scope : entry . tokens . scope ,
146+ issuer : entry . tokens . issuer ,
107147 }
108148 }
109149
@@ -115,6 +155,7 @@ export class McpOAuthProvider implements OAuthClientProvider {
115155 refreshToken : tokens . refresh_token ,
116156 expiresAt : tokens . expires_in ? Date . now ( ) / 1000 + tokens . expires_in : undefined ,
117157 scope : tokens . scope ,
158+ issuer : tokens . issuer ,
118159 } ,
119160 this . serverUrl ,
120161 )
@@ -157,12 +198,20 @@ export class McpOAuthProvider implements OAuthClientProvider {
157198 return
158199 }
159200
201+ // The binding of configured credentials outlives invalidation: the secret is still in
202+ // config, and dropping the binding would let the next authorization server receive it.
203+ const binding = this . config . clientId ? await this . configuredBinding ( this . config . clientId ) : undefined
160204 switch ( type ) {
161205 case "all" :
206+ if ( binding ) {
207+ await McpAuth . set ( this . mcpName , { clientInfo : binding , serverUrl : entry . serverUrl } )
208+ break
209+ }
162210 await McpAuth . remove ( this . mcpName )
163211 break
164212 case "client" :
165- delete entry . clientInfo
213+ if ( binding ) entry . clientInfo = binding
214+ else delete entry . clientInfo
166215 await McpAuth . set ( this . mcpName , entry )
167216 break
168217 case "tokens" :
0 commit comments