Skip to content
Merged
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
108 changes: 53 additions & 55 deletions src/Azure.DataApiBuilder.Mcp/Core/McpServerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,71 +22,69 @@ internal static IServiceCollection ConfigureMcpServer(this IServiceCollection se
services.AddMcpServer(options =>
{
options.ServerInfo = new() { Name = McpProtocolDefaults.MCP_SERVER_NAME, Version = McpProtocolDefaults.MCP_SERVER_VERSION };
options.Capabilities = new()
})
.WithHttpTransport()
.WithListToolsHandler(
(request, ct) =>
{
Comment thread
RubenCerna2079 marked this conversation as resolved.
Tools = new()
McpToolRegistry? toolRegistry = request.Services?.GetRequiredService<McpToolRegistry>();
if (toolRegistry == null)
{
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.");
}
return ValueTask.FromResult(new ListToolsResult
{
Tools = tools
});
}
)
.WithCallToolHandler(
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.");
}
string? toolName = request.Params?.Name;
if (string.IsNullOrEmpty(toolName))
{
throw new McpException("Tool name is required.");
}

if (!toolRegistry.TryGetTool(toolName, out IMcpTool? tool))
{
throw new McpException($"Unknown tool: '{toolName}'");
}
if (!toolRegistry.TryGetTool(toolName, out IMcpTool? tool))
{
throw new McpException($"Unknown tool: '{toolName}'");
}

JsonDocument? arguments = null;
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;
}
JsonDocument? arguments = null;
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);
}
string json = JsonSerializer.Serialize(jsonObject);
arguments = JsonDocument.Parse(json);
Comment thread
RubenCerna2079 marked this conversation as resolved.
}

try
{
return await tool!.ExecuteAsync(arguments, request.Services!, ct);
}
finally
{
arguments?.Dispose();
}
}
try
{
return await tool!.ExecuteAsync(arguments, request.Services!, ct);
}
};
})
.WithHttpTransport();
finally
{
arguments?.Dispose();
}
}
);

return services;
}
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
4 changes: 2 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="8.0.10" />
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.38.1" />
<PackageVersion Include="ModelContextProtocol" Version="0.3.0-preview.4" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.4" />
<PackageVersion Include="ModelContextProtocol" Version="1.0.0" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="1.0.0" />
<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" />
Expand Down
Loading