agentleFS
LeanMeanCleanAppsLeanMeanCleanApps/CleanModelContextProtocol/.agents/skills/json-serialization/SKILL.md

json-serialization

Skill1 starsChanged 6 days agoMITView on GitHub

How this MCP server reads tool arguments and writes results with System.Text.Json - McpSerializer.Options built on the SDK defaults, the source-generated AppJsonSerializerContext and which types it lists, and how tests use the same options. Use when adding a result model, changing how JSON looks, or when a value serialises differently than expected.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Add every new result model, and the ResultPage<T> you return, to AppJsonSerializerContext with [JsonSerializable(typeof(...))].

    line 30
  2. Distinctive:

    Remove a type from the context when nothing returns it any more.

    line 31
  3. Very specific:

    Add ReferenceHandler.IgnoreCycles or Preserve to hide a cycle.

    line 51

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

JSON serialization (MCP)

The rule

Tool arguments, tool results, resources and prompt attachments all use one set of options: McpSerializer.Options in src/Presentation/Serialization/McpSerializer.cs. It starts from the SDK's McpJsonUtilities.DefaultOptions, then sets camelCase names and writes nulls, because tool output schemas list nullable fields such as nextOffset as required and strict clients reject a result that leaves them out, and puts AppJsonSerializerContext first in the resolver chain so the listed types use generated code. The options are read-only once built.

Used byHow
Tools and promptsWithToolsFromAssembly(serializerOptions: McpSerializer.Options) and WithPromptsFromAssembly(...)
Resources and embedded prompt resourcesMcpSerializer.Serialize(value)
Integration testsDeserialize<T>() in tests/Presentation.Tests.Integration/Extensions/JsonSerializerExtensions.cs

AppJsonSerializerContext lists exactly the types the server writes: the result models, their ResultPage<T> forms and Version.

Results are trees. A review inside a movie carries the movie's id and title, not the movie, so there's no cycle handling.

Tool schemas are generated from the C# types, and a nullable field comes out as "type": ["string", "null"]. That's valid JSON Schema, but clients that map schemas onto a single-type dialect, such as Gemini's function declarations, can reject it or drop its constraints. PortableSchemaFilter rewrites each one to an anyOf of single types when tools are listed, keeping description, title and default on the outer node. It runs for every tool, so a new tool needs nothing extra. ListTools_ShouldWrite_PortableSchemas fails if a type array gets through.

The filter is a workaround for those clients, and nothing else needs it. Delete it, with that test, once they accept type arrays. To check, take it out and confirm the Inspector shows no schema portability warnings on any tool.

Steps

  1. Add every new result model, and the ResultPage<T> you return, to AppJsonSerializerContext with [JsonSerializable(typeof(...))].
  2. Remove a type from the context when nothing returns it any more.
  3. Change how JSON looks only in McpSerializer.CreateOptions. Keep the context's [JsonSourceGenerationOptions] matching it.
  4. Serialise resources with McpSerializer.Serialize, never JsonSerializer.Serialize with default options.

Example

// src/Presentation/Serialization/McpSerializer.cs
var options = new JsonSerializerOptions(McpJsonUtilities.DefaultOptions)
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    DefaultIgnoreCondition = JsonIgnoreCondition.Never
};
options.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
options.MakeReadOnly();

Don't

  • Build your own JsonSerializerOptions, including in tests.
  • Add ReferenceHandler.IgnoreCycles or Preserve to hide a cycle. Change the result shape.
  • List Application entities in the context unless a resource returns one directly.
  • Use Newtonsoft.Json.
  • Hand-write a tool schema to get around a portability warning. Change PortableSchemaFilter.

Check

dotnet test tests/Presentation.Tests.Integration

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.