Skip to main content

Definition

Assembly: Microsoft.OData.Mcp.Core.dll Namespace: Microsoft.OData.Mcp.Core.Tools Inheritance: System.Object

Syntax

Microsoft.OData.Mcp.Core.Tools.McpToolResult

Summary

Represents the result of an MCP tool execution.

Remarks

This class standardizes the format of tool execution results, providing consistent error handling, data formatting, and metadata across all tools.

Constructors

.ctor

Initializes a new instance of the McpToolResult class.

Syntax

public McpToolResult()

.ctor

Initializes a new instance of the McpToolResult class with success status.

Syntax

public McpToolResult(System.Text.Json.JsonDocument data, string correlationId = null)

Parameters

NameTypeDescription
dataSystem.Text.Json.JsonDocument?The result data.
correlationIdstring?The correlation ID.

.ctor

Initializes a new instance of the McpToolResult class with error status.

Syntax

public McpToolResult(string errorMessage, string errorCode = null, string correlationId = null)

Parameters

NameTypeDescription
errorMessagestringThe error message.
errorCodestring?The error code.
correlationIdstring?The correlation ID.

.ctor Inherited

Inherited from object

Syntax

public Object()

Properties

CompletedAt

Gets or sets the timestamp when the execution completed.

Syntax

public System.DateTime CompletedAt { get; set; }

Property Value

Type: System.DateTime? The UTC timestamp when the tool finished executing.

Remarks

This timestamp can be used for auditing and troubleshooting purposes.

CorrelationId

Gets or sets the correlation ID for this execution.

Syntax

public string CorrelationId { get; set; }

Property Value

Type: string? The correlation ID that can be used to trace this execution across systems.

Remarks

This should match the correlation ID from the tool context for end-to-end tracing.

Data

Gets or sets the result data as a JSON document.

Syntax

public System.Text.Json.JsonDocument Data { get; set; }

Property Value

Type: System.Text.Json.JsonDocument? The structured result data, or null if no data is available.

Remarks

The data format depends on the specific tool but should always be valid JSON that can be consumed by MCP clients.

ErrorCode

Gets or sets the error code for failed executions.

Syntax

public string ErrorCode { get; set; }

Property Value

Type: string? A machine-readable error code, or null if execution was successful.

Remarks

Error codes provide a standardized way for clients to handle specific error conditions programmatically.

ErrorMessage

Gets or sets the error message for failed executions.

Syntax

public string ErrorMessage { get; set; }

Property Value

Type: string? A human-readable error message, or null if execution was successful.

Remarks

Error messages should be clear and actionable, helping users understand what went wrong and how to fix it.

ExecutionDuration

Gets or sets the execution duration.

Syntax

public System.TimeSpan ExecutionDuration { get; set; }

Property Value

Type: System.TimeSpan? The time taken to execute the tool.

Remarks

This information is useful for performance monitoring and optimization.

IsSuccess

Gets or sets a value indicating whether the tool execution was successful.

Syntax

public bool IsSuccess { get; set; }

Property Value

Type: bool true if the execution was successful; otherwise, false.

Remarks

Success indicates that the tool executed without errors and completed its intended operation, regardless of whether data was found or modified.

Metadata

Gets or sets additional metadata about the execution.

Syntax

public System.Collections.Generic.Dictionary<string, object> Metadata { get; set; }

Property Value

Type: System.Collections.Generic.Dictionary<string, object> A dictionary of metadata key-value pairs.

Remarks

Metadata can include information such as execution time, record counts, caching status, or other operational details.

StatusCode

Gets or sets the HTTP status code equivalent for this result.

Syntax

public int StatusCode { get; set; }

Property Value

Type: int The HTTP status code that best represents this result.

Remarks

This helps clients understand the result in terms of standard HTTP semantics, even when the tool isn’t directly related to HTTP operations.

Warnings

Gets or sets warnings that occurred during execution.

Syntax

public System.Collections.Generic.List<string> Warnings { get; set; }

Property Value

Type: System.Collections.Generic.List<string> A collection of warning messages.

Remarks

Warnings indicate potential issues or notable conditions that didn’t prevent successful execution but may be of interest to users.

Methods

AddMetadata

Adds metadata to the result.

Syntax

public void AddMetadata(string key, object value)

Parameters

NameTypeDescription
keystringThe metadata key.
valueobjectThe metadata value.

Exceptions

ExceptionDescription
ArgumentExceptionThrown when key is null or whitespace.

AddWarning

Adds a warning to the result.

Syntax

public void AddWarning(string warning)

Parameters

NameTypeDescription
warningstringThe warning message.

Exceptions

ExceptionDescription
ArgumentExceptionThrown when warning is null or whitespace.

Equals Inherited Virtual

Inherited from object

Syntax

