Skip to content

DynamicCustomTool: Align tool input schema with DB metadata at runtime#3619

Merged
souvikghosh04 merged 6 commits into
mainfrom
Usr/sogh/cust-tool-schema-alignment
May 21, 2026
Merged

DynamicCustomTool: Align tool input schema with DB metadata at runtime#3619
souvikghosh04 merged 6 commits into
mainfrom
Usr/sogh/cust-tool-schema-alignment

Conversation

@souvikghosh04
Copy link
Copy Markdown
Contributor

@souvikghosh04 souvikghosh04 commented May 20, 2026

Why make this change?

Custom MCP tools used a permissive multi-type array for all parameters in the input schema. MCP clients had no type information to guide users or validate inputs before execution.

What is this change?

  • InitializeMetadata: Resolves StoredProcedureDefinition.Parameters from the metadata provider and builds a typed JSON Schema (integer/number/boolean/string) for each DB-discovered parameter.
  • Fallback: Falls back to config-based permissive schema when DB metadata is unavailable.
  • Both modes: Applied in HTTP mode (McpToolRegistryInitializer) and stdio mode (McpStdioHelper) via shared McpToolRegistry.InitializeAndRegisterTools helper.
  • Default descriptions: Surfaces (default: value) in parameter descriptions when HasConfigDefault is set.

How was this tested?

  • Unit Tests
Test Scenario
UsesDbMetadata_WhenInitialized Schema uses typed params after InitializeMetadata
FallsBackToConfig_WhenDbMetadataUnavailable Permissive array when no metadata factory
UsesConfigSchema_WhenNotInitialized Config schema used without InitializeMetadata call
IncludesAllDbDiscoveredParams Params not in config but in DB appear in schema
MapsSystemTypesToJsonSchemaTypes (x12) int/long/short/byte to integer, float/double/decimal to number, bool to boolean, string/Guid/DateTime to string
UsesPermissiveType_WhenSystemTypeIsNull Null SystemType falls back to multi-type array
IncludesDefaultInDescription (default: value) in description
ZeroParamSP_ReturnsEmptyProperties Empty properties for zero-param SP
  • Integration Tests
Test Scenario
InitializeMetadata_SchemaReflectsDbParameterTypes (x3) int->integer, varchar->string mapping against live DB
InitializeMetadata_ZeroParamSP_HasEmptyProperties Zero-param SP has empty properties from DB metadata
InitializeMetadata_DescriptionIncludesConfigDefaults (x2) Config defaults (randomX, 1234) in descriptions
  • Manual Testing
Tool Before After
get_book(id) type: [...] type: integer
insert_book(title, publisher_id) permissive string, integer + defaults in desc
update_book_title(id, title) permissive integer, string + defaults in desc
get_books (zero-param) empty empty
End-to-end execution get_book(id=1) N/A returns book record

Sample Request(s)

tools/list response (get_book schema after fix):

{ "name": "get_book", "inputSchema": { "type": "object", "properties": { "id": { "type": "integer", "description": "Parameter id" } } } }

The stdio code path in McpStdioHelper.RunMcpStdioHost registers tools
directly without going through McpToolRegistryInitializer, so custom
tools were missing the DB-metadata-based schema enrichment.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates MCP DynamicCustomTool registration so the tool input JSON Schema reflects stored procedure parameter metadata discovered from the database at runtime (with a config-based permissive fallback), improving client-side validation and parameter discoverability.

Changes:

  • Added post-DI initialization (InitializeMetadata) to build/cache a DB-metadata-based input schema for DynamicCustomTool.
  • Initialized metadata before tool registration in both HTTP-hosted (McpToolRegistryInitializer) and stdio (McpStdioHelper) MCP modes.
  • Added unit tests covering schema alignment behaviors (typed mapping, fallback behavior, defaults in descriptions, zero-parameter SP).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/Azure.DataApiBuilder.Mcp/Core/DynamicCustomTool.cs Adds DB-metadata-based schema building and caching, plus type mapping and default-in-description support.
src/Azure.DataApiBuilder.Mcp/Core/McpToolRegistryInitializer.cs Ensures custom tools initialize DB-backed schema before being registered (HTTP mode).
src/Service/Utilities/McpStdioHelper.cs Ensures custom tools initialize DB-backed schema before being registered (stdio mode).
src/Service.Tests/Mcp/DynamicCustomToolTests.cs Adds unit tests validating DB-metadata schema enrichment and fallback behaviors.

Comment thread src/Azure.DataApiBuilder.Mcp/Core/DynamicCustomTool.cs Outdated
Comment thread src/Azure.DataApiBuilder.Mcp/Core/DynamicCustomTool.cs
Comment thread src/Service.Tests/Mcp/DynamicCustomToolTests.cs Outdated
Comment thread src/Azure.DataApiBuilder.Mcp/Core/McpToolRegistryInitializer.cs Outdated
Comment thread src/Azure.DataApiBuilder.Mcp/Core/DynamicCustomTool.cs
- Use ParameterDefinition.Description with fallback to generic text
- Add ArgumentNullException guard to InitializeMetadata
- Remove unnecessary JsonDocument allocation in test helper
- Extract InitializeAndRegisterTools into McpToolRegistry (shared by
  McpToolRegistryInitializer and McpStdioHelper)
Comment thread src/Azure.DataApiBuilder.Mcp/Core/McpToolRegistry.cs
@souvikghosh04 souvikghosh04 enabled auto-merge (squash) May 21, 2026 05:43
@souvikghosh04 souvikghosh04 requested a review from aaronburtle May 21, 2026 05:43
Copy link
Copy Markdown
Contributor

@aaronburtle aaronburtle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for fixing everything!

@souvikghosh04 souvikghosh04 merged commit b84d8d6 into main May 21, 2026
12 checks passed
@souvikghosh04 souvikghosh04 deleted the Usr/sogh/cust-tool-schema-alignment branch May 21, 2026 19:55
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Data API builder May 21, 2026
souvikghosh04 added a commit that referenced this pull request May 22, 2026
#3601, #3619) (#3630)

Cherry-pick of commits from main to release/2.0:

- 5d6faf9 ExecuteEntityTool: DB metadata based parameter support
(#3600)
- a085d86 DynamicCustomTool: DB metadata based parameter support
(#3601)
- b84d8d6 DynamicCustomTool: Align tool input schema with DB metadata
at runtime (#3619)

---------

Co-authored-by: Anusha Kolan <anushakolan10@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

DynamicCustomTool: Align tool input schema with DB metadata at runtime

5 participants