Table of Contents

Class ResultOrAlternate<TResult>

Namespace
ModelContextProtocol.Protocol
Assembly
ModelContextProtocol.Core.dll

Represents the result of a request that may return either the standard result or an alternate Result subtype for scenarios like asynchronous task execution.

[Experimental("MCPEXP002", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#mcpexp002")]
public class ResultOrAlternate<TResult> where TResult : Result

Type Parameters

TResult

The standard result type for the request (e.g., CallToolResult).

Inheritance
ResultOrAlternate<TResult>
Inherited Members

Remarks

Extensions that augment request handling (such as the Tasks extension) use this type to indicate that the server returned an alternate result instead of the normal one. The alternate carries its own JsonTypeInfo so the transport layer can serialize it without compile-time knowledge of the concrete type.

Use IsAlternate to determine which variant was returned, then access either Result for the immediate result or Alternate for the alternate.

Constructors

ResultOrAlternate(TResult)

Initializes a new instance of ResultOrAlternate<TResult> with an immediate result.

public ResultOrAlternate(TResult result)

Parameters

result TResult

The standard result returned by the server.

Properties

Alternate

Gets the alternate result, or null if the server returned the standard result.

public Result? Alternate { get; }

Property Value

Result

AlternateTypeInfo

Gets the JsonTypeInfo for serializing the alternate result, or null if the server returned the standard result.

public JsonTypeInfo? AlternateTypeInfo { get; }

Property Value

JsonTypeInfo

IsAlternate

Gets a value indicating whether the server returned an alternate result instead of the standard result.

public bool IsAlternate { get; }

Property Value

bool

Result

Gets the immediate result, or null if the server returned an alternate.

public TResult? Result { get; }

Property Value

TResult

Methods

FromAlternate<TAlternate>(TAlternate, JsonTypeInfo<TAlternate>)

Creates a ResultOrAlternate<TResult> that carries an alternate Result subtype (for example an InputRequiredResult or a task-creation result) in place of the standard result.

public static ResultOrAlternate<TResult> FromAlternate<TAlternate>(TAlternate alternate, JsonTypeInfo<TAlternate> alternateTypeInfo) where TAlternate : Result

Parameters

alternate TAlternate

The alternate result to return instead of the standard result.

alternateTypeInfo JsonTypeInfo<TAlternate>

The JsonTypeInfo<T> used to serialize alternate. Requiring the strongly-typed contract keeps the alternate value paired with matching serializer metadata rather than an unrelated type.

Returns

ResultOrAlternate<TResult>

A ResultOrAlternate<TResult> that wraps the alternate result.

Type Parameters

TAlternate

The concrete alternate result type.

Operators

implicit operator ResultOrAlternate<TResult>(TResult)

Implicitly converts a TResult to a ResultOrAlternate<TResult> wrapping the immediate result.

public static implicit operator ResultOrAlternate<TResult>(TResult result)

Parameters

result TResult

The result to wrap.

Returns

ResultOrAlternate<TResult>