modelcontextprotocol / modelcontextprotocol/csharp-sdk

Configure McpServerToolCreateOptions via WithTools

Open
#1,865 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
4.5k
Forks
814
Avg merge
9d 19h
Merged PRs (30d)
4

Description

Is your feature request related to a problem? Please describe.
When using the same class in multiple places, Text.Json creates a schema with "$ref", e.g.

"Relations": {
      "$ref": "#/properties/Table/properties/Relations",
      "items": {}
},

This is a valid json schema even if the "items":{} is redundant.

however, mcpinspector shows a warning.

Schema carries no validation keyword at all, so it accepts any value — the object-literal spelling of a bare true.
Fix: Declare what the value actually is — e.g. {"type": "object", "additionalProperties": true} for a free-form object. That is a deliberate change of contract, which is the point: as written it constrains nothing.
These constructs are legal JSON Schema but are refused or mishandled by some MCP clients, so a tool can work here and fail there.

I added a schema transform to remove the redundant property

public static AIJsonSchemaCreateOptions CreateMcpClientCompatibleSchemaOptions() => new()
{
    TransformSchemaNode = static (_, schema) =>
    {
        if (schema is JsonObject schemaObject && 
            schemaObject.ContainsKey("$ref") && schemaObject["items"] is JsonObject { Count: 0 })
        {
            schemaObject.Remove("items");
        }

        return schema;
    }

but I found no way to set the McpServerToolCreateOptions.SchemaCreateOptions via .WithTools.

Solved it by copying the library .WithTools code to my own method.

public static IMcpServerBuilder WithTools<TToolType>(this IMcpServerBuilder builder,
                                                     Action<McpServerToolCreateOptions>? configureOptions)
{
    foreach (var toolMethod in typeof(TToolType).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
    {
        if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is null) 
            continue;

        if (toolMethod.IsStatic)
        {
            builder.Services.AddSingleton(services =>
            {
                var options = new McpServerToolCreateOptions();
                configureOptions?.Invoke(options);
                options.Services = services;
                return McpServerTool.Create(toolMethod, options);
            });
        }
        else
        {
            static object CreateTarget(IServiceProvider? services, Type type) => 
                services is not null ? ActivatorUtilities.CreateInstance(services, type) : Activator.CreateInstance(type)!;

            builder.Services.AddSingleton(services =>
            {
                var options = new McpServerToolCreateOptions();
                configureOptions?.Invoke(options);
                options.Services = services;
                return McpServerTool.Create(toolMethod, static r => CreateTarget(r.Services, typeof(TToolType)), options);
            });
        }
    }

    return builder;
}

and used like:

services.AddMcpServer()
        .WithHttpTransport(o => { o.Stateless = true; })
        .WithTools<MyTools>(o => { 
             o.SerializerOptions = jsonOptions;
             o.SchemaCreateOptions = CreateMcpClientCompatibleSchemaOptions();
         })

Describe the solution you'd like
Possibility to configure the McpServerToolCreateOptions, maybe like the code above.

Describe alternatives you've considered
Using my own method like above but it seems fragile.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the existing WithTools entry point and inspect how it creates McpServerTool instances and handles McpServerToolCreateOptions. Add a supported configuration path for SchemaCreateOptions and SerializerOptions, then verify that the issue's WithTools(...) usage applies those options to the created tools.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.