Class ValueObjectMcpServerBuilderExtensions
Namespace: AdCodicem.ValueObjects.ModelContextProtocol
Assembly: AdCodicem.ValueObjects.ModelContextProtocol.dll
Registers Model Context Protocol tools whose schemas carry the rules of their value objects, and answers a value-object argument a rule refuses with that rule, as a tool execution error a model can correct from.
public static class ValueObjectMcpServerBuilderExtensions
Inheritance
object ← ValueObjectMcpServerBuilderExtensions
Inherited Members
object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()
Remarks
The SDK's WithTools methods hand a tool no schema options, and the SDK describes a value object, serialized by
a converter of its own, as true, the schema that accepts anything; it answers the exception binding a refused
argument throws with An error occurred invoking 'place_order'., which loses the rule. Each method here mirrors
one of the SDK's, finding the same methods and creating an instance of an instance method's type for each call, and
closes both gaps:
builder.Services
.AddMcpServer()
.WithHttpTransport()
.WithValueObjectTools<OrderTools>();
Each value-object argument is read, before the tool binds it, through the contract the tool's serializer options hold
for its parameter, which runs the value object's converter and its rules, never CreateUnchecked. The first
refused, in the order of the parameters, is answered instead of calling the tool, and no instance of its type is
created:
{"isError":true,"content":[{"type":"text","text":"Argument 'quantity' rejected (value_object.out_of_range): The value is not a valid Quantity: The value must be less than or equal to 100."}],
"structuredContent":{"error":"invalid_argument","argument":"quantity","code":"value_object.out_of_range","message":"The value is not a valid Quantity: The value must be less than or equal to 100."}}
The structured content is left out for a tool that declares an output schema, which it would not conform to. The
message names the value object and the rule, never the value sent. An absent argument the parameter has no default
for, and a null for a value object that cannot be one, are value_object.required; a collection, a
dictionary or an object is answered for a value object it holds, any other error being left to the tool. Parameters
the SDK binds itself, the server, the request context, a progress reporter, a cancellation token or a service, are
never read.
The check is a call-tool filter, added once whatever the number of calls, after every filter the server's options
are configured with, so that it runs after the application's own filters and authorization; a filter added later,
in HttpServerTransportOptions.ConfigureSessionOptions, runs inside it, unless
CallToolWithAlternateHandler, and throws when it creates the server: at
its start over stdio, for each session over HTTP, for each request when the server is stateless.
Where reflection-based serialization is disabled, as native AOT disables it, serializerOptions has to describe
every parameter and result: a copy of
Methods
WithValueObjectTools<TToolType>(IMcpServerBuilder, JsonSerializerOptions?)
Registers the
public static IMcpServerBuilder WithValueObjectTools<TToolType>(this IMcpServerBuilder builder, JsonSerializerOptions? serializerOptions = null)
Parameters
builder IMcpServerBuilder
The builder of the server.
serializerOptions JsonSerializerOptions?
The options the arguments and results are serialized with, or
null for
Returns
IMcpServerBuilder
The builder.
Type Parameters
TToolType
The tool type, whose static and instance methods, public and not, are searched; an instance of it is created for each call of an instance method, its constructor's parameters resolved from the services of the call.
Exceptions
builder is null.
WithValueObjectTools(IMcpServerBuilder, IEnumerable<Type>, JsonSerializerOptions?)
Registers the
[RequiresUnreferencedCode("Discovers tool methods by reflection, as the SDK's non-generic WithTools and WithToolsFromAssembly do, and might not work in native AOT. Use WithValueObjectTools<TToolType>() instead.")]
public static IMcpServerBuilder WithValueObjectTools(this IMcpServerBuilder builder, IEnumerable<Type> toolTypes, JsonSerializerOptions? serializerOptions = null)
Parameters
builder IMcpServerBuilder
The builder of the server.
toolTypes IEnumerable<Type>
The tool types; a null among them is skipped.
serializerOptions JsonSerializerOptions?
The options the arguments and results are serialized with, or
null for
Returns
IMcpServerBuilder
The builder.
Remarks
A static class, which cannot be a type argument, is registered here.
Exceptions
builder or toolTypes is
null.
WithValueObjectTools(IMcpServerBuilder, IEnumerable<McpServerTool>)
Registers tools as
public static IMcpServerBuilder WithValueObjectTools(this IMcpServerBuilder builder, IEnumerable<McpServerTool> tools)
Parameters
builder IMcpServerBuilder
The builder of the server.
tools IEnumerable<McpServerTool>
The tools; a null among them is skipped.
Returns
IMcpServerBuilder
The builder.
Remarks
A tool WithTools alone,
on a server none of these methods configured, its refused arguments get the SDK's bare error, unless the
server's options are handed to
Exceptions
builder or tools is
null.
WithValueObjectToolsFromAssembly(IMcpServerBuilder, Assembly?, JsonSerializerOptions?)
Registers the
[RequiresUnreferencedCode("Discovers tool methods by reflection, as the SDK's non-generic WithTools and WithToolsFromAssembly do, and might not work in native AOT. Use WithValueObjectTools<TToolType>() instead.")]
public static IMcpServerBuilder WithValueObjectToolsFromAssembly(this IMcpServerBuilder builder, Assembly? toolAssembly = null, JsonSerializerOptions? serializerOptions = null)
Parameters
builder IMcpServerBuilder
The builder of the server.
toolAssembly Assembly?
The assembly, or null for the one that calls this method.
serializerOptions JsonSerializerOptions?
The options the arguments and results are serialized with, or
null for
Returns
IMcpServerBuilder
The builder.
Exceptions
builder is null.