Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pipelines/cosmos-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ steps:

- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

# The .NET CLI commands in proceeding tasks use the .NET SDK version specified ("selected") here.
# Per Microsoft Learn Docs, "Selecting the .NET SDK version is independent from
Expand Down
4 changes: 4 additions & 0 deletions .pipelines/dwsql-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
steps:
- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

# The .NET CLI commands in proceeding tasks use the .NET SDK version specified ("selected") here.
# Per Microsoft Learn Docs, "Selecting the .NET SDK version is independent from
Expand Down Expand Up @@ -170,6 +172,8 @@ jobs:

- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

# The .NET CLI commands in proceeding tasks use the .NET SDK version specified ("selected") here.
# Per Microsoft Learn Docs, "Selecting the .NET SDK version is independent from
Expand Down
4 changes: 4 additions & 0 deletions .pipelines/mssql-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
steps:
- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

# The .NET CLI commands in proceeding tasks use the .NET SDK version specified ("selected") here.
# Per Microsoft Learn Docs, "Selecting the .NET SDK version is independent from
Expand Down Expand Up @@ -174,6 +176,8 @@ jobs:

- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

# The .NET CLI commands in proceeding tasks use the .NET SDK version specified ("selected") here.
# Per Microsoft Learn Docs, "Selecting the .NET SDK version is independent from
Expand Down
2 changes: 2 additions & 0 deletions .pipelines/mysql-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
steps:
- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

# The .NET CLI commands in proceeding tasks use the .NET SDK version specified ("selected") here.
# Per Microsoft Learn Docs, "Selecting the .NET SDK version is independent from
Expand Down
2 changes: 2 additions & 0 deletions .pipelines/pg-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:
steps:
- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

# The .NET CLI commands in proceeding tasks use the .NET SDK version specified ("selected") here.
# Per Microsoft Learn Docs, "Selecting the .NET SDK version is independent from
Expand Down
2 changes: 2 additions & 0 deletions .pipelines/templates/build-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
steps:
- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

# If this is a release, do not append the build number at the end as it will
# generate the prerelease nuget version.
Expand Down
2 changes: 2 additions & 0 deletions .pipelines/templates/static-tools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
steps:
- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'
inputs:
nuGetServiceConnections: 'EngThriveNugetFeedAccessForSqlDab'

- checkout: self # self represents the repo where the initial Pipelines YAML file was found
clean: true # if true, execute `execute git clean -ffdx && git reset --hard HEAD` before fetching
Expand Down
19 changes: 19 additions & 0 deletions Nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@
<packageSources>
<clear />
<add key="data_api_builder_build_packages" value="https://pkgs.dev.azure.com/sqldab/fcb212b3-b288-4c9e-b55a-5842a268b16d/_packaging/data_api_builder_build_packages/nuget/v3/index.json" />
<add key="EngThrive-MCP" value="https://pkgs.dev.azure.com/msazure/One/_packaging/EngThrive-MCP/nuget/v3/index.json" />
</packageSources>
<packageSourceMapping>
<packageSource key="data_api_builder_build_packages">
<package pattern="*" />
</packageSource>
<!-- CI (Azure Pipelines NuGetAuthenticate) renames the source with a "feed-" prefix at runtime -->
<packageSource key="feed-data_api_builder_build_packages">
<package pattern="*" />
</packageSource>
<packageSource key="EngThrive-MCP">
<package pattern="Microsoft.ModelContextProtocol.*" />
<package pattern="ModelContextProtocol" />
<package pattern="ModelContextProtocol.*" />
<package pattern="Microsoft.Identity.ServiceEssentials.*" />
<package pattern="Microsoft.Identity.SettingsProvider" />
<package pattern="Microsoft.Identity.TokenRevocationFromSettings" />
<package pattern="Microsoft.Identity.ServerAuthorization" />
</packageSource>
</packageSourceMapping>
<disabledPackageSources>
<clear />
</disabledPackageSources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- Suppress transitive dependency version mismatch warnings from MCP SDK -->
<NoWarn>$(NoWarn);NU1603</NoWarn>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Azure.DataApiBuilder.Service.Tests" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ModelContextProtocol" />
<PackageReference Include="ModelContextProtocol.AspNetCore" />
<PackageReference Include="Microsoft.ModelContextProtocol.HttpServer" />
</ItemGroup>

