experimental-ext-interceptors/csharp/sdk
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Peder Holdgaard Pedersen 45f9e0ef27
C# Implementation (#2)
C# implementation of SEP-1763

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 23:53:44 +00:00
..
samples C# Implementation (#2) 2026-01-27 23:53:44 +00:00
src C# Implementation (#2) 2026-01-27 23:53:44 +00:00
tests/ModelContextProtocol.Interceptors.Tests C# Implementation (#2) 2026-01-27 23:53:44 +00:00
AGENTS.md C# Implementation (#2) 2026-01-27 23:53:44 +00:00
CLAUDE.md C# Implementation (#2) 2026-01-27 23:53:44 +00:00
ModelContextProtocol.Interceptors.sln C# Implementation (#2) 2026-01-27 23:53:44 +00:00
README.md C# Implementation (#2) 2026-01-27 23:53:44 +00:00
TODO.md C# Implementation (#2) 2026-01-27 23:53:44 +00:00

MCP Interceptors C# SDK

This library provides interceptor support for the Model Context Protocol (MCP) .NET SDK. Interceptors enable validation, mutation, and observation of MCP messages without modifying the original server or client implementations.

Overview

MCP Interceptors (SEP-1763) allow you to:

  • Validate incoming requests before they reach handlers
  • Mutate requests or responses to transform data
  • Observe message flow for logging, metrics, or auditing

Interceptors can be deployed as:

  • Sidecars alongside MCP servers
  • Gateway services that proxy MCP traffic
  • Embedded validators within applications

Installation

dotnet add package ModelContextProtocol.Interceptors

Quick Start

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Interceptors;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddMcpServer()
    .WithStdioServerTransport()
    .WithInterceptors<ParameterValidator>();

await builder.Build().RunAsync();

Creating an Interceptor

using ModelContextProtocol.Interceptors;
using ModelContextProtocol.Interceptors.Server;
using System.Text.Json.Nodes;

[McpServerInterceptorType]
public class ParameterValidator
{
    [McpServerInterceptor(
        Name = "parameter-validator",
        Description = "Validates tool call parameters",
        Events = new[] { InterceptorEvents.ToolsCall },
        Phase = InterceptorPhase.Request)]
    public ValidationInterceptorResult ValidateToolCall(JsonNode? payload)
    {
        if (payload is null)
        {
            return new ValidationInterceptorResult
            {
                Valid = false,
                Severity = ValidationSeverity.Error,
                Messages = [new() { Message = "Payload is required" }]
            };
        }

        return new ValidationInterceptorResult { Valid = true };
    }
}

Interceptor Types

Validation Interceptors

Validate requests/responses and return pass/fail results with optional error messages.

Mutation Interceptors

Transform request or response payloads before they continue through the pipeline.

Observability Interceptors

Observe message flow for logging, metrics collection, or auditing without modifying data.

Configuration Options

Phase

  • InterceptorPhase.Request - Intercept incoming requests
  • InterceptorPhase.Response - Intercept outgoing responses
  • InterceptorPhase.Both - Intercept both directions

Events

Interceptors can target specific MCP events:

  • InterceptorEvents.ToolsCall - Tool invocation requests
  • InterceptorEvents.PromptGet - Prompt retrieval
  • InterceptorEvents.ResourceRead - Resource access
  • And more...

Priority

Use PriorityHint to control interceptor execution order (lower values run first).

Sample Projects

See the samples/InterceptorServiceSample directory for a complete example of a security-focused validation interceptor.

Requirements

  • .NET 8.0 or later (or .NET Standard 2.0 compatible runtime)
  • ModelContextProtocol SDK 0.1.0-preview.10 or later

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please see the FSIG CONTRIBUTING.md for guidelines.