Path: redb-route/redb.Route.Llm.Mcp/Protocol/McpProtocol.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm.Mcp/Protocol/McpProtocol.cs
----
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace redb.Route.Llm.Mcp.Protocol;
///
/// MCP protocol DTOs and JSON-RPC envelope types. Pinned to MCP protocol version
/// 2024-11-05 — the dialect Serena, Claude Desktop, and the official Anthropic
/// servers all speak.
///
public static class McpProtocol
{
/// The MCP protocol version this client advertises during initialize.
public const string ProtocolVersion = "2024-11-05";
/// JSON-RPC 2.0 protocol identifier.
public const string JsonRpcVersion = "2.0";
/// Shared JSON serialization options. matches MCP-server expectations (no nulls on the wire).
public static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = false,
// UTF-8 on the wire, not \uXXXX — non-ASCII tool args / results stay readable.
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
}
/// JSON-RPC 2.0 request envelope.
public sealed class JsonRpcRequest
{
/// Always "2.0".
[JsonPropertyName("jsonrpc")]
public string JsonRpc { get; set; } = McpProtocol.JsonRpcVersion;
/// Correlation id matching the eventual .
[JsonPropertyName("id")]
public long Id { get; set; }
/// RPC method name (e.g. "tools/call").
[JsonPropertyName("method")]
public string Method { get; set; } = "";
/// Method-specific parameters as a JSON object.
[JsonPropertyName("params")]
public JsonNode? Params { get; set; }
}
/// JSON-RPC 2.0 response envelope.
public sealed class JsonRpcResponse
{
/// Always "2.0".
[JsonPropertyName("jsonrpc")]
public string JsonRpc { get; set; } = McpProtocol.JsonRpcVersion;
/// Correlation id matching the originating .
[JsonPropertyName("id")]
public long? Id { get; set; }
/// Successful method result. Mutually exclusive with .
[JsonPropertyName("result")]
public JsonNode? Result { get; set; }
/// Error envelope when the call failed.
[JsonPropertyName("error")]
public JsonRpcError? Error { get; set; }
}
/// JSON-RPC error object.
public sealed class JsonRpcError
{
/// Numeric error code per JSON-RPC convention.
[JsonPropertyName("code")]
public int Code { get; set; }
/// Human-readable error message.
[JsonPropertyName("message")]
public string Message { get; set; } = "";
/// Optional structured error data.
[JsonPropertyName("data")]
public JsonNode? Data { get; set; }
}
/// JSON-RPC 2.0 server-initiated notification (no response expected).
public sealed class JsonRpcNotification
{
/// Always "2.0".
[JsonPropertyName("jsonrpc")]
public string JsonRpc { get; set; } = McpProtocol.JsonRpcVersion;
/// Notification method name (e.g. "notifications/tools/list_changed").
[JsonPropertyName("method")]
public string Method { get; set; } = "";
/// Notification payload.
[JsonPropertyName("params")]
public JsonNode? Params { get; set; }
}
/// Result of the initialize handshake.
public sealed class InitializeResult
{
/// Protocol version the server has agreed to speak.
[JsonPropertyName("protocolVersion")]
public string ProtocolVersion { get; set; } = "";
/// Server identification (name/version).
[JsonPropertyName("serverInfo")]
public ServerInfo? ServerInfo { get; set; }
/// Server capability flags (verbatim — we don't interpret beyond logging).
[JsonPropertyName("capabilities")]
public JsonNode? Capabilities { get; set; }
}
/// Server identification block returned during initialize.
public sealed class ServerInfo
{
/// Server name (free-form, e.g. "serena").
[JsonPropertyName("name")]
public string Name { get; set; } = "";
/// Server version string.
[JsonPropertyName("version")]
public string Version { get; set; } = "";
}
/// One tool entry as returned by tools/list.
public sealed class ToolDefinition
{
/// Server-side tool name (raw, before redb sanitisation).
[JsonPropertyName("name")]
public string Name { get; set; } = "";
/// Human-readable description shown to the model.
[JsonPropertyName("description")]
public string? Description { get; set; }
/// JSON Schema for the tool's input arguments.
[JsonPropertyName("inputSchema")]
public JsonNode? InputSchema { get; set; }
}
/// Result of tools/list.
public sealed class ListToolsResult
{
/// The list of tools the server exposes.
[JsonPropertyName("tools")]
public List Tools { get; set; } = [];
}
/// Result of tools/call.
public sealed class CallToolResult
{
/// Content blocks produced by the tool (text, images, embedded resources).
[JsonPropertyName("content")]
public JsonNode? Content { get; set; }
/// True when the tool reported an error condition.
[JsonPropertyName("isError")]
public bool IsError { get; set; }
}