Skip to content

Refactor McpFunctionMetadataTransformer using Builder pattern with pluggable steps#216

Open
liliankasem wants to merge 1 commit intomainfrom
liliankasem/refactor/mcp-binding-builder
Open

Refactor McpFunctionMetadataTransformer using Builder pattern with pluggable steps#216
liliankasem wants to merge 1 commit intomainfrom
liliankasem/refactor/mcp-binding-builder

Conversation

@liliankasem
Copy link
Copy Markdown
Member

Summary

Decomposes the monolithic McpFunctionMetadataTransformer into a fluent McpBindingBuilder with pluggable extension method steps. Each transformation concern lives in its own file and can be added or removed without modifying the builder itself.

Motivation

The transformer had grown to ~210 lines mixing parsing, tool property resolution, metadata enrichment, and property binding patching in a single class. This refactor improves:

  • Separation of concerns — each step is a self-contained extension method
  • Extensibility — new steps are added by creating a new file, no existing code changes
  • Testability — each step can be tested in isolation

Changes

New: McpBindingBuilder/

File Purpose
McpBindingBuilder.cs Fluent builder core — HasBindings, Build(), binding parsing. Never needs to change.
McpBuilderContext.cs Shared state between steps (bindings, function metadata, resolved properties)
McpParsedBinding.cs Typed representation of a parsed MCP binding
MetadataMerger.cs Static utility for merging fluent + attributed metadata JSON

New: McpBindingBuilder/Steps/

File Purpose
AddToolPropertiesExtension.cs Resolves tool properties (configured options → attribute reflection fallback)
AddMetadataExtension.cs Applies fluent API + [McpMetadata] attribute metadata onto trigger bindings
PatchPropertyBindingsExtension.cs Patches mcpToolProperty bindings with type info from resolved properties

Modified: McpFunctionMetadataTransformer.cs

Reduced from ~210 lines to ~40 lines. Now a thin loop:

new McpBindingBuilder(function, logger)
    .AddToolProperties(toolOptionsMonitor)
    .AddMetadata(toolOptionsMonitor, resourceOptionsMonitor)
    .PatchPropertyBindings()
    .Build();

How to add a new step

Create a single file with an extension method — no other files change:

// Steps/AddMyNewStepExtension.cs
internal static class AddMyNewStepExtension
{
    public static McpBindingBuilder AddMyNewStep(this McpBindingBuilder builder, ...)
    {
        var context = builder.Context;
        // transform bindings...
        return builder;
    }
}

aishwaryabh
aishwaryabh previously approved these changes Mar 27, 2026
…uggable steps

Decomposes the monolithic McpFunctionMetadataTransformer into a fluent
McpBindingBuilder with pluggable extension method steps. Each transformation
concern lives in its own file and can be added or removed without modifying
the builder itself.

New builder steps:
- AddToolProperties: resolves tool properties from options/attributes
- AddPromptArguments: resolves prompt arguments from options/attributes
- AddMetadata: applies fluent + attributed metadata for tool/resource/prompt
- AddAppUiMetadata: merges MCP App UI metadata and emits synthetic functions
- PatchPropertyBindings: patches mcpToolProperty bindings with type info

Also extracts MetadataMerger as a standalone utility class.

Rebased on top of main, incorporating prompt support (#210/#211),
MCP Apps fluent API (#226), and instrumentation changes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@liliankasem liliankasem force-pushed the liliankasem/refactor/mcp-binding-builder branch from 09cf354 to 34ffdcc Compare April 7, 2026 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants