Documentation
¶
Overview ¶
Package gomiddleware provides an optional MCP middleware that automatically executes interceptors for every matching request/response. It uses the chain.Chain from the interceptors/chain package, which invokes interceptors via interceptor/invoke RPCs per the SEP execution model.
The middleware is installed on an mcp.Server via mcp.Server.AddReceivingMiddleware. It marshals request params and response results to json.RawMessage, calls chain.Chain.Execute, and applies any mutated payloads back to the request or response.
Usage ¶
// Create interceptor extension (registers interceptors as resources)
ext := extension.New()
ext.AddInterceptor(myValidator)
ext.AddInterceptor(myMutator)
ext.Install(mcpServer)
// Create chain via in-memory transport
chain, err := ext.LocalChain(ctx, mcpServer)
// Install middleware for automatic execution
mcpServer.AddReceivingMiddleware(
gomiddleware.Middleware(chain,
gomiddleware.WithLogger(logger),
),
)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(ch *chain.Chain, opts ...Option) mcp.Middleware
Middleware creates an mcp.Middleware that intercepts incoming requests and outgoing responses, running the interceptor chain for each.
The middleware calls chain.Execute() which invokes interceptors via interceptor/invoke RPCs. Install it on an mcp.Server via mcp.Server.AddReceivingMiddleware:
chain, _ := srv.LocalChain(ctx)
mcpServer.AddReceivingMiddleware(
gomiddleware.Middleware(chain, gomiddleware.WithLogger(logger)),
)
Types ¶
type ContextProviderFunc ¶
type ContextProviderFunc func(ctx context.Context, req mcp.Request) *interceptors.InvocationContext
ContextProviderFunc extracts an InvocationContext from an incoming MCP request. This is called once per intercepted request and the result is passed to all interceptor handlers via the chain execution params.
Typical use: extract principal identity from OAuth tokens available on the request's session (via RequestExtra.TokenInfo).
type Option ¶
type Option func(*config)
Option configures the middleware.
func WithContextProvider ¶
func WithContextProvider(f ContextProviderFunc) Option
WithContextProvider sets a function that populates InvocationContext for every intercepted request. Without this, the context is nil.
func WithLogger ¶
WithLogger sets the logger for the middleware. If not set, slog.Default() is used.