@@ -10,6 +10,8 @@ import {
1010 UserError ,
1111} from "@microsoft/teamsfx-api" ;
1212import { getSystemInputs } from "../utils/systemEnvUtils" ;
13+ import { ExtTelemetry } from "../telemetry/extTelemetry" ;
14+ import { TelemetryEvent } from "../telemetry/extTelemetryEvents" ;
1315import path from "path" ;
1416import * as fs from "fs-extra" ;
1517import { QuestionNames } from "@microsoft/teamsfx-core" ;
@@ -20,6 +22,7 @@ import { VS_CODE_UI } from "../qm/vsc_ui";
2022import * as parser from "jsonc-parser" ;
2123import { getDefaultString , localize } from "../utils/localizeUtils" ;
2224import { ExtensionErrors } from "../error/error" ;
25+ import { getTriggerFromProperty } from "../utils/telemetryUtils" ;
2326
2427function sanitizeMCPName ( name : string ) : string {
2528 // Replace special characters except "-" with "_", but if two special characters are adjacent,
@@ -31,6 +34,10 @@ function sanitizeMCPName(name: string): string {
3134}
3235
3336export async function updateActionWithMCP ( args ?: any [ ] ) : Promise < Result < any , FxError > > {
37+ ExtTelemetry . sendTelemetryEvent (
38+ TelemetryEvent . UpdateActionWithMCPStart ,
39+ getTriggerFromProperty ( args && args . length > 1 ? [ args [ 1 ] ] : undefined )
40+ ) ;
3441 const inputs = getSystemInputs ( ) ;
3542 let mcpName = args && args . length > 0 ? args [ 0 ] . serverName : undefined ;
3643 let server = args && args . length > 0 ? args [ 0 ] . serverConfig ?. url : undefined ;
@@ -44,42 +51,42 @@ export async function updateActionWithMCP(args?: any[]): Promise<Result<any, FxE
4451 const mcpFile = path . join ( inputs . projectPath ! , ".vscode" , "mcp.json" ) ;
4552 if ( ! fs . pathExistsSync ( mcpFile ) ) {
4653 void vscode . window . showErrorMessage ( localize ( "teamstoolkit.MCP.FileNotFound" ) ) ;
47- return err (
48- new UserError (
49- "da-mcp" ,
50- ExtensionErrors . MCPFileNotFound ,
51- getDefaultString ( "teamstoolkit.MCP.FileNotFound" ) ,
52- localize ( "teamstoolkit.MCP.FileNotFound" )
53- )
54+ const error = new UserError (
55+ "da-mcp" ,
56+ ExtensionErrors . MCPFileNotFound ,
57+ getDefaultString ( "teamstoolkit.MCP.FileNotFound" ) ,
58+ localize ( "teamstoolkit.MCP.FileNotFound" )
5459 ) ;
60+ ExtTelemetry . sendTelemetryErrorEvent ( TelemetryEvent . UpdateActionWithMCP , error ) ;
61+ return err ( error ) ;
5562 }
5663 // const mcpContent = await fs.readJSON(mcpFile);
5764 const mcpOriginalContent = fs . readFileSync ( mcpFile , "utf-8" ) ;
5865 const mcpContent = parser . parse ( mcpOriginalContent ) ;
5966 if ( ! mcpContent || ! mcpContent . servers ) {
6067 void vscode . window . showErrorMessage ( localize ( "teamstoolkit.MCP.ContentInvalid" ) ) ;
61- return err (
62- new UserError (
63- "da-mcp" ,
64- ExtensionErrors . MCPContentInvalid ,
65- getDefaultString ( "teamstoolkit.MCP.ContentInvalid" ) ,
66- localize ( "teamstoolkit.MCP.ContentInvalid" )
67- )
68+ const error = new UserError (
69+ "da-mcp" ,
70+ ExtensionErrors . MCPContentInvalid ,
71+ getDefaultString ( "teamstoolkit.MCP.ContentInvalid" ) ,
72+ localize ( "teamstoolkit.MCP.ContentInvalid" )
6873 ) ;
74+ ExtTelemetry . sendTelemetryErrorEvent ( TelemetryEvent . UpdateActionWithMCP , error ) ;
75+ return err ( error ) ;
6976 }
7077
7178 // TODO: support multiple MCP servers
7279 const mcpNames = Object . keys ( mcpContent . servers ) ;
7380 if ( mcpNames . length === 0 ) {
7481 void vscode . window . showErrorMessage ( localize ( "teamstoolkit.MCP.ServerNotFound" ) ) ;
75- return err (
76- new UserError (
77- "da-mcp" ,
78- ExtensionErrors . MCPServerNotFound ,
79- getDefaultString ( "teamstoolkit.MCP.ServerNotFound" ) ,
80- localize ( "teamstoolkit.MCP.ServerNotFound" )
81- )
82+ const error = new UserError (
83+ "da-mcp" ,
84+ ExtensionErrors . MCPServerNotFound ,
85+ getDefaultString ( "teamstoolkit.MCP.ServerNotFound" ) ,
86+ localize ( "teamstoolkit.MCP.ServerNotFound" )
8287 ) ;
88+ ExtTelemetry . sendTelemetryErrorEvent ( TelemetryEvent . UpdateActionWithMCP , error ) ;
89+ return err ( error ) ;
8390 }
8491 if ( mcpNames . length === 1 ) {
8592 mcpName = sanitizeMCPName ( mcpNames [ 0 ] ) ;
@@ -99,6 +106,7 @@ export async function updateActionWithMCP(args?: any[]): Promise<Result<any, FxE
99106 void vscode . window . showErrorMessage (
100107 result . error . message || localize ( "teamstoolkit.MCP.SelectServerFailed" )
101108 ) ;
109+ ExtTelemetry . sendTelemetryErrorEvent ( TelemetryEvent . UpdateActionWithMCP , result . error ) ;
102110 return err ( result . error ) ;
103111 }
104112 const originalMcpName = result . value . result as string ;
@@ -107,14 +115,14 @@ export async function updateActionWithMCP(args?: any[]): Promise<Result<any, FxE
107115 }
108116 } else if ( ! mcpName || ! server ) {
109117 void vscode . window . showErrorMessage ( localize ( "teamstoolkit.MCP.NameOrServerUrlMissing" ) ) ;
110- return err (
111- new UserError (
112- "da-mcp" ,
113- ExtensionErrors . MCPNameOrServerUrlMissing ,
114- getDefaultString ( "teamstoolkit.MCP.NameOrServerUrlMissing" ) ,
115- localize ( "teamstoolkit.MCP.NameOrServerUrlMissing" )
116- )
118+ const error = new UserError (
119+ "da-mcp" ,
120+ ExtensionErrors . MCPNameOrServerUrlMissing ,
121+ getDefaultString ( "teamstoolkit.MCP.NameOrServerUrlMissing" ) ,
122+ localize ( "teamstoolkit.MCP.NameOrServerUrlMissing" )
117123 ) ;
124+ ExtTelemetry . sendTelemetryErrorEvent ( TelemetryEvent . UpdateActionWithMCP , error ) ;
125+ return err ( error ) ;
118126 }
119127
120128 inputs [ QuestionNames . MCPForDAServerUrl ] = server ;
@@ -136,15 +144,14 @@ export async function updateActionWithMCP(args?: any[]): Promise<Result<any, FxE
136144 } ) ;
137145 if ( tools . length === 0 ) {
138146 void vscode . window . showErrorMessage ( localize ( "teamstoolkit.MCP.ToolsNotFound" ) ) ;
139- // Return an error result
140- return err (
141- new UserError (
142- "da-mcp" ,
143- ExtensionErrors . MCPToolsNotFound ,
144- getDefaultString ( "teamstoolkit.MCP.ToolsNotFound" ) ,
145- localize ( "teamstoolkit.MCP.ToolsNotFound" )
146- )
147+ const error = new UserError (
148+ "da-mcp" ,
149+ ExtensionErrors . MCPToolsNotFound ,
150+ getDefaultString ( "teamstoolkit.MCP.ToolsNotFound" ) ,
151+ localize ( "teamstoolkit.MCP.ToolsNotFound" )
147152 ) ;
153+ ExtTelemetry . sendTelemetryErrorEvent ( TelemetryEvent . UpdateActionWithMCP , error ) ;
154+ return err ( error ) ;
148155 }
149156 inputs [ QuestionNames . MCPForDAAvailableTools ] = tools ;
150157
@@ -178,5 +185,16 @@ export async function updateActionWithMCP(args?: any[]): Promise<Result<any, FxE
178185 inputs [ QuestionNames . MCPForDAAuth ] = auth ;
179186 inputs [ QuestionNames . MCPForDAAuthMetadataUrl ] = oauthMetadataUrl ;
180187 const result = await runCommand ( Stage . updateActionWithMCP , inputs ) ;
188+ if ( result . isErr ( ) ) {
189+ ExtTelemetry . sendTelemetryErrorEvent ( TelemetryEvent . UpdateActionWithMCP , result . error , {
190+ "auth-type" : auth ,
191+ "tool-number" : tools . length . toString ( ) ,
192+ } ) ;
193+ } else {
194+ ExtTelemetry . sendTelemetryEvent ( TelemetryEvent . UpdateActionWithMCP , {
195+ "auth-type" : auth ,
196+ "tool-number" : tools . length . toString ( ) ,
197+ } ) ;
198+ }
181199 return result ;
182200}
0 commit comments