Skip to content

Commit 3243510

Browse files
gmohit21Copilot
andauthored
mcp: merge DB columns with config fields in describe_entities for tables/views (#3924)
## Why make this change? Closes #3590. For stored-procedure entities, `describe_entities` already uses database metadata as the source of truth and overlays config metadata on top (#3400 / #3425). However, for **table and view** entities, `DescribeEntitiesTool.BuildFieldMetadataInfo` still read exclusively from `entity.Fields` in `dab-config.json` and ignored database-introspected columns (`SourceDefinition.Columns` and `SourceDefinition.PrimaryKey`). Because `entities.<entity>.fields` is optional and typically used only to configure an `alias`, `description`, or `primary-key` override on individual columns: - A table or view without a `fields` block emitted `"fields": []`, leaving MCP clients/agents blind to all columns and primary keys. - A table or view with a partial `fields` block emitted **only** the configured column(s) and hid the remaining database columns from MCP clients/agents. ## What is this change? `describe_entities` now resolves `DatabaseObject` and `ISqlMetadataProvider` metadata for table and view entities (when `nameOnly` is `false`) via `McpMetadataHelper.TryResolveMetadata`, using the database schema as the source of truth and applying configuration as an overlay: - **Column discovery**: Surfaces every authorized column in `SourceDefinition.Columns`, including database columns not listed under `entity.Fields`. - **`name`**: Uses `ISqlMetadataProvider.TryGetExposedColumnName` (matching `AuthorizationResolver`, REST, and GraphQL exposed-name resolution: `fields[].alias` -> backing column if claimed in `fields[]` without an alias -> legacy `mappings[column]` -> backing column name). - **`description`**: Overlays `fields[].description` from config when configured (prefers exact `Ordinal` column match before `OrdinalIgnoreCase`); defaults to `""`. - **`primaryKey`**: Uses upstream-resolved `SourceDefinition.PrimaryKey` (which already applies `fields[].primary-key` -> `source.key-fields` -> DB primary key and matches `RequestValidator.ValidatePrimaryKey` for `update_record` / `delete_record`). - **`dataType`**: Maps `ColumnDefinition` (`IsArrayType`, `SystemType`, and `DbType`) to `"integer"`, `"number"`, `"boolean"`, `"string"`, `"array"`, or `null` when unknown, enabling agents to identify numeric columns for tools such as `aggregate_records`. - **Column-level authorization, cancellation & stored procedures**: Preserves `ComputeAllowedFieldNames` (`fields.include` / `fields.exclude`) filtering across both merged and fallback paths, propagates `OperationCanceledException` during metadata lookup, and leaves stored-procedure `fields` behavior unchanged. - **Degraded fallback**: When database metadata is unavailable (e.g., non-SQL providers such as Cosmos DB where `EntityToDatabaseObject` is empty), gracefully falls back to projecting config-declared `entity.Fields` (resolving fallback `primaryKey` via `fields[].primary-key` -> `source.key-fields`). ## How was this tested? - [x] Integration Tests (`DescribeEntitiesTableAndViewFieldsMsSqlIntegrationTests`) - [x] Unit Tests (existing MCP / `DescribeEntities` regression suites) ### 1. Automated Integration & Regression Suites `DescribeEntitiesTableAndViewFieldsMsSqlIntegrationTests` covers 7 scenarios end-to-end against `MsSqlMetadataProvider` and `AuthorizationResolver`: - `Publisher` (`fields: []` table) -> surfaces all DB columns and DB primary key. - `Book` (partial `fields` table) -> surfaces unconfigured DB column (`publisher_id`) and preserves DB primary key. - `SupportedType` (`dbo.type_table` with aliased `typeid`) -> preserves aliased DB primary key and verifies scalar `dataType` mappings (`integer`, `number`, `boolean`, `string`) across SQL types. - `books_view_with_mapping` (view with aliased `primary-key: true` in `fields`) -> merges aliased key with remaining view columns. - `books_publishers_view_composite_insertable` (view with composite `source.key-fields` and no `fields`) -> surfaces all view columns and marks composite keys. - `BooksCustomOverlay` -> verifies `Description`, `Fields`-without-alias precedence over `Mappings`, `Mappings` aliasing, and `fields.exclude` column-level authorization. - `CosmosFallbackBook` -> verifies degraded config-field fallback and column-level authorization when `EntityToDatabaseObject` is empty. Commands executed from the repository root and their results: ```powershell # PASSED: 0 errors (full solution build) dotnet build src/Azure.DataApiBuilder.sln --nologo -v quiet # PASSED: 0 formatting changes required dotnet format src/Azure.DataApiBuilder.sln --verify-no-changes --include ` src/Azure.DataApiBuilder.Mcp/BuiltInTools/DescribeEntitiesTool.cs ` src/Service.Tests/Mcp/DescribeEntitiesTableAndViewFieldsMsSqlIntegrationTests.cs # PASSED: 53 passed, 0 failed (DescribeEntities + McpMetadataHelper unit & live SQL Server integration suites) dotnet test src/Service.Tests/Azure.DataApiBuilder.Service.Tests.csproj --no-build ` --filter "FullyQualifiedName~DescribeEntities|FullyQualifiedName~McpMetadataHelperTests" # PASSED: 630 passed, 0 failed (full DB-free MCP unit test suite regression check) dotnet test src/Service.Tests/Azure.DataApiBuilder.Service.Tests.csproj --no-build ` --filter "FullyQualifiedName~Azure.DataApiBuilder.Service.Tests.Mcp&TestCategory!=MsSql&TestCategory!=PostgreSql&TestCategory!=MySql&TestCategory!=DwSql&TestCategory!=CosmosDb_NoSql" ``` ### 2. Local Live End-to-End MCP & VS Code Copilot Chat Testing Validated against a live local SQL Server database (`DabMcpDemo`) hosted through `dab start` over both MCP Streamable HTTP and `--mcp-stdio`, and exercised interactively from **VS Code GitHub Copilot Chat (Agent mode)**: | Entity | Backing SQL Object | Config `fields` Setup | Verified MCP Behavior | | --- | --- | --- | --- | | `Customer` | `dbo.Customers` (Table) | No `fields` block in config | `describe_entities` surfaced all 6 DB columns (`id` with `primaryKey: true, dataType: "integer"`, `full_name`, `email`, `city`, `loyalty_tier`, and `credit_limit` with `dataType: "number"`). Verified `read_records` and `create_record` without column guessing. | | `Order` | `dbo.Orders` (Table) | Partial `fields` block (only `total_amount` aliased to `orderTotal` with description) | `describe_entities` surfaced all 6 columns, merging `orderTotal` + description alongside DB-discovered `order_id` (`primaryKey: true`), `customer_id`, `product_name`, `quantity`, and `status`. Verified `create_record`, `update_record` (by `order_id`), `delete_record`, and `aggregate_records` (`SUM(orderTotal)` grouped by `status`). | | `CustomerOrderSummary` | `dbo.vw_CustomerOrderSummary` (View) | No `fields` block; `source.key-fields: ["customer_id"]` | `describe_entities` surfaced all 5 view columns with `customer_id` marked `primaryKey: true` and numeric `order_count` / `total_spent` types. Verified `read_records` and `aggregate_records`. | ## Sample Request(s) Given a table `dbo.Orders(order_id INT PK, customer_id INT, product_name NVARCHAR(120), quantity INT, total_amount DECIMAL(10,2), status NVARCHAR(30))` configured with a partial `fields` block: ```json "Order": { "source": { "object": "dbo.Orders", "type": "table" }, "fields": [ { "name": "total_amount", "alias": "orderTotal", "description": "Total billed order amount" } ], "permissions": [ { "role": "anonymous", "actions": ["*"] } ] } ``` Calling MCP `tools/call` for `describe_entities` with `{ "entities": ["Order"] }`: ```json { "entities": [ { "name": "Order", "description": "", "fields": [ { "name": "order_id", "description": "", "primaryKey": true, "dataType": "integer" }, { "name": "customer_id", "description": "", "primaryKey": false, "dataType": "integer" }, { "name": "product_name", "description": "", "primaryKey": false, "dataType": "string" }, { "name": "quantity", "description": "", "primaryKey": false, "dataType": "integer" }, { "name": "orderTotal", "description": "Total billed order amount", "primaryKey": false, "dataType": "number" }, { "name": "status", "description": "", "primaryKey": false, "dataType": "string" } ], "permissions": ["CREATE", "DELETE", "READ", "UPDATE"] } ], "count": 1, "status": "success" } ``` --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1088b91 commit 3243510

2 files changed

Lines changed: 809 additions & 19 deletions

File tree

0 commit comments

Comments
 (0)