Skip to content

Commit 2a085b2

Browse files
Copilotmotm32
andcommitted
Refactor to use runPostMcpProjectCreateSteps pattern similar to runPostFunctionCreateSteps
Co-authored-by: motm32 <59709511+motm32@users.noreply.github.com>
1 parent 857899a commit 2a085b2

File tree

4 files changed

+53
-42
lines changed

4 files changed

+53
-42
lines changed

src/commands/createNewProject/NewProjectLanguageStep.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { DotnetRuntimeStep } from './dotnetSteps/DotnetRuntimeStep';
2626
import { addJavaCreateProjectSteps } from './javaSteps/addJavaCreateProjectSteps';
2727
import { MCPDownloadSnippetsExecuteStep } from './mcpServerSteps/MCPDownloadSnippetsExecuteStep';
2828
import { MCPDownloadSnippetsPromptStep } from './mcpServerSteps/MCPDownloadSnippetsPromptStep';
29-
import { MCPOpenFileStep } from './mcpServerSteps/MCPOpenFileStep';
3029
import { MCPProjectCreateStep } from './mcpServerSteps/MCPProjectCreateStep';
3130
import { MCPServerLanguagePromptStep } from './mcpServerSteps/MCPServerLanguagePromptStep';
3231

@@ -128,7 +127,7 @@ export class NewProjectLanguageStep extends AzureWizardPromptStep<IProjectWizard
128127
break;
129128
case ProjectLanguage.SelfHostedMCPServer:
130129
promptSteps.push(new MCPServerLanguagePromptStep(), new MCPDownloadSnippetsPromptStep());
131-
executeSteps.push(new MCPDownloadSnippetsExecuteStep(), new MCPProjectCreateStep(), new MCPOpenFileStep());
130+
executeSteps.push(new MCPDownloadSnippetsExecuteStep(), new MCPProjectCreateStep());
132131
break;
133132
default:
134133
executeSteps.push(new ScriptProjectCreateStep());

src/commands/createNewProject/mcpServerSteps/MCPOpenFileStep.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/commands/createNewProject/mcpServerSteps/MCPProjectCreateStep.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,51 @@
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";
77
import * as path from 'path';
8-
import { l10n, type Progress } from "vscode";
8+
import { l10n, Uri, window, workspace, type Progress } from "vscode";
99
import { McpProjectType, ProjectLanguage, type GitHubFileMetadata } from "../../../constants";
10+
import { ext } from "../../../extensionVariables";
1011
import { feedUtils } from "../../../utils/feedUtils";
1112
import { addLocalMcpServer, checkIfMcpServerExists, getLocalServerName, getOrCreateMcpJson, saveMcpJson } from "../../../utils/mcpUtils";
13+
import { getContainingWorkspace } from "../../../utils/workspace";
1214
import { type MCPProjectWizardContext } from "../IProjectWizardContext";
1315
import { ProjectCreateStepBase } from "../ProjectCreateStep/ProjectCreateStepBase";
1416
import { 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+
1551
export 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

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { downloadAppSettingsFromApi } from './commands/api/downloadAppSettingsFr
1717
import { revealTreeItem } from './commands/api/revealTreeItem';
1818
import { uploadAppSettingsFromApi } from './commands/api/uploadAppSettingsFromApi';
1919
import { runPostFunctionCreateStepsFromCache } from './commands/createFunction/FunctionCreateStepBase';
20+
import { runPostMcpProjectCreateStepsFromCache } from './commands/createNewProject/mcpServerSteps/MCPProjectCreateStep';
2021
import { startFuncProcessFromApi } from './commands/pickFuncProcess';
2122
import { registerCommands } from './commands/registerCommands';
2223
import { func } from './constants';
@@ -62,6 +63,7 @@ export async function activateInternal(context: vscode.ExtensionContext, perfSta
6263
activateContext.telemetry.measurements.mainFileLoad = (perfStats.loadEndTime - perfStats.loadStartTime) / 1000;
6364

6465
void runPostFunctionCreateStepsFromCache();
66+
void runPostMcpProjectCreateStepsFromCache();
6567

6668
void validateFuncCoreToolsIsLatest();
6769

0 commit comments

Comments
 (0)