33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import { nonNullProp } from "@microsoft/vscode-azext-utils" ;
6+ import { AzExtFsExtra , callWithTelemetryAndErrorHandling , nonNullProp , type IActionContext } from "@microsoft/vscode-azext-utils" ;
77import * as path from 'path' ;
8- import { l10n , type Progress } from "vscode" ;
8+ import { l10n , Uri , window , workspace , type Progress } from "vscode" ;
99import { McpProjectType , ProjectLanguage , type GitHubFileMetadata } from "../../../constants" ;
10+ import { ext } from "../../../extensionVariables" ;
1011import { feedUtils } from "../../../utils/feedUtils" ;
1112import { addLocalMcpServer , checkIfMcpServerExists , getLocalServerName , getOrCreateMcpJson , saveMcpJson } from "../../../utils/mcpUtils" ;
13+ import { getContainingWorkspace } from "../../../utils/workspace" ;
1214import { type MCPProjectWizardContext } from "../IProjectWizardContext" ;
1315import { ProjectCreateStepBase } from "../ProjectCreateStep/ProjectCreateStepBase" ;
1416import { MCPDownloadSnippetsExecuteStep } from "./MCPDownloadSnippetsExecuteStep" ;
17+
18+ interface ICachedMcpProject {
19+ projectPath : string ;
20+ }
21+
22+ const mcpProjectCacheKey : string = 'azFuncPostMcpProjectCreate' ;
23+
24+ export async function runPostMcpProjectCreateStepsFromCache ( ) : Promise < void > {
25+ const cachedProject : ICachedMcpProject | undefined = ext . context . globalState . get ( mcpProjectCacheKey ) ;
26+ if ( cachedProject ) {
27+ try {
28+ runPostMcpProjectCreateSteps ( cachedProject ) ;
29+ } finally {
30+ await ext . context . globalState . update ( mcpProjectCacheKey , undefined ) ;
31+ }
32+ }
33+ }
34+
35+ function runPostMcpProjectCreateSteps ( project : ICachedMcpProject ) : void {
36+ // Don't wait
37+ void callWithTelemetryAndErrorHandling ( 'postMcpProjectCreate' , async ( context : IActionContext ) => {
38+ context . telemetry . suppressIfSuccessful = true ;
39+
40+ // Open mcp.json file in an editor
41+ if ( getContainingWorkspace ( project . projectPath ) ) {
42+ const mcpJsonFilePath : string = path . join ( project . projectPath , '.vscode' , 'mcp.json' ) ;
43+ if ( await AzExtFsExtra . pathExists ( mcpJsonFilePath ) ) {
44+ const mcpJsonFile = await workspace . openTextDocument ( Uri . file ( mcpJsonFilePath ) ) ;
45+ await window . showTextDocument ( mcpJsonFile , { preview : false } ) ;
46+ }
47+ }
48+ } ) ;
49+ }
50+
1551export class MCPProjectCreateStep extends ProjectCreateStepBase {
1652 public async executeCore ( context : MCPProjectWizardContext , _progress : Progress < { message ?: string | undefined ; increment ?: number | undefined ; } > ) : Promise < void > {
1753 context . mcpProjectType = McpProjectType . SelfHostedMcpServer ;
@@ -36,6 +72,18 @@ export class MCPProjectCreateStep extends ProjectCreateStepBase {
3672 } ) ;
3773 }
3874 }
75+
76+ const cachedProject : ICachedMcpProject = { projectPath : context . projectPath } ;
77+
78+ if ( context . openBehavior ) {
79+ // OpenFolderStep sometimes restarts the extension host, so we will cache this to run on the next extension activation
80+ await ext . context . globalState . update ( mcpProjectCacheKey , cachedProject ) ;
81+ // Delete cached information if the extension host was not restarted after 5 seconds
82+ setTimeout ( ( ) => { void ext . context . globalState . update ( mcpProjectCacheKey , undefined ) ; } , 5 * 1000 ) ;
83+ }
84+
85+ runPostMcpProjectCreateSteps ( cachedProject ) ;
86+
3987 return ;
4088 }
4189
0 commit comments