From 054c984199a2ec701c1b49f321169abbecd122e9 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Fri, 17 Oct 2025 17:22:58 -0400 Subject: [PATCH 1/3] Investigation into setting IsServerCommandInvoked more locally --- .../src/Areas/Server/Commands/Runtime/McpRuntime.cs | 4 ---- .../Server/Commands/ToolLoading/NamespaceToolLoader.cs | 4 ++-- .../Server/Commands/ToolLoading/ServerToolLoader.cs | 5 +++-- .../Commands/ToolLoading/SingleProxyToolLoader.cs | 6 ++++-- core/Azure.Mcp.Core/src/Commands/CommandFactory.cs | 4 ++-- .../src/Services/Telemetry/TelemetryService.cs | 10 ++++------ 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/core/Azure.Mcp.Core/src/Areas/Server/Commands/Runtime/McpRuntime.cs b/core/Azure.Mcp.Core/src/Areas/Server/Commands/Runtime/McpRuntime.cs index 0b614d38a3..7000cd1469 100644 --- a/core/Azure.Mcp.Core/src/Areas/Server/Commands/Runtime/McpRuntime.cs +++ b/core/Azure.Mcp.Core/src/Areas/Server/Commands/Runtime/McpRuntime.cs @@ -77,10 +77,6 @@ public async ValueTask CallToolHandler(RequestContext. If not, the tag will be updated in - // the children ToolLoaders. - activity?.AddTag(TagName.IsServerCommandInvoked, true); - var symbol = OptionDefinitions.Common.Subscription; var subscriptionArgument = request.Params?.Arguments? diff --git a/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/NamespaceToolLoader.cs b/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/NamespaceToolLoader.cs index 48f7996359..fa69c41584 100644 --- a/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/NamespaceToolLoader.cs +++ b/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/NamespaceToolLoader.cs @@ -173,8 +173,6 @@ public override async ValueTask CallToolHandler(RequestContext InvokeChildToolAsync( }; } + Activity.Current?.SetTag(TagName.IsServerCommandInvoked, true); IReadOnlyDictionary namespaceCommands; try { @@ -460,6 +459,7 @@ There was an error finding or calling tool and command. private async Task InvokeToolLearn(RequestContext request, string? intent, string namespaceName, CancellationToken cancellationToken) { + Activity.Current?.SetTag(TagName.IsServerCommandInvoked, false); var toolsJson = GetChildToolListJson(request, namespaceName); var learnResponse = new CallToolResult diff --git a/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/ServerToolLoader.cs b/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/ServerToolLoader.cs index bc41832413..4eabe7940b 100644 --- a/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/ServerToolLoader.cs +++ b/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/ServerToolLoader.cs @@ -9,6 +9,7 @@ using ModelContextProtocol; using ModelContextProtocol.Client; using ModelContextProtocol.Protocol; +using static Azure.Mcp.Core.Services.Telemetry.TelemetryConstants; namespace Azure.Mcp.Core.Areas.Server.Commands.ToolLoading; @@ -139,8 +140,6 @@ public override async ValueTask CallToolHandler(RequestContext InvokeChildToolAsync(RequestContext InvokeToolLearn(RequestContext request, string? intent, string tool, CancellationToken cancellationToken) { + Activity.Current?.SetTag(TagName.IsServerCommandInvoked, false); var toolsJson = await GetChildToolListJsonAsync(request, tool); var learnResponse = new CallToolResult diff --git a/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/SingleProxyToolLoader.cs b/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/SingleProxyToolLoader.cs index 99400ca9a7..f0b9942df9 100644 --- a/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/SingleProxyToolLoader.cs +++ b/core/Azure.Mcp.Core/src/Areas/Server/Commands/ToolLoading/SingleProxyToolLoader.cs @@ -8,6 +8,7 @@ using ModelContextProtocol; using ModelContextProtocol.Client; using ModelContextProtocol.Protocol; +using static Azure.Mcp.Core.Services.Telemetry.TelemetryConstants; namespace Azure.Mcp.Core.Areas.Server.Commands.ToolLoading; @@ -132,8 +133,6 @@ public override async ValueTask CallToolHandler(RequestContext GetToolListJsonAsync(RequestContext RootLearnModeAsync(RequestContext request, string intent, CancellationToken cancellationToken) { + Activity.Current?.SetTag(TagName.IsServerCommandInvoked, false); var toolsJson = await GetRootToolsJsonAsync(); var learnResponse = new CallToolResult { @@ -236,6 +236,7 @@ Here are the available list of tools. private async Task ToolLearnModeAsync(RequestContext request, string intent, string tool, CancellationToken cancellationToken) { + Activity.Current?.SetTag(TagName.IsServerCommandInvoked, false); var toolsJson = await GetToolListJsonAsync(request, tool); if (string.IsNullOrEmpty(toolsJson)) { @@ -272,6 +273,7 @@ private async Task ToolLearnModeAsync(RequestContext CommandModeAsync(RequestContext request, string intent, string tool, string command, Dictionary parameters, CancellationToken cancellationToken) { + Activity.Current?.SetTag(TagName.IsServerCommandInvoked, true); McpClient? client; try diff --git a/core/Azure.Mcp.Core/src/Commands/CommandFactory.cs b/core/Azure.Mcp.Core/src/Commands/CommandFactory.cs index 9e8deb5656..e5c3917a7e 100644 --- a/core/Azure.Mcp.Core/src/Commands/CommandFactory.cs +++ b/core/Azure.Mcp.Core/src/Commands/CommandFactory.cs @@ -175,7 +175,7 @@ private void ConfigureCommands(CommandGroup group) private void ConfigureCommandHandler(Command command, IBaseCommand implementation) { - command.SetAction(async (ParseResult parseResult, CancellationToken ct) => + command.SetAction(async (parseResult, ct) => { _logger.LogTrace("Executing '{Command}'.", command.Name); @@ -226,7 +226,7 @@ private void ConfigureCommandHandler(Command command, IBaseCommand implementatio } finally { - _logger.LogTrace("Finished running '{Command}'.", command.Name); + _logger.LogTrace("Finished running '{Command}'.", command.Name); } }); } diff --git a/core/Azure.Mcp.Core/src/Services/Telemetry/TelemetryService.cs b/core/Azure.Mcp.Core/src/Services/Telemetry/TelemetryService.cs index 1165027ca7..9f67329095 100644 --- a/core/Azure.Mcp.Core/src/Services/Telemetry/TelemetryService.cs +++ b/core/Azure.Mcp.Core/src/Services/Telemetry/TelemetryService.cs @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using System.Collections.Generic; -using System.Collections.Immutable; using System.Diagnostics; using Azure.Mcp.Core.Areas.Server.Options; using Azure.Mcp.Core.Configuration; @@ -42,10 +40,10 @@ public TelemetryService(IMachineInformationProvider informationProvider, ILogger logger) { _isEnabled = options.Value.IsTelemetryEnabled; - _tagsList = new List>() - { + _tagsList = + [ new(TagName.AzureMcpVersion, options.Value.Version), - }; + ]; if (serverOptions?.Value != null) { @@ -68,7 +66,7 @@ public TelemetryService(IMachineInformationProvider informationProvider, } CheckInitialization(); - return _tagsList.ToImmutableList(); + return [.. _tagsList]; } /// From 594bfffd7030f5a02003f6d1a760ff9748161a54 Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Mon, 20 Oct 2025 12:27:50 -0400 Subject: [PATCH 2/3] Fix linting --- core/Azure.Mcp.Core/src/Commands/CommandFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Azure.Mcp.Core/src/Commands/CommandFactory.cs b/core/Azure.Mcp.Core/src/Commands/CommandFactory.cs index 8c3ba17316..f4c02b92ac 100644 --- a/core/Azure.Mcp.Core/src/Commands/CommandFactory.cs +++ b/core/Azure.Mcp.Core/src/Commands/CommandFactory.cs @@ -226,7 +226,7 @@ private void ConfigureCommandHandler(Command command, IBaseCommand implementatio } finally { - _logger.LogTrace("Finished running '{Command}'.", command.Name); + _logger.LogTrace("Finished running '{Command}'.", command.Name); } }); } From 8767866ba4e25102d09698ae4b99fc738355ad0f Mon Sep 17 00:00:00 2001 From: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com> Date: Mon, 20 Oct 2025 14:18:11 -0400 Subject: [PATCH 3/3] CHANGELOG entry --- servers/Azure.Mcp.Server/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/servers/Azure.Mcp.Server/CHANGELOG.md b/servers/Azure.Mcp.Server/CHANGELOG.md index 6a9747b508..4e726db795 100644 --- a/servers/Azure.Mcp.Server/CHANGELOG.md +++ b/servers/Azure.Mcp.Server/CHANGELOG.md @@ -12,6 +12,8 @@ The Azure MCP Server updates automatically by default whenever a new release com ### Other Changes +- Updated how `IsServerCommandInvoked` telemetry is captured to more correctly report whether learning or tool call was performed. [[#874](https://github.com/microsoft/mcp/pull/874)] + ## 0.9.4 (2025-10-17) ### Features Added