Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for the application.
Index ¶
- Constants
- func AuthMiddleware(authManager *auth.Manager) mcp.Middleware
- func CORSMiddleware() mcp.Middleware
- func DebugMiddleware() mcp.Middleware
- func LoggingMiddleware(log *slog.Logger) mcp.Middleware
- func SetRedisClientCreatorForTests(creator func(opts *redis.Options) *redis.Client)
- func SetTimeNowForTests(nowFunc func() time.Time)
- type AuditEntry
- type AuditMiddleware
- type CachingMiddleware
- type CallPolicyMiddleware
- type IPAllowlistMiddleware
- type Limiter
- type LocalLimiter
- type RateLimitMiddleware
- type RedisLimiter
Constants ¶
const RedisRateLimitScript = `` /* 996-byte string literal not displayed */
RedisRateLimitScript is the Lua script used for rate limiting.
Variables ¶
This section is empty.
Functions ¶
func AuthMiddleware ¶
func AuthMiddleware(authManager *auth.Manager) mcp.Middleware
AuthMiddleware creates an MCP middleware for handling authentication. It is intended to inspect incoming requests and use the provided `AuthManager` to verify credentials before passing the request to the next handler.
Parameters:
- authManager: The authentication manager to be used for authenticating requests.
Returns an `mcp.Middleware` function.
func CORSMiddleware ¶
func CORSMiddleware() mcp.Middleware
CORSMiddleware creates an MCP middleware for handling Cross-Origin Resource Sharing (CORS). It is intended to add the necessary CORS headers to outgoing responses, allowing web browsers to securely make cross-origin requests to the MCP server.
NOTE: This middleware is currently a placeholder and does not yet add any CORS headers. It passes all requests and responses through without modification.
Returns an `mcp.Middleware` function.
func DebugMiddleware ¶
func DebugMiddleware() mcp.Middleware
DebugMiddleware returns a middleware function that logs the full request and response of each MCP method call. This is useful for debugging and understanding the flow of data through the server.
func LoggingMiddleware ¶
func LoggingMiddleware(log *slog.Logger) mcp.Middleware
LoggingMiddleware creates an MCP middleware that logs information about each incoming request. It records the start and completion of each request, including the duration of the handling.
This is useful for debugging and monitoring the flow of requests through the server.
Parameters:
- log: The logger to be used. If `nil`, the default global logger will be used.
Returns an `mcp.Middleware` function.
func SetRedisClientCreatorForTests ¶ added in v0.0.2
SetRedisClientCreatorForTests sets the redis client creator for tests.
func SetTimeNowForTests ¶ added in v0.0.2
SetTimeNowForTests sets the time.Now function for tests.
Types ¶
type AuditEntry ¶ added in v0.0.2
type AuditEntry struct {
Timestamp time.Time `json:"timestamp"`
ToolName string `json:"tool_name"`
UserID string `json:"user_id,omitempty"`
ProfileID string `json:"profile_id,omitempty"`
Arguments json.RawMessage `json:"arguments,omitempty"`
Result any `json:"result,omitempty"`
Error string `json:"error,omitempty"`
Duration string `json:"duration"`
DurationMs int64 `json:"duration_ms"`
}
AuditEntry represents a single audit log entry.
type AuditMiddleware ¶ added in v0.0.2
type AuditMiddleware struct {
// contains filtered or unexported fields
}
AuditMiddleware provides audit logging for tool executions.
func NewAuditMiddleware ¶ added in v0.0.2
func NewAuditMiddleware(config *configv1.AuditConfig) (*AuditMiddleware, error)
NewAuditMiddleware creates a new AuditMiddleware.
func (*AuditMiddleware) Close ¶ added in v0.0.2
func (m *AuditMiddleware) Close() error
Close closes the underlying file.
func (*AuditMiddleware) Execute ¶ added in v0.0.2
func (m *AuditMiddleware) Execute(ctx context.Context, req *tool.ExecutionRequest, next tool.ExecutionFunc) (any, error)
Execute intercepts tool execution to log audit events.
type CachingMiddleware ¶
type CachingMiddleware struct {
// contains filtered or unexported fields
}
CachingMiddleware is a tool execution middleware that provides caching functionality.
func NewCachingMiddleware ¶
func NewCachingMiddleware(toolManager tool.ManagerInterface) *CachingMiddleware
NewCachingMiddleware creates a new CachingMiddleware.
func (*CachingMiddleware) Clear ¶ added in v0.0.2
func (m *CachingMiddleware) Clear(ctx context.Context) error
Clear clears the cache.
func (*CachingMiddleware) Execute ¶
func (m *CachingMiddleware) Execute(ctx context.Context, req *tool.ExecutionRequest, next tool.ExecutionFunc) (any, error)
Execute executes the caching middleware.
type CallPolicyMiddleware ¶ added in v0.0.2
type CallPolicyMiddleware struct {
// contains filtered or unexported fields
}
CallPolicyMiddleware is a middleware that enforces call policies (allow/deny) based on tool name and arguments.
func NewCallPolicyMiddleware ¶ added in v0.0.2
func NewCallPolicyMiddleware(toolManager tool.ManagerInterface) *CallPolicyMiddleware
NewCallPolicyMiddleware creates a new CallPolicyMiddleware.
func (*CallPolicyMiddleware) Execute ¶ added in v0.0.2
func (m *CallPolicyMiddleware) Execute(ctx context.Context, req *tool.ExecutionRequest, next tool.ExecutionFunc) (any, error)
Execute enforces call policies before proceeding to the next handler.
type IPAllowlistMiddleware ¶
type IPAllowlistMiddleware struct {
// contains filtered or unexported fields
}
IPAllowlistMiddleware restricts access to allowed IP addresses.
func NewIPAllowlistMiddleware ¶
func NewIPAllowlistMiddleware(allowedCIDRs []string) (*IPAllowlistMiddleware, error)
NewIPAllowlistMiddleware creates a new IPAllowlistMiddleware.
func (*IPAllowlistMiddleware) Allow ¶
func (m *IPAllowlistMiddleware) Allow(remoteAddr string) bool
Allow checks if the given remote address is allowed. remoteAddr should be in the form "IP" or "IP:Port".
type Limiter ¶ added in v0.0.2
type Limiter interface {
// Allow checks if the request is allowed.
Allow(ctx context.Context) (bool, error)
// Update updates the limiter configuration.
Update(rps float64, burst int)
}
Limiter interface defines the methods required for a rate limiter.
type LocalLimiter ¶ added in v0.0.2
LocalLimiter is an in-memory implementation of Limiter.
func (*LocalLimiter) Allow ¶ added in v0.0.2
func (l *LocalLimiter) Allow(ctx context.Context) (bool, error)
Allow checks if the request is allowed.
func (*LocalLimiter) Update ¶ added in v0.0.2
func (l *LocalLimiter) Update(rps float64, burst int)
Update updates the limiter configuration.
type RateLimitMiddleware ¶
type RateLimitMiddleware struct {
// contains filtered or unexported fields
}
RateLimitMiddleware is a tool execution middleware that provides rate limiting functionality for upstream services.
func NewRateLimitMiddleware ¶
func NewRateLimitMiddleware(toolManager tool.ManagerInterface) *RateLimitMiddleware
NewRateLimitMiddleware creates a new RateLimitMiddleware.
func (*RateLimitMiddleware) Execute ¶
func (m *RateLimitMiddleware) Execute(ctx context.Context, req *tool.ExecutionRequest, next tool.ExecutionFunc) (any, error)
Execute executes the rate limiting middleware.
type RedisLimiter ¶ added in v0.0.2
type RedisLimiter struct {
// contains filtered or unexported fields
}
RedisLimiter implements rate limiting using Redis.
func NewRedisLimiter ¶ added in v0.0.2
func NewRedisLimiter(serviceID string, config *configv1.RateLimitConfig) (*RedisLimiter, error)
NewRedisLimiter creates a new RedisLimiter.
func (*RedisLimiter) Allow ¶ added in v0.0.2
func (l *RedisLimiter) Allow(ctx context.Context) (bool, error)
Allow checks if the request is allowed.
func (*RedisLimiter) Close ¶ added in v0.0.2
func (l *RedisLimiter) Close() error
Close closes the Redis client.
func (*RedisLimiter) Update ¶ added in v0.0.2
func (l *RedisLimiter) Update(rps float64, burst int)
Update updates the limiter configuration.