@@ -376,6 +376,26 @@ async def _get_user_credential(self, jwt_token: str) -> str | None:
376376 if not application_id :
377377 return None
378378
379+ # Query for existing shared "OAuth MCP" credential first
380+ cred_list_resp = await client .get (
381+ f"{ settings .platform_base_url } /api/v1/credentials/" ,
382+ params = {"application_id" : application_id , "name" : "OAuth MCP" },
383+ headers = headers ,
384+ )
385+ existing_creds = []
386+ if cred_list_resp .status_code == 200 :
387+ cred_data = cred_list_resp .json ()
388+ existing_creds = cred_data .get ("items" , cred_data .get ("results" , []))
389+
390+ # Reuse existing credential if found
391+ if existing_creds and isinstance (existing_creds , list ):
392+ data = existing_creds [0 ]
393+ token = data .get ("token" ) if isinstance (data , dict ) else None
394+ if isinstance (token , str ) and token :
395+ logger .info (f"Reusing existing OAuth MCP Credential (id={ data .get ('id' )} )" )
396+ return token
397+
398+ # Create new credential only if not found
379399 cred_resp = await client .post (
380400 f"{ settings .platform_base_url } /api/v1/credentials/" ,
381401 headers = {** headers , "Content-Type" : "application/json" },
@@ -386,16 +406,16 @@ async def _get_user_credential(self, jwt_token: str) -> str | None:
386406 )
387407 if cred_resp .status_code not in (200 , 201 ):
388408 logger .error (
389- "Failed to get managed MCP Credential: "
409+ "Failed to create OAuth MCP Credential: "
390410 f"{ cred_resp .status_code } { cred_resp .text [:500 ]} "
391411 )
392412 return None
393413 data = cred_resp .json ()
394414 token = data .get ("token" ) if isinstance (data , dict ) else None
395415 if isinstance (token , str ) and token :
396- logger .info (f"Using shared OAuth MCP Credential (id={ data .get ('id' )} )" )
416+ logger .info (f"Created new OAuth MCP Credential (id={ data .get ('id' )} )" )
397417 return token
398- logger .error ("Managed MCP Credential response did not contain a token" )
418+ logger .error ("OAuth MCP Credential response did not contain a token" )
399419 except Exception :
400420 logger .exception ("Managed MCP Credential provisioning failed" )
401421 return None
0 commit comments