Benevia.Core.MCP.Generator

Introduction

Benevia.Core.MCP.Generator is a Roslyn source generator that produces MCP (Model Context Protocol) tools at compile time from entities marked with [McpEntity]. It works alongside Benevia.Core.MCP to provide the generated tool classes that the runtime library registers and serves.

This generator follows the same pattern as Benevia.Core.API.Generator and Benevia.Core.Events.Generator — it reads entity model attributes and produces source files during compilation.

What Gets Generated

For each [McpEntity] entity, the generator produces:

Output File Purpose
Tool class [Entity]Tools.g.cs Get, List, Create, Update, Delete methods
DTO class [Entity]Dto.g.cs Nullable property class for Create/Update (only if Create or Update is enabled)
Feature registration McpFeatureRegistration.g.cs Tool metadata for the registry
Feature enum Feature.g.cs Enum of feature groups
Entity type enum EntityType.g.cs Enum of exposed entities
Entity schemas EntitySchemas.g.cs JSON schemas for all entities
Meta-tools MetaTools.g.cs Dynamic toolset filtering
Schema resources SchemaResources.g.cs MCP resource endpoints

Feature Derivation

Features are derived from entity namespaces. The segment immediately after .Model. becomes the feature name:

Namespace Feature
MyApp.Model.Products Products
MyApp.Model.Products.Catalog Products
MyApp.Model.Sales Sales
MyApp.Model.Hatchery Hatchery

Setup

Add as an analyzer reference in your model project:

<PackageReference Include="Benevia.Core.MCP.Generator"
                  Version="$(BeneviaCoreMcpGeneratorVersion)"
                  OutputItemType="Analyzer"
                  ReferenceOutputAssembly="false" />

To inspect generated files, enable EmitCompilerGeneratedFiles in your model project:

<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

Generated files appear under obj/Generated/Benevia.Core.MCP.Generator/.

Dependencies

  • Benevia.Core.Generator — shared generator infrastructure (EntityResult, PropertyResult, attribute analysis)
  • Microsoft.CodeAnalysis.CSharp — Roslyn APIs for source generation

More Info