Groups¶
When MCP servers expose many tools, agents can become overwhelmed with options, leading to poor tool selection and increased token usage. The GroupsMiddleware in wags enables progressive tool disclosure by organizing tools into hierarchical groups that agents can enable or disable as needed.
How It Works¶
Tools are assigned to groups using the @in_group decorator. The middleware:
- Hides all grouped tools initially (or starts with configured
initial_groups) - Exposes
enable_toolsanddisable_toolsmeta-tools - Progressively reveals child groups as parents are enabled
- Enforces optional
max_toolslimits
Example¶
Configure the middleware with group definitions:
from wags.middleware import GroupsMiddleware, GroupDefinition
handlers = GithubHandlers()
mcp.add_middleware(
GroupsMiddleware(
groups={
"issues": GroupDefinition(description="Issue tracking"),
"repos": GroupDefinition(description="Repository management"),
},
handlers=handlers,
initial_groups=["issues"], # Start with issues enabled
max_tools=10, # Optional limit
)
)
Hierarchical Groups¶
Groups can be nested using the parent parameter for progressive disclosure:
groups = {
"code": GroupDefinition(description="Code management"),
"repos": GroupDefinition(description="Repositories", parent="code"),
"branches": GroupDefinition(description="Branches", parent="repos"),
}
With this hierarchy:
- Only
codeis visible initially (root group) - Enabling
coderevealsreposas an option - Enabling
reposrevealsbranchesas an option - Disabling
codecascades to disablereposandbranches
Agent Interaction¶
When an agent calls enable_tools(groups=["issues"]), it receives a structured JSON response:
{
"enabled": ["issues"],
"enabled_groups": ["issues"],
"available_tools": ["create_issue", "list_issues"],
"available_groups": [],
"errors": []
}
The response includes:
enabled: Groups that were newly enabled by this callenabled_groups: All currently enabled groupsavailable_tools: Tools now available to the agentavailable_groups: Child groups that can now be enablederrors: Any validation errors (unknown groups, already enabled, etc.)
Similarly, disable_tools returns:
A tools/list_changed notification is sent whenever groups are enabled or disabled, prompting the client to refresh its tool list.
When a tool from a disabled group is called, the agent receives an error message with a hint about which group to enable.