@@ -1238,6 +1238,44 @@ private static Command CreateRegisterExternalMcpServerSubcommand(
12381238 } ;
12391239 }
12401240
1241+ // Create Copilot (VS Code) Entra app — same pattern as A365 Proxy
1242+ string ? copilotAppClientId = null ;
1243+ string ? copilotAppObjectId = null ;
1244+
1245+ if ( force )
1246+ {
1247+ var existingCopilotObjectId = await graphApiService . GetAppObjectIdByDisplayNameAsync ( tenantId , $ "{ serverName } -Copilot") ;
1248+ if ( ! string . IsNullOrWhiteSpace ( existingCopilotObjectId ) )
1249+ {
1250+ logger . LogDebug ( "Deleting existing Copilot app: {ObjectId}" , existingCopilotObjectId ) ;
1251+ await graphApiService . DeleteEntraAppAsync ( tenantId , existingCopilotObjectId ) ;
1252+ }
1253+ }
1254+
1255+ logger . LogDebug ( "Creating Entra application for Copilot (VS Code)..." ) ;
1256+ var copilotApp = await graphApiService . CreateEntraAppAsync ( tenantId , $ "{ serverName } -Copilot", serviceTreeId : serviceTreeId ) ;
1257+ if ( copilotApp != null )
1258+ {
1259+ copilotAppClientId = copilotApp . Value . ClientId ;
1260+ copilotAppObjectId = copilotApp . Value . ObjectId ;
1261+ logger . LogDebug ( "Created Copilot app: {ClientId}" , copilotAppClientId ) ;
1262+
1263+ var copilotRedirectUri = $ "ms-appx-web://MicrosoftAAD.BrokerPlugin/{ copilotAppClientId } ";
1264+ try
1265+ {
1266+ await graphApiService . UpdateAppRedirectUrisAsync ( tenantId , copilotAppObjectId , new [ ] { copilotRedirectUri } ) ;
1267+ logger . LogDebug ( "Set Copilot redirect URI: {Uri}" , copilotRedirectUri ) ;
1268+ }
1269+ catch ( Exception ex )
1270+ {
1271+ logger . LogWarning ( "Failed to set redirect URI on Copilot app: {Error}" , ex . Message ) ;
1272+ }
1273+ }
1274+ else
1275+ {
1276+ logger . LogWarning ( "Failed to create Copilot Entra app. Continuing without it." ) ;
1277+ }
1278+
12411279 // Track warnings for non-fatal failures during registration
12421280 var warnings = new List < string > ( ) ;
12431281
@@ -1265,6 +1303,7 @@ private static Command CreateRegisterExternalMcpServerSubcommand(
12651303 RemoteServerScopes = remoteScopes ,
12661304 PublisherName = publisherName ,
12671305 Description = serverDescription ,
1306+ CopilotClientAppId = copilotAppClientId ,
12681307 Force = force ,
12691308 } ;
12701309
@@ -1427,6 +1466,23 @@ await graphApiService.AddRequiredResourceAccessAsync(
14271466 logger . LogWarning ( msg ) ;
14281467 warnings . Add ( msg ) ;
14291468 }
1469+
1470+ // Add same PPMI API permissions on Copilot app
1471+ if ( ppmiScopeIds . Count > 0 && copilotAppObjectId != null )
1472+ {
1473+ try
1474+ {
1475+ logger . LogDebug ( "Adding API permissions on Copilot app for PPMI scopes..." ) ;
1476+ await graphApiService . AddRequiredResourceAccessAsync (
1477+ tenantId , copilotAppObjectId , ppmiAppClientId , ppmiScopeIds ) ;
1478+ }
1479+ catch ( Exception ex )
1480+ {
1481+ var msg = $ "Failed to add API permissions on Copilot app: { ex . Message } ";
1482+ logger . LogWarning ( msg ) ;
1483+ warnings . Add ( msg ) ;
1484+ }
1485+ }
14301486 }
14311487 else
14321488 {
0 commit comments