|
| 1 | +# Groups |
| 2 | + |
| 3 | +When MCP servers expose many tools, agents can become overwhelmed with options, leading to poor tool selection and increased token usage. The `GroupsMiddleware` in <span class="wags-brand">wags</span> enables progressive tool disclosure by organizing tools into hierarchical groups that agents can enable or disable as needed. |
| 4 | + |
| 5 | +## How It Works |
| 6 | + |
| 7 | +Tools are assigned to groups using the `@in_group` decorator. The middleware: |
| 8 | + |
| 9 | +1. Hides all grouped tools initially (or starts with configured `initial_groups`) |
| 10 | +2. Exposes `enable_tools` and `disable_tools` meta-tools |
| 11 | +3. Progressively reveals child groups as parents are enabled |
| 12 | +4. Enforces optional `max_tools` limits |
| 13 | + |
| 14 | +## Example |
| 15 | + |
| 16 | +```python linenums="1" title="handlers.py" |
| 17 | +from wags.middleware import GroupsMiddleware, GroupDefinition, in_group |
| 18 | + |
| 19 | +class GithubHandlers: |
| 20 | + @in_group("issues") |
| 21 | + async def create_issue(self, owner: str, repo: str, title: str): |
| 22 | + pass |
| 23 | + |
| 24 | + @in_group("issues") |
| 25 | + async def list_issues(self, owner: str, repo: str): |
| 26 | + pass |
| 27 | + |
| 28 | + @in_group("repos") |
| 29 | + async def create_repository(self, name: str): |
| 30 | + pass |
| 31 | +``` |
| 32 | + |
| 33 | +Configure the middleware with group definitions: |
| 34 | + |
| 35 | +```python title="main.py" |
| 36 | +from wags.middleware import GroupsMiddleware, GroupDefinition |
| 37 | + |
| 38 | +handlers = GithubHandlers() |
| 39 | +mcp.add_middleware( |
| 40 | + GroupsMiddleware( |
| 41 | + groups={ |
| 42 | + "issues": GroupDefinition(description="Issue tracking"), |
| 43 | + "repos": GroupDefinition(description="Repository management"), |
| 44 | + }, |
| 45 | + handlers=handlers, |
| 46 | + initial_groups=["issues"], # Start with issues enabled |
| 47 | + max_tools=10, # Optional limit |
| 48 | + ) |
| 49 | +) |
| 50 | +``` |
| 51 | + |
| 52 | +## Hierarchical Groups |
| 53 | + |
| 54 | +Groups can be nested using the `parent` parameter for progressive disclosure: |
| 55 | + |
| 56 | +```python |
| 57 | +groups = { |
| 58 | + "code": GroupDefinition(description="Code management"), |
| 59 | + "repos": GroupDefinition(description="Repositories", parent="code"), |
| 60 | + "branches": GroupDefinition(description="Branches", parent="repos"), |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +With this hierarchy: |
| 65 | + |
| 66 | +- Only `code` is visible initially (root group) |
| 67 | +- Enabling `code` reveals `repos` as an option |
| 68 | +- Enabling `repos` reveals `branches` as an option |
| 69 | +- Disabling `code` cascades to disable `repos` and `branches` |
| 70 | + |
| 71 | +## Agent Interaction |
| 72 | + |
| 73 | +When an agent calls `enable_tools(groups=["issues"])`, it receives a structured JSON response: |
| 74 | + |
| 75 | +```json |
| 76 | +{ |
| 77 | + "enabled": ["issues"], |
| 78 | + "enabled_groups": ["issues"], |
| 79 | + "available_tools": ["create_issue", "list_issues"], |
| 80 | + "available_groups": [], |
| 81 | + "errors": [] |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +The response includes: |
| 86 | + |
| 87 | +- `enabled`: Groups that were newly enabled by this call |
| 88 | +- `enabled_groups`: All currently enabled groups |
| 89 | +- `available_tools`: Tools now available to the agent |
| 90 | +- `available_groups`: Child groups that can now be enabled |
| 91 | +- `errors`: Any validation errors (unknown groups, already enabled, etc.) |
| 92 | + |
| 93 | +Similarly, `disable_tools` returns: |
| 94 | + |
| 95 | +```json |
| 96 | +{ |
| 97 | + "disabled": ["issues"], |
| 98 | + "enabled_groups": [], |
| 99 | + "available_tools": [], |
| 100 | + "errors": [] |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +A `tools/list_changed` notification is sent whenever groups are enabled or disabled, prompting the client to refresh its tool list. |
| 105 | + |
| 106 | +When a tool from a disabled group is called, the agent receives an error message with a hint about which group to enable. |
| 107 | + |
| 108 | +## API Documentation |
| 109 | + |
| 110 | +::: wags.middleware.groups.in_group |
| 111 | + options: |
| 112 | + show_source: false |
| 113 | + members: [] |
| 114 | + show_signature: false |
| 115 | + |
| 116 | +::: wags.middleware.groups.GroupDefinition |
| 117 | + options: |
| 118 | + show_source: false |
| 119 | + members: [] |
| 120 | + show_signature: false |
| 121 | + |
| 122 | +::: wags.middleware.groups.GroupsMiddleware |
| 123 | + options: |
| 124 | + show_source: false |
| 125 | + show_bases: false |
| 126 | + members: [] |
| 127 | + show_signature: false |
| 128 | + |
| 129 | +::: wags.middleware.groups.EnableToolsResult |
| 130 | + options: |
| 131 | + show_source: false |
| 132 | + members: [] |
| 133 | + show_signature: false |
| 134 | + |
| 135 | +::: wags.middleware.groups.DisableToolsResult |
| 136 | + options: |
| 137 | + show_source: false |
| 138 | + members: [] |
| 139 | + show_signature: false |
0 commit comments