Path: redb-route/tests/redb.Route.Tests.Llm.Mcp/Governance/McpSafetyUnitTests.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/tests/redb.Route.Tests.Llm.Mcp/Governance/McpSafetyUnitTests.cs
----
using System.Text.RegularExpressions;
namespace redb.Route.Tests.Llm.Mcp.Governance;
///
/// Unit-level locks for the MCP safety surface. There is no local fake MCP server in this
/// repository (only live Serena tests under SerenaFact), so MCP behaviour is pinned where it can
/// be pinned without a server: on the descriptor, the override matcher and the configuration defaults.
///
[Trait("Category", "Governance")]
public sealed class McpSafetyUnitTests
{
private static McpSafetyOverride Override(string pattern) => new()
{
ToolNamePattern = pattern,
Safety = new LlmToolSafety { RequiresApproval = true, SideEffect = ToolSideEffect.External }
};
///
/// A trap: McpSafetyOverride.ToolNamePattern
/// is matched against the raw server-side tool name, while the operator and the model see the
/// sanitized {server}__{tool} form. Copying the model-facing name into the pattern therefore
/// matches nothing — silently, falling back to DefaultSafety.
///
[Fact]
public void McpSafetyOverride_ModelFacingPattern_DoesNotMatchRawName()
{
var modelFacing = McpToolDescriptor.BuildModelFacingName("serena", "find_symbol");
modelFacing.Should().Be("serena__find_symbol", "the model-facing form is server__tool");
var overridden = Override("^" + Regex.Escape(modelFacing) + "$");
overridden.Matches("find_symbol").Should().BeFalse(
"matching runs against the raw name, so a pattern built from the model-facing name silently misses");
}
///
/// The same trap, made loud: discovery reports every override that matched no discovered tool, so an
/// operator sees the miss instead of quietly running on DefaultSafety.
///
[Fact]
public void McpSafetyOverride_ModelFacingPattern_IsReportedAsUnmatched()
{
var server = new McpServerOptions
{
Name = "serena",
Transport = McpTransport.Stdio("mcp-server"),
SafetyOverrides = [Override("^serena__find_symbol$")]
};
var tools = new List { new() { Name = "find_symbol" } };
McpDiscoveryService.FindUnmatchedOverrides(server, tools).Should().ContainSingle(
"a pattern that matched nothing must be reported, not left for the operator to discover in production");
}
/// A pattern that does match the raw name is, of course, not reported.
[Fact]
public void McpSafetyOverride_RawNamePattern_IsNotReported()
{
var server = new McpServerOptions
{
Name = "serena",
Transport = McpTransport.Stdio("mcp-server"),
SafetyOverrides = [Override("^find_symbol$")]
};
var tools = new List { new() { Name = "find_symbol" } };
McpDiscoveryService.FindUnmatchedOverrides(server, tools).Should().BeEmpty();
}
///
/// A typo in a pattern used to surface only during discovery, where the exception turned into
/// "the server is silently absent from the tool set". Compiling it at build time makes the typo
/// fail where it is written.
///
[Fact]
public void McpSafetyOverride_InvalidRegex_FailsAtConstruction()
{
var act = () => Override("^find_symbol(");
act.Should().Throw()
.WithMessage("*not a valid regex*",
"the message must say which pattern is broken, not only that something is wrong");
}
///
/// Wave 6 deliberately flips the discovered-tool default: a third-party tool is unknown, so it is
/// declared external and approval-required. Approval only becomes a control point with a gate
/// other than the shipped — this test pins the declaration, and
/// ApprovalDefaultsTests.AddRedbRouteLlm_ResolvesAutoApproveGateByDefault pins what that
/// declaration currently buys.
///
[Fact]
public void McpServerOptions_DefaultSafety_RequiresApproval()
{
var options = new McpServerOptions
{
Name = "srv",
Transport = McpTransport.Stdio("mcp-server")
};
options.DefaultSafety.SideEffect.Should().Be(ToolSideEffect.External);
options.DefaultSafety.RequiresApproval.Should().BeTrue(
"discovery cannot know what a third-party tool does, so the safe default is the declared one");
}
///
/// A caching policy on a discovered tool that is not read-only is refused when the server options are
/// built — at configuration time, where the operator can still fix it, instead of during discovery,
/// where it would read as "this server is silently missing from the tool set".
///
[Fact]
public void McpServerOptions_CachingOnNonReadOnlySafety_FailsAtConfiguration()
{
var builder = new McpServerOptionsBuilder
{
Name = "srv",
Transport = McpTransport.Stdio("mcp-server"),
DefaultSafety = new LlmToolSafety
{
SideEffect = ToolSideEffect.External,
Caching = ToolCachingPolicy.Persist
}
};
var act = () => builder.Build();
act.Should().Throw()
.Which.Message.Should().Contain("Only read-only tools may be cached");
}
}