1fad6c54ab
* add readonly and toolset support * address feedback * forgotten files * remove redundant checks in WithRequestConfig * move middleware in RegisterRoutes * improve comment and add TestParseCommaSeparated * fix broken TestInventoryFiltersForRequest * parse X-MCP-Tools into ctx * clean up TestInventoryFiltersForRequest * review http args and add lockdown ctx helpers * parse lockdown header and update GetFlags to retrieve ff from ctx * clean up and fix tests * fix GetFlags check, move lockdown parsing in WithRequestConfig and fix broken tests
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package context
|
|
|
|
import "context"
|
|
|
|
// readonlyCtxKey is a context key for read-only mode
|
|
type readonlyCtxKey struct{}
|
|
|
|
// WithReadonly adds read-only mode state to the context
|
|
func WithReadonly(ctx context.Context, enabled bool) context.Context {
|
|
return context.WithValue(ctx, readonlyCtxKey{}, enabled)
|
|
}
|
|
|
|
// IsReadonly retrieves the read-only mode state from the context
|
|
func IsReadonly(ctx context.Context) bool {
|
|
if enabled, ok := ctx.Value(readonlyCtxKey{}).(bool); ok {
|
|
return enabled
|
|
}
|
|
return false
|
|
}
|
|
|
|
// toolsetsCtxKey is a context key for the active toolsets
|
|
type toolsetsCtxKey struct{}
|
|
|
|
// WithToolsets adds the active toolsets to the context
|
|
func WithToolsets(ctx context.Context, toolsets []string) context.Context {
|
|
return context.WithValue(ctx, toolsetsCtxKey{}, toolsets)
|
|
}
|
|
|
|
// GetToolsets retrieves the active toolsets from the context
|
|
func GetToolsets(ctx context.Context) []string {
|
|
if toolsets, ok := ctx.Value(toolsetsCtxKey{}).([]string); ok {
|
|
return toolsets
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// toolsCtxKey is a context key for tools
|
|
type toolsCtxKey struct{}
|
|
|
|
// WithTools adds the tools to the context
|
|
func WithTools(ctx context.Context, tools []string) context.Context {
|
|
return context.WithValue(ctx, toolsCtxKey{}, tools)
|
|
}
|
|
|
|
// GetTools retrieves the tools from the context
|
|
func GetTools(ctx context.Context) []string {
|
|
if tools, ok := ctx.Value(toolsCtxKey{}).([]string); ok {
|
|
return tools
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lockdownCtxKey is a context key for lockdown mode
|
|
type lockdownCtxKey struct{}
|
|
|
|
// WithLockdownMode adds lockdown mode state to the context
|
|
func WithLockdownMode(ctx context.Context, enabled bool) context.Context {
|
|
return context.WithValue(ctx, lockdownCtxKey{}, enabled)
|
|
}
|
|
|
|
// IsLockdownMode retrieves the lockdown mode state from the context
|
|
func IsLockdownMode(ctx context.Context) bool {
|
|
if enabled, ok := ctx.Value(lockdownCtxKey{}).(bool); ok {
|
|
return enabled
|
|
}
|
|
return false
|
|
}
|