Path: redb-route/redb.Route.Llm.Mcp/McpToolDescriptor.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/McpToolDescriptor.cs ---- using System.Text.RegularExpressions; using redb.Route.Abstractions; using redb.Route.Llm.Abstractions.Tools; using redb.Route.Llm.Mcp.Protocol; namespace redb.Route.Llm.Mcp; /// /// backed by a remote MCP tool. Capability built once /// from tools/list metadata; returns the /// precomputed mcp://server/tool URI so dispatch is constant-time. /// public sealed class McpToolDescriptor : ILlmToolDescriptor { /// Maximum length of an LLM-facing tool name (Anthropic / OpenAI cap). public const int MaxToolNameLength = 64; /// Maximum length of the server-prefix segment. public const int MaxServerPrefixLength = 24; /// Maximum length of the tool-name segment. public const int MaxToolPartLength = 36; /// Separator between sanitized server prefix and tool name. public const string Separator = "__"; private static readonly Regex InvalidChars = new("[^a-zA-Z0-9_]", RegexOptions.Compiled); private static readonly Regex StartsWithDigit = new("^[0-9]", RegexOptions.Compiled); /// public LlmToolCapability Capability { get; } /// The raw server-side tool name (as returned by tools/list). public string RawToolName { get; } /// Logical MCP server name. public string ServerName { get; } /// Precomputed mcp://server/tool dispatch URI. public string EndpointUri { get; } /// Creates a new descriptor — usually built by . /// Logical server name. /// Raw server-side tool name. /// Capability projected to the model. public McpToolDescriptor(string serverName, string rawToolName, LlmToolCapability capability) { ArgumentException.ThrowIfNullOrWhiteSpace(serverName); ArgumentException.ThrowIfNullOrWhiteSpace(rawToolName); ArgumentNullException.ThrowIfNull(capability); ServerName = serverName; RawToolName = rawToolName; Capability = capability; EndpointUri = $"mcp://{serverName}/{rawToolName}"; } /// public string BuildEndpointUri(string inputJson, IExchange parentExchange) => EndpointUri; /// /// Builds an LLM-facing tool name {server}{Separator}{tool} truncated to fit /// the 64-char model limit, with non-alphanumerics replaced by _. /// /// Logical server name. /// Raw tool name. /// Sanitized model-facing name (≤ ). public static string BuildModelFacingName(string serverName, string toolName) { var server = Sanitize(serverName, MaxServerPrefixLength); var tool = Sanitize(toolName, MaxToolPartLength); var combined = $"{server}{Separator}{tool}"; if (combined.Length > MaxToolNameLength) combined = combined[..MaxToolNameLength]; if (StartsWithDigit.IsMatch(combined)) combined = "_" + combined[..(MaxToolNameLength - 1)]; return combined; } /// Builds the JSON-Schema string for a descriptor — falls back to a permissive object when missing. /// Tool definition received from tools/list. public static string BuildInputSchema(ToolDefinition definition) { ArgumentNullException.ThrowIfNull(definition); return definition.InputSchema?.ToJsonString() ?? """{"type":"object","additionalProperties":true}"""; } private static string Sanitize(string value, int maxLength) { var cleaned = InvalidChars.Replace(value, "_"); if (cleaned.Length > maxLength) cleaned = cleaned[..maxLength]; return cleaned; } }