Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add Enabled function and WithFilter to registry for flexible tool filtering #1618

Closed
@SamMorrowDrums

Description

@SamMorrowDrums

Summary

Add two generic mechanisms to the registry package for flexible tool filtering without coupling to specific domain concepts:

  1. Enabled function onServerTool - Allows tools to self-filter based on request context
  2. WithFilter method onBuilder - Allows cross-cutting filters to be applied to all tools

Motivation

Consumers of the registry (like github-mcp-server-remote) need to filter tools based on various conditions:

  • User permissions and policies
  • Request context (e.g., which client is calling)
  • Feature flags with complex logic (AND/OR combinations)
  • Runtime availability of dependencies

Currently, consumers must do this filtering externally before passing tools to the registry, which leads to complex orchestration code. By adding generic filter hooks, consumers can encapsulate their domain-specific logic while the registry remains agnostic.

Implementation

1. AddEnabled field toServerTool struct inpkg/registry/tool.go:

typeServerToolstruct {Tool              mcp.ToolToolsetToolsetMetadataHandlerFuncHandlerFuncFeatureFlagEnablestringFeatureFlagDisablestring// Enabled is an optional function called at build/filter time to determine// if this tool should be available. If nil, the tool is considered enabled// (subject to FeatureFlagEnable/FeatureFlagDisable checks).// The context carries request-scoped information for the consumer to use.// Returns (enabled, error). On error, the tool should be treated as disabled.Enabledfunc(ctx context.Context) (bool,error)}

2. AddToolFilter type andWithFilter method toBuilder inpkg/registry/registry.go:

// ToolFilter is a function that determines if a tool should be included.// Returns true if the tool should be included, false to exclude it.typeToolFilterfunc(ctx context.Context,tool*ServerTool) (bool,error)// WithFilter adds a filter function that will be applied to all tools.// Multiple filters can be added and are evaluated in order.// If any filter returns false or an error, the tool is excluded.func (b*Builder)WithFilter(filterToolFilter)*Builder {b.filters=append(b.filters,filter)returnb}

3. UpdateisToolEnabled in the builder to check these in order:

  1. Check tool's ownEnabled function (if set)
  2. CheckFeatureFlagEnable (existing behavior)
  3. CheckFeatureFlagDisable (existing behavior)
  4. Apply builder filters (new)

4. Add a context-aware method to get filtered tools:

// FilteredTools returns tools filtered by the Enabled function and builder filters.// This should be called per-request with the request context.func (r*Registry)FilteredTools(ctx context.Context) ([]ServerTool,error)

Example Usage

// Tool with self-filtering logictool:= registry.ServerTool{Tool:myTool,Toolset:myToolset,HandlerFunc:myHandler,Enabled:func(ctx context.Context) (bool,error) {// Consumer's domain-specific logic hereuser:=mypackage.UserFromContext(ctx)returnuser!=nil&&user.HasPermission("use_tool"),nil    },}// Builder with cross-cutting filterreg:=registry.NewBuilder().SetTools(tools).WithFilter(func(ctx context.Context,tool*registry.ServerTool) (bool,error) {// Cross-cutting concern like scope filteringreturn!shouldHideForCurrentAuth(ctx,tool),nil    }).Build()// Get filtered tools for a requestenabledTools,err:=reg.FilteredTools(ctx)

Testing

Add tests for:

  • Enabled function returning true/false/error
  • WithFilter with single and multiple filters
  • Interaction betweenEnabled, feature flags, and filters
  • FilteredTools method with various filter combinations

Backwards Compatibility

  • All new fields are optional with nil/empty defaults
  • Existing code continues to work unchanged
  • FeatureFlagEnable/FeatureFlagDisable remain fully functional

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp