using System.Text.Json.Serialization;
namespace ColaFlow.Modules.Mcp.Contracts.JsonRpc;
///
/// JSON-RPC 2.0 response object
///
public class JsonRpcResponse
{
///
/// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0"
///
[JsonPropertyName("jsonrpc")]
public string JsonRpc { get; set; } = "2.0";
///
/// This member is REQUIRED on success. Must not exist if there was an error
///
[JsonPropertyName("result")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? Result { get; set; }
///
/// This member is REQUIRED on error. Must not exist if there was no error
///
[JsonPropertyName("error")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public JsonRpcError? Error { get; set; }
///
/// This member is REQUIRED. It MUST be the same as the value of the id member in the Request Object.
/// If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it MUST be Null.
///
[JsonPropertyName("id")]
public object? Id { get; set; }
///
/// Creates a success response
///
public static JsonRpcResponse Success(object? result, object? id)
{
return new JsonRpcResponse
{
Result = result,
Id = id
};
}
///
/// Creates an error response
///
public static JsonRpcResponse CreateError(JsonRpcError error, object? id)
{
return new JsonRpcResponse
{
Error = error,
Id = id
};
}
///
/// Creates a ParseError response (id is null because request couldn't be parsed)
///
public static JsonRpcResponse ParseError(string? details = null)
{
return CreateError(JsonRpcError.ParseError(details), null);
}
///
/// Creates an InvalidRequest response
///
public static JsonRpcResponse InvalidRequest(string? details = null, object? id = null)
{
return CreateError(JsonRpcError.InvalidRequest(details), id);
}
///
/// Creates a MethodNotFound response
///
public static JsonRpcResponse MethodNotFound(string method, object? id)
{
return CreateError(JsonRpcError.MethodNotFound(method), id);
}
///
/// Creates an InvalidParams response
///
public static JsonRpcResponse InvalidParams(string? details, object? id)
{
return CreateError(JsonRpcError.InvalidParams(details), id);
}
///
/// Creates an InternalError response
///
public static JsonRpcResponse InternalError(string? details, object? id)
{
return CreateError(JsonRpcError.InternalError(details), id);
}
}