<ItemGroup>
Expand Down
121 changes: 63 additions & 58 deletions src/Azure.DataApiBuilder.Mcp/Core/McpServerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;

namespace Azure.DataApiBuilder.Mcp.Core
{
Expand All @@ -16,80 +17,84 @@ namespace Azure.DataApiBuilder.Mcp.Core
internal static class McpServerConfiguration
{
/// <summary>
/// Configures the MCP server with tool capabilities
/// Configures the MCP server with tool capabilities.
/// </summary>
internal static IServiceCollection ConfigureMcpServer(this IServiceCollection services)
{
services.AddMcpServer(options =>
services.AddMcpServer()
.WithListToolsHandler((RequestContext<ListToolsRequestParams> request, CancellationToken ct) =>
{
options.ServerInfo = new() { Name = McpProtocolDefaults.MCP_SERVER_NAME, Version = McpProtocolDefaults.MCP_SERVER_VERSION };
options.Capabilities = new()
McpToolRegistry? toolRegistry = request.Services?.GetRequiredService<McpToolRegistry>();
if (toolRegistry == null)
{
Tools = new()
{
ListToolsHandler = (request, ct) =>
{
McpToolRegistry? toolRegistry = request.Services?.GetRequiredService<McpToolRegistry>();
if (toolRegistry == null)
{
throw new InvalidOperationException("Tool registry is not available.");
}
throw new InvalidOperationException("Tool registry is not available.");
}

List<Tool> tools = toolRegistry.GetAllTools().ToList();
List<Tool> tools = toolRegistry.GetAllTools().ToList();

return ValueTask.FromResult(new ListToolsResult
{
Tools = tools
});
},
CallToolHandler = async (request, ct) =>
{
McpToolRegistry? toolRegistry = request.Services?.GetRequiredService<McpToolRegistry>();
if (toolRegistry == null)
{
throw new InvalidOperationException("Tool registry is not available.");
}

string? toolName = request.Params?.Name;
if (string.IsNullOrEmpty(toolName))
{
throw new McpException("Tool name is required.");
}
return ValueTask.FromResult(new ListToolsResult
{
Tools = tools
});
})
.WithCallToolHandler(async (RequestContext<CallToolRequestParams> request, CancellationToken ct) =>
{
McpToolRegistry? toolRegistry = request.Services?.GetRequiredService<McpToolRegistry>();
if (toolRegistry == null)
{
throw new InvalidOperationException("Tool registry is not available.");
}

if (!toolRegistry.TryGetTool(toolName, out IMcpTool? tool))
{
throw new McpException($"Unknown tool: '{toolName}'");
}
string? toolName = request.Params?.Name;
if (string.IsNullOrEmpty(toolName))
{
throw new McpException("Tool name is required.");
}

JsonDocument? arguments = null;
try
{
if (request.Params?.Arguments != null)
{
// Convert IReadOnlyDictionary<string, JsonElement> to JsonDocument
Dictionary<string, object?> jsonObject = new();
foreach (KeyValuePair<string, JsonElement> kvp in request.Params.Arguments)
{
jsonObject[kvp.Key] = kvp.Value;
}
if (!toolRegistry.TryGetTool(toolName, out IMcpTool? tool))
{
throw new McpException($"Unknown tool: '{toolName}'");
}

string json = JsonSerializer.Serialize(jsonObject);
arguments = JsonDocument.Parse(json);
}
if (tool is null || request.Services is null)
{
throw new InvalidOperationException("Tool or service provider unexpectedly null.");
}

return await McpTelemetryHelper.ExecuteWithTelemetryAsync(
tool!, toolName, arguments, request.Services!, ct);
}
finally
{
arguments?.Dispose();
}
JsonDocument? arguments = null;
try
{
if (request.Params?.Arguments != null)
{
// Convert IReadOnlyDictionary<string, JsonElement> to JsonDocument
Dictionary<string, object?> jsonObject = new();
foreach (KeyValuePair<string, JsonElement> kvp in request.Params.Arguments)
{
jsonObject[kvp.Key] = kvp.Value;
}

string json = JsonSerializer.Serialize(jsonObject);
arguments = JsonDocument.Parse(json);
}
};

return await McpTelemetryHelper.ExecuteWithTelemetryAsync(
tool, toolName, arguments, request.Services, ct);
}
finally
{
arguments?.Dispose();
}
})
.WithHttpTransport();

// Configure underlying MCP server options
services.PostConfigure<McpServerOptions>(options =>
{
options.ServerInfo = new() { Name = McpProtocolDefaults.MCP_SERVER_NAME, Version = McpProtocolDefaults.MCP_SERVER_VERSION };
options.Capabilities ??= new();
options.Capabilities.Tools ??= new();
});

return services;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Azure.DataApiBuilder.Mcp/Core/McpToolRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public void RegisterTool(IMcpTool tool)
// Check for duplicate tool names (case-insensitive)
if (_tools.TryGetValue(toolName, out IMcpTool? existingTool))
{
// If the same tool instance is already registered, skip silently.
// This can happen when both McpToolRegistryInitializer (hosted service)
// and McpStdioHelper register tools during stdio mode startup.
if (ReferenceEquals(existingTool, tool))
{
return;
}

string existingToolType = existingTool.ToolType == ToolType.BuiltIn ? "built-in" : "custom";
string newToolType = tool.ToolType == ToolType.BuiltIn ? "built-in" : "custom";

Expand Down
4 changes: 2 additions & 2 deletions src/Azure.DataApiBuilder.Mcp/Utils/McpResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static CallToolResult BuildSuccessResult(
{
Content = new List<ContentBlock>
{
new TextContentBlock { Type = "text", Text = output }
new TextContentBlock { Text = output }
}
};
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public static CallToolResult BuildErrorResult(
{
Content = new List<ContentBlock>
{
new TextContentBlock { Type = "text", Text = output }
new TextContentBlock { Text = output }
},
IsError = true
};
Expand Down
7 changes: 3 additions & 4 deletions src/Core/Telemetry/TelemetryTracesHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.Net;
using Azure.DataApiBuilder.Config.ObjectModel;
using OpenTelemetry.Trace;
using Kestral = Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpMethod;

namespace Azure.DataApiBuilder.Core.Telemetry
Expand Down Expand Up @@ -104,8 +103,8 @@ public static void TrackMainControllerActivityFinishedWithException(
{
if (activity.IsAllDataRequested)
{
activity.SetStatus(Status.Error.WithDescription(ex.Message));
activity.RecordException(ex);
activity.SetStatus(ActivityStatusCode.Error, ex.Message);
activity.AddException(ex);
activity.SetTag("error.type", ex.GetType().Name);
activity.SetTag("error.message", ex.Message);
activity.SetTag("status.code", statusCode);
Expand Down Expand Up @@ -174,7 +173,7 @@ public static void TrackMcpToolExecutionFinishedWithException(
if (activity.IsAllDataRequested)
{
activity.SetStatus(ActivityStatusCode.Error, ex.Message);
activity.RecordException(ex);
activity.AddException(ex);
activity.SetTag("error.type", ex.GetType().Name);
activity.SetTag("error.message", ex.Message);

Expand Down
15 changes: 7 additions & 8 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Primitives" Version="9.0.0" />
<PackageVersion Include="ModelContextProtocol" Version="0.3.0-preview.4" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.4" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageVersion Include="Microsoft.ModelContextProtocol.HttpServer" Version="0.1.0-preview.25" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.13.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.13.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
<!--When updating Microsoft.Data.SqlClient, update license URL in scripts/notice-generation.ps1-->
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.3" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" />
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.1.2" />
Expand Down
Loading