C# SDK: align mode value and priorityHint with the SEP (#17)

Addresses the first two items of #15 (the chain-orchestrator
multi-server item follows in a separate PR).

## `mode`: keep `"active"`, add `"audit"` round-trip coverage

> **Update:** an earlier revision of this PR renamed `mode` from
`"active"` to `"enforce"`, following a SEP **draft**. The now-current
revised SEP-1763 ([PR
#2624](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2624))
defines `mode?: "active" | "audit"` with default `"active"` — so
`"active"` was correct all along. @jeongukjae flagged this; the rename
has been reverted (commit 58edf2c). The C# SDK's wire value `"active"`
matches the SEP and the TypeScript SDK.

`InterceptorMode` keeps members `Active` (wire `"active"`, default) and
`Audit` (wire `"audit"`). Added a deserialization test alongside the
existing serialization test so both wire forms are covered.

## `priorityHint`: support `number | { request?, response? }`

The SDK only supported a plain `int`. This adds a `PriorityHint`
protocol type whose converter accepts both wire forms and preserves them
on round-trip (a number stays a number, an object stays an object).
Chain ordering now resolves the effective priority per phase exactly as
the SEP's `resolvePriority` algorithm (unset → 0, alphabetical tie-break
unchanged).

- `Interceptor.PriorityHint` is now `PriorityHint?`; an implicit
conversion from `int` keeps existing code compiling.
- `[McpServerInterceptor]` gains `RequestPriorityHint` /
`ResponsePriorityHint` (mutually exclusive with the scalar
`PriorityHint`), following the official SDK's backing-field pattern for
optional attribute values.
- Behavior note: attribute-discovered interceptors with no priority set
now omit `priorityHint` from the wire instead of emitting `0` —
semantically identical per the SEP default, and consistent with the
existing default-skipping for `Mode`/`FailOpen`.

## Testing

`dotnet test` from `csharp/sdk/`: all passing, including new coverage
for both `priorityHint` wire forms, partial objects, unknown-key
tolerance, phase-dependent mutation ordering (the SEP's
pii-redactor/compressor example), the attribute conflict error, and both
`mode` wire values.

Part of #15.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Peder Holdgaard Pedersen 2026-07-21 15:56:41 +02:00 • committed by GitHub
commit 7cf90c9ccb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,6 +29,14 @@ public class TestInterceptors
return new MutationInterceptorResult { Modified = true, Payload = obj };
}
[McpServerInterceptor(Name = "phase-mutator", Type = InterceptorType.Mutation, Events = ["tools/call"], RequestPriorityHint = -50000, ResponsePriorityHint = 50000)]
public static MutationInterceptorResult MutateWithPhasePriorities(JsonNode payload)
=> new() { Modified = false };
[McpServerInterceptor(Name = "conflicting-mutator", Type = InterceptorType.Mutation, Events = ["tools/call"], PriorityHint = -100, RequestPriorityHint = -50000)]
public static MutationInterceptorResult MutateWithConflictingPriorities(JsonNode payload)
=> new() { Modified = false };
[McpServerInterceptor(Name = "sink", Type = InterceptorType.Sink, Events = ["*"])]
public static SinkInterceptorResult Sink(JsonNode payload, InvokeInterceptorContext? context)
{
@ -127,7 +135,8 @@ public class ReflectionMcpServerInterceptorTests
var interceptor = ReflectionMcpServerInterceptor.Create(method, target: null);
Assert.Equal("mutator", interceptor.ProtocolInterceptor.Name);
Assert.Equal(-100, interceptor.ProtocolInterceptor.PriorityHint);
Assert.Equal(-100, interceptor.ProtocolInterceptor.PriorityHint!.GetEffective(InterceptorPhase.Request));
Assert.Equal(-100, interceptor.ProtocolInterceptor.PriorityHint.GetEffective(InterceptorPhase.Response));
var request = new InvokeInterceptorRequestParams
{
@ -183,6 +192,36 @@ public class ReflectionMcpServerInterceptorTests
Assert.True(validation.Valid);
}
[Fact]
public void Create_PerPhasePriorityHints_ProduceObjectFormHint()
{
var method = typeof(TestInterceptors).GetMethod(nameof(TestInterceptors.MutateWithPhasePriorities))!;
var interceptor = ReflectionMcpServerInterceptor.Create(method, target: null);
var hint = interceptor.ProtocolInterceptor.PriorityHint;
Assert.NotNull(hint);
Assert.Equal(-50000, hint!.Request);
Assert.Equal(50000, hint.Response);
Assert.Equal(-50000, hint.GetEffective(InterceptorPhase.Request));
Assert.Equal(50000, hint.GetEffective(InterceptorPhase.Response));
}
[Fact]
public void Create_NoPriorityHint_OmitsHint()
{
var method = typeof(TestInterceptors).GetMethod(nameof(TestInterceptors.ValidateWithBool))!;
var interceptor = ReflectionMcpServerInterceptor.Create(method, target: null);
Assert.Null(interceptor.ProtocolInterceptor.PriorityHint);
}
[Fact]
public void Create_ScalarAndPerPhasePriorityHints_Throws()
{
var method = typeof(TestInterceptors).GetMethod(nameof(TestInterceptors.MutateWithConflictingPriorities))!;
Assert.Throws<InvalidOperationException>(() => ReflectionMcpServerInterceptor.Create(method, target: null));
}
[Fact]
public void Create_MethodWithoutAttribute_Throws()
{