public virtual bool Equals(object obj)

Parameters

NameTypeDescription
objobject?-

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

public static bool Equals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

Error

Creates an error result with the specified message and code.

Syntax

public static Microsoft.OData.Mcp.Core.Tools.McpToolResult Error(string errorMessage, string errorCode = null, string correlationId = null)

Parameters

NameTypeDescription
errorMessagestringThe error message.
errorCodestring?The error code.
correlationIdstring?The correlation ID.

Returns

Type: Microsoft.OData.Mcp.Core.Tools.McpToolResult An error MCP tool result.

Exceptions

ExceptionDescription
ArgumentExceptionThrown when errorMessage is null or whitespace.

Error

Creates an error result from an exception.

Syntax

public static Microsoft.OData.Mcp.Core.Tools.McpToolResult Error(System.Exception exception, string correlationId = null)

Parameters

NameTypeDescription
exceptionSystem.ExceptionThe exception that occurred.
correlationIdstring?The correlation ID.

Returns

Type: Microsoft.OData.Mcp.Core.Tools.McpToolResult An error MCP tool result.

Exceptions

ExceptionDescription
ArgumentNullExceptionThrown when exception is null.

GetHashCode Inherited Virtual

Inherited from object

Syntax

public virtual int GetHashCode()

Returns

Type: int

GetMetadata

Gets metadata value by key.

Syntax

public T GetMetadata<T>(string key)

Parameters

NameTypeDescription
keystringThe metadata key.

Returns

Type: T? The metadata value if found and of the correct type; otherwise, the default value.

Type Parameters

  • T - The type of the metadata value.

GetType Inherited

Inherited from object

Syntax

public System.Type GetType()

Returns

Type: System.Type

MemberwiseClone Inherited

Inherited from object

Syntax

protected internal object MemberwiseClone()

Returns

Type: object

NotFound

Creates a not found result.

Syntax

public static Microsoft.OData.Mcp.Core.Tools.McpToolResult NotFound(string message = null, string correlationId = null)

Parameters

NameTypeDescription
messagestring?The not found message.
correlationIdstring?The correlation ID.

Returns

Type: Microsoft.OData.Mcp.Core.Tools.McpToolResult A not found MCP tool result.

ReferenceEquals Inherited

Inherited from object

Syntax

public static bool ReferenceEquals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

SetExecutionDuration

Sets the execution duration based on a start time.

Syntax

public void SetExecutionDuration(System.DateTime startTime)

Parameters

NameTypeDescription
startTimeSystem.DateTime?The execution start time.

Success

Creates a successful result with the specified data.

Syntax

public static Microsoft.OData.Mcp.Core.Tools.McpToolResult Success(System.Text.Json.JsonDocument data = null, string correlationId = null)

Parameters

NameTypeDescription
dataSystem.Text.Json.JsonDocument?The result data.
correlationIdstring?The correlation ID.

Returns

Type: Microsoft.OData.Mcp.Core.Tools.McpToolResult A successful MCP tool result.

Success

Creates a successful result with the specified object data.

Syntax

public static Microsoft.OData.Mcp.Core.Tools.McpToolResult Success(object data, string correlationId = null)

Parameters

NameTypeDescription
dataobjectThe data object to serialize to JSON.
correlationIdstring?The correlation ID.

Returns

Type: Microsoft.OData.Mcp.Core.Tools.McpToolResult A successful MCP tool result.

Exceptions

ExceptionDescription
ArgumentNullExceptionThrown when data is null.

ToDictionary

Converts the result to a dictionary for JSON serialization.

Syntax

public System.Collections.Generic.Dictionary<string, object> ToDictionary()

Returns

Type: System.Collections.Generic.Dictionary<string, object?> A dictionary representation of the result.

ToString Override

Returns a JSON string representation of the result.

Syntax

public override string ToString()

Returns

Type: string A JSON string representation of the result.

ToString Inherited Virtual

Inherited from object

Syntax

public virtual string ToString()

Returns

Type: string?

Unauthorized

Creates an unauthorized result.

Syntax

public static Microsoft.OData.Mcp.Core.Tools.McpToolResult Unauthorized(string message = null, string correlationId = null)

Parameters

NameTypeDescription
messagestring?The unauthorized message.
correlationIdstring?The correlation ID.

Returns

Type: Microsoft.OData.Mcp.Core.Tools.McpToolResult An unauthorized MCP tool result.

ValidationError

Creates a validation error result.

Syntax

public static Microsoft.OData.Mcp.Core.Tools.McpToolResult ValidationError(string message, string correlationId = null)

Parameters

NameTypeDescription
messagestringThe validation error message.
correlationIdstring?The correlation ID.

Returns

Type: Microsoft.OData.Mcp.Core.Tools.McpToolResult A validation error MCP tool result.