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
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ public async Task<CallToolResult> ExecuteAsync(
if (!string.IsNullOrWhiteSpace(select))
{
// Update the context to specify which fields will be returned from the entity.
IEnumerable<string> fieldsReturnedForFind = select.Split(",").ToList();
List<string> fieldsReturnedForFind = select.Split(',').Select(field => field.Trim()).ToList();
if (fieldsReturnedForFind.Any(string.IsNullOrEmpty))
{
return McpResponseBuilder.BuildErrorResult(toolName, "InvalidArguments", "The 'select' argument cannot contain empty field names.", logger);
}

context.UpdateReturnFields(fieldsReturnedForFind);
}

Expand Down
33 changes: 33 additions & 0 deletions src/Service.Tests/Mcp/ReadRecordsToolMsSqlIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,39 @@ public async Task ReadRecords_WithSelect_ReturnsSelectedFields()
Assert.IsTrue(firstRecord.TryGetProperty("title", out _), "Expected 'title' field in result.");
}

/// <summary>
/// Reads records with whitespace after a comma in the select clause.
/// </summary>
[TestMethod]
public async Task ReadRecords_WithWhitespaceAfterSelectComma_ReturnsSelectedFields()
{
CallToolResult result = await ExecuteReadAsync("Book", select: "id, title");

AssertSuccess(result, "ReadRecords with whitespace after a select comma should succeed.");

JsonElement root = ParseResultRoot(result);
JsonElement records = GetRecordsArray(root);
JsonElement firstRecord = records[0];
Assert.IsTrue(firstRecord.TryGetProperty("id", out _), "Expected 'id' field in result.");
Assert.IsTrue(firstRecord.TryGetProperty("title", out _), "Expected 'title' field in result.");
}

/// <summary>
/// Rejects empty field names in the select clause with a clear error.
/// </summary>
[DataTestMethod]
[DataRow("id,title,")]
[DataRow("id,,title")]
public async Task ReadRecords_WithEmptySelectField_ReturnsInvalidArguments(string select)
{
CallToolResult result = await ExecuteReadAsync("Book", select: select);

AssertError(result);
JsonElement error = ParseResultRoot(result).GetProperty("error");
Assert.AreEqual("InvalidArguments", error.GetProperty("type").GetString());
Assert.AreEqual("The 'select' argument cannot contain empty field names.", error.GetProperty("message").GetString());
}

/// <summary>
/// Reads records with an OData filter expression and verifies filtered results are returned.
/// </summary>
Expand Down