@@ -64,8 +64,11 @@ func main() {
6464				Value :    "" , // Empty means auto-generate persistent ID 
6565			},
6666			& cli.BoolFlag {
67- 				Name :  "analytics-off" , // Disable analytics completely 
68- 				Usage : "Disable Google Analytics tracking" ,
67+ 				Name :     "analytics-off" , // Disable analytics completely 
68+ 				Required : false ,
69+ 				Sources :  cli .EnvVars ("RP_MCP_ANALYTICS_OFF" ),
70+ 				Usage :    "Disable Google Analytics tracking" ,
71+ 				Value :    false ,
6972			},
7073		},
7174		Commands : []* cli.Command {
@@ -118,7 +121,7 @@ func initLogger() func(ctx context.Context, command *cli.Command) (context.Conte
118121	}
119122}
120123
121- func  newMCPServer (cmd  * cli.Command ) (* server.MCPServer , error ) {
124+ func  newMCPServer (cmd  * cli.Command ) (* server.MCPServer , * mcpreportportal. Analytics ,  error ) {
122125	// Retrieve required parameters from the command flags 
123126	token  :=  cmd .String ("token" )                           // API token 
124127	host  :=  cmd .String ("host" )                             // ReportPortal host URL 
@@ -129,28 +132,28 @@ func newMCPServer(cmd *cli.Command) (*server.MCPServer, error) {
129132
130133	hostUrl , err  :=  url .Parse (host )
131134	if  err  !=  nil  {
132- 		return  nil , fmt .Errorf ("invalid host URL: %w" , err )
135+ 		return  nil , nil ,  fmt .Errorf ("invalid host URL: %w" , err )
133136	}
134137
135138	// Create a new stdio server using the ReportPortal client 
136- 	mcpServer , err  :=  mcpreportportal .NewServer (
139+ 	mcpServer , analytics ,  err  :=  mcpreportportal .NewServer (
137140		version ,
138141		hostUrl ,
139142		token ,
140143		project ,
141144		userID ,
142145		analyticsAPISecret ,
143- 		analyticsOff ,
146+ 		! analyticsOff ,  // Convert analyticsOff to analyticsOn 
144147	)
145148	if  err  !=  nil  {
146- 		return  nil , fmt .Errorf ("failed to create ReportPortal MCP server: %w" , err )
149+ 		return  nil , nil ,  fmt .Errorf ("failed to create ReportPortal MCP server: %w" , err )
147150	}
148- 	return  mcpServer , nil 
151+ 	return  mcpServer , analytics ,  nil 
149152}
150153
151154// runStdioServer starts the ReportPortal MCP server in stdio mode. 
152155func  runStdioServer (ctx  context.Context , cmd  * cli.Command ) error  {
153- 	mcpServer , err  :=  newMCPServer (cmd )
156+ 	mcpServer , analytics ,  err  :=  newMCPServer (cmd )
154157	if  err  !=  nil  {
155158		return  fmt .Errorf ("failed to create ReportPortal MCP server: %w" , err )
156159	}
@@ -169,9 +172,11 @@ func runStdioServer(ctx context.Context, cmd *cli.Command) error {
169172	// Wait for a shutdown signal or an error from the server 
170173	select  {
171174	case  <- ctx .Done (): // Context canceled (e.g., SIGTERM received) 
172- 		slog .Error ("shutting down server..." )
175+ 		slog .Info ("shutting down server..." )
176+ 		mcpreportportal .StopAnalytics (analytics , "" )
173177	case  err  :=  <- errC : // Error occurred while running the server 
174178		if  err  !=  nil  {
179+ 			mcpreportportal .StopAnalytics (analytics , "server error" )
175180			return  fmt .Errorf ("error running server: %w" , err )
176181		}
177182	}
@@ -181,7 +186,7 @@ func runStdioServer(ctx context.Context, cmd *cli.Command) error {
181186
182187// runStreamingServer starts the ReportPortal MCP server in streaming mode. 
183188func  runStreamingServer (ctx  context.Context , cmd  * cli.Command ) error  {
184- 	mcpServer , err  :=  newMCPServer (cmd )
189+ 	mcpServer , analytics ,  err  :=  newMCPServer (cmd )
185190	if  err  !=  nil  {
186191		return  fmt .Errorf ("failed to create ReportPortal MCP server: %w" , err )
187192	}
@@ -200,14 +205,16 @@ func runStreamingServer(ctx context.Context, cmd *cli.Command) error {
200205	// Wait for a shutdown signal or an error from the server 
201206	select  {
202207	case  <- ctx .Done (): // Context canceled (e.g., SIGTERM received) 
203- 		slog .Error ("shutting down server..." )
208+ 		slog .Info ("shutting down server..." )
209+ 		mcpreportportal .StopAnalytics (analytics , "" )
204210		sCtx , cancel  :=  context .WithTimeout (context .Background (), 5 * time .Second )
205211		defer  cancel ()
206212		if  err  :=  streamingServer .Shutdown (sCtx ); err  !=  nil  {
207213			slog .Error ("error during server shutdown" , "error" , err )
208214		}
209215	case  err  :=  <- errC : // Error occurred while running the server 
210216		if  err  !=  nil  {
217+ 			mcpreportportal .StopAnalytics (analytics , "server error" )
211218			return  fmt .Errorf ("error running server: %w" , err )
212219		}
213220	}
0 commit comments