Custom C# data models usage in MCP Tool by generating JSON Schema #1115
IvanMurzak
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Pre-submission Checklist
What would you like to share?
Problem
Need to have easy way to generate Json Schema for complex Input/Output data arguments for MCP Tool, Prompt, Resources. Sample:
Expected Json Schema: Input Arguments
{ "type": "object", "properties": { "dataList": { "$ref": "#/$defs/System.Collections.Generic.List\u003CData\u003E" }, "flag": { "type": "string", "enum": [ "red", "green", "yellow" ], "description": "DEMO ENUM DESCRIPTION." }, "optionalData": { "$ref": "#/$defs/Data" } }, "$defs": { "Data": { "type": "object", "properties": { "optionalInt": { "type": "integer" }, "address": { "type": "string", "description": "THIS IS THE DEMO DESCRIPTION." }, "list": { "$ref": "#/$defs/System.Collections.Generic.List\u003CData\u003E" } } }, "System.Collections.Generic.List\u003CData\u003E": { "type": "array", "items": { "$ref": "#/$defs/Data" } } }, "required": [ "dataList", "flag" ] }Expected Json Schema: Output Result
{ "type": "object", "properties": { "result": { "$ref": "#/$defs/Result" } }, "$defs": { "Result": { "type": "object", "properties": { "isDone": { "type": "boolean" }, "errorMessage": { "type": "string" } }, "required": [ "isDone" ] } }, "required": [ "result" ] }Solution - Automatic JSON Schema Generation
I made ReflectorNet isn't just about reflection; it bridges the gap between strong C# typing and dynamic data formats. One of its most powerful features is the Automatic JSON Schema Generator.
It creates Json Schema with respect to Optional arguments, with respect to almost any type of C# data models (generic, nested class, struct, array, collections and any combinations of all of them). Even if you still have something very exotic, just register your custom
ReflectionConverterwith Json Schema generation overridden method.ReflectorNet solves this with a single line of code.
🚀 Capabilities
The schema generator recursively analyzes your types to produce standard JSON Schemas. It handles edge cases that often break other libraries:
Tin types likeResponse<User>.List<T>,T[],IEnumerable<T>,HashSet<T>.structand primitives (int, bool, double).Dictionary<string, T>to object properties.📦 Installation
💻 Usage Example
Here is a demonstration of generating a schema for a complex, real-world data model involving generics, nested structs, and dictionaries.
1. Define Your Data Model
2. Generate the Schema
You do not need to decorate your classes with attributes. Just create the
Reflectorand callGetSchema.3. The Result
ReflectorNet resolves the Generic
T, correctly identifies theListof structs, and maps theDictionary.{ "type": "object", "properties": { "Timestamp": { "type": "integer" }, "Player": { "$ref": "#/$defs/PlayerProfile" }, "Checkpoints": { "$ref": "#/$defs/System.Collections.Generic.List\u003CGeoPoint\u003E" } }, "required": [ "Timestamp" ], "$defs": { "GameState\u003CPlayerProfile\u003E": { "type": "object", "properties": { "Timestamp": { "type": "integer" }, "Player": { "$ref": "#/$defs/PlayerProfile" }, "Checkpoints": { "$ref": "#/$defs/System.Collections.Generic.List\u003CGeoPoint\u003E" } }, "required": [ "Timestamp" ] }, "PlayerProfile": { "type": "object", "properties": { "Id": { "type": "string", "format": "uuid" }, "Username": { "type": "string" }, "Badges": { "$ref": "#/$defs/System.String[]" }, "Stats": { "$ref": "#/$defs/System.Collections.Generic.Dictionary\u003CSystem.String,System.Int32\u003E" } }, "required": [ "Id" ] }, "System.String[]": { "type": "array", "items": { "type": "string" } }, "System.Collections.Generic.Dictionary\u003CSystem.String,System.Int32\u003E": { "type": "object", "additionalProperties": { "type": "integer" } }, "System.Collections.Generic.List\u003CGeoPoint\u003E": { "type": "array", "items": { "$ref": "#/$defs/GeoPoint" } }, "GeoPoint": { "type": "object", "properties": { "Latitude": { "type": "number" }, "Longitude": { "type": "number" } }, "required": [ "Latitude", "Longitude" ] } } }Relevant Links
Beta Was this translation helpful? Give feedback.
All reactions