sdk

module
v0.0.0-...-5bd7ab4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: Apache-2.0

README

MCP Interceptors - Go Implementation

Go implementation of the MCP Interceptor Extension based on SEP-1763.

Note: Currently the MCP SDK is vendored, in-order to add the Protocol Methods needed for interceptors.

Quick Start

mcpServer := mcp.NewServer(&mcp.Implementation{
    Name:    "my-server",
    Version: "0.1.0",
}, nil)

// Create an extension and register interceptors.
ext := extension.New()

// Register a validator that blocks dangerous tool calls.
ext.AddInterceptor(&interceptors.Validator{
    Metadata: interceptors.Metadata{
        Name: "block-dangerous",
        Hook: interceptors.Hook{
            Events: []string{interceptors.EventToolsCall},
            Phase:  interceptors.PhaseRequest,
        },
        Mode: interceptors.ModeEnforce,
    },
    Handler: func(_ context.Context, inv *interceptors.Invocation) (*interceptors.ValidationResult, error) {
        raw := inv.Payload.(json.RawMessage)
        var params struct{ Name string `json:"name"` }
        json.Unmarshal(raw, &params)
        // validate the request...
        return &interceptors.ValidationResult{Valid: true}, nil
    },
})

// Install on the server and create a chain for middleware.
ext.Install(mcpServer)
chain, err := ext.LocalChain(ctx, mcpServer)
mcpServer.AddReceivingMiddleware(gomiddleware.Middleware(chain))

mcpServer.Run(context.Background(), &mcp.StdioTransport{})

See examples/ for complete working examples.

Documentation

  • DESIGN.md — architecture, execution model, integration with the go-sdk.
  • PERFORMANCE.md — per-request cost model, allocation summary, and optimization notes.
  • CONFORMANCE.md — SEP conformance status.

Package API documentation is available via go doc:

go doc github.com/modelcontextprotocol/ext-interceptors/go/sdk/interceptors

Directories

Path Synopsis
examples
mutator command
Example: a simple mutator interceptor that adds an "[audited]" prefix to every tool call result's text content.
Example: a simple mutator interceptor that adds an "[audited]" prefix to every tool call result's text content.
validator command
Example: a simple validator interceptor that rejects tool calls to a tool named "dangerous_tool".
Example: a simple validator interceptor that rejects tool calls to a tool named "dangerous_tool".
Package interceptors defines the core types for the MCP interceptor framework: interceptor descriptors (Validator, Mutator), wire protocol types (InvokeParams, InvokeResult), result types (ValidationResult, MutationResult), and supporting enums.
Package interceptors defines the core types for the MCP interceptor framework: interceptor descriptors (Validator, Mutator), wire protocol types (InvokeParams, InvokeResult), result types (ValidationResult, MutationResult), and supporting enums.
chain
Package chain provides the SEP-compliant interceptor chain orchestrator.
Package chain provides the SEP-compliant interceptor chain orchestrator.
extension
Package extension registers interceptors as first-class MCP primitives.
Package extension registers interceptors as first-class MCP primitives.
integrations/gomiddleware
Package gomiddleware provides an optional MCP middleware that automatically executes interceptors for every matching request/response.
Package gomiddleware provides an optional MCP middleware that automatically executes interceptors for every matching request/response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL