Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453---title: Component VisibilitysidebarTitle: Visibilitydescription: Control which components are available to clientsicon: toggle-ontag: NEW---
import { VersionBadge } from '/snippets/version-badge.mdx'
<VersionBadge version="3.0.0" />
Components can be dynamically enabled or disabled at runtime. A disabled tool disappears from listings and cannot be called. This enables runtime access control, feature flags, and context-aware component exposure.
## Component Visibility
Every FastMCP server provides `enable()` and `disable()` methods for controlling component availability.
### Disabling Components
The `disable()` method marks components as disabled. Disabled components are filtered out from all client queries.
```pythonfrom fastmcp import FastMCP
mcp = FastMCP("Server")
@mcp.tool(tags={"admin"})def delete_everything() -> str: """Delete all data.""" return "Deleted"
@mcp.tool(tags={"admin"})def reset_system() -> str: """Reset the system.""" return "Reset"
@mcp.tooldef get_status() -> str: """Get system status.""" return "OK"
# Disable admin toolsmcp.disable(tags={"admin"})
# Clients only see: get_status```
### Enabling Components
The `enable()` method re-enables previously disabled components.
```python# Re-enable admin toolsmcp.enable(tags={"admin"})
# Clients now see all three tools```
## Keys and Tags
Visibility filtering works with two identifiers: keys (for specific components) and tags (for groups).
### Component Keys
Every component has a unique key in the format `{type}:{identifier}`.
| Component | Key Format | Example ||-----------|------------|---------|| Tool | `tool:{name}` | `tool:delete_everything` || Resource | `resource:{uri}` | `resource:data://config` || Template | `template:{uri}` | `template:file://{path}` || Prompt | `prompt:{name}` | `prompt:analyze` |
Use keys to target specific components.
```python# Disable a specific toolmcp.disable(keys={"tool:delete_everything"})
# Disable multiple specific componentsmcp.disable(keys={"tool:reset_system", "resource:data://secrets"})```
### Tags
Tags group components for bulk operations. Define tags when creating components, then filter by them.
```pythonfrom fastmcp import FastMCP
mcp = FastMCP("Server")
@mcp.tool(tags={"public", "read"})def get_data() -> str: return "data"
@mcp.tool(tags={"admin", "write"})def set_data(value: str) -> str: return f"Set: {value}"
@mcp.tool(tags={"admin", "dangerous"})def delete_data() -> str: return "Deleted"
# Disable all admin toolsmcp.disable(tags={"admin"})
# Disable all dangerous tools (some overlap with admin)mcp.disable(tags={"dangerous"})```
A component is disabled if it has **any** of the disabled tags. The component doesn't need all the tags; one match is enough.
### Combining Keys and Tags
You can specify both keys and tags in a single call. The filters combine additively.
```python# Disable specific tools AND all dangerous-tagged componentsmcp.disable(keys={"tool:debug_info"}, tags={"dangerous"})```
## Allowlist Mode
By default, visibility filtering uses blocklist mode: everything is enabled unless explicitly disabled. The `only=True` parameter switches to allowlist mode, where **only** specified components are enabled.
```pythonfrom fastmcp import FastMCP
mcp = FastMCP("Server")
@mcp.tool(tags={"safe"})def read_only_operation() -> str: return "Read"
@mcp.tool(tags={"safe"})def list_items() -> list[str]: return ["a", "b", "c"]
@mcp.tool(tags={"dangerous"})def delete_all() -> str: return "Deleted"
@mcp.tooldef untagged_tool() -> str: return "Untagged"
# Only enable safe tools - everything else is disabledmcp.enable(tags={"safe"}, only=True)
# Clients see: read_only_operation, list_items# Disabled: delete_all, untagged_tool```
Allowlist mode is useful for restrictive environments where you want to explicitly opt-in components rather than opt-out.
### Allowlist Behavior
When you call `enable(only=True)`:
1. Default visibility state switches to "disabled"2. Previous allowlists are cleared3. Only specified keys/tags become enabled
```python# Start fresh - only enable these specific toolsmcp.enable(keys={"tool:safe_read", "tool:safe_write"}, only=True)
# Later, switch to a different allowlistmcp.enable(tags={"production"}, only=True)```
### Ordering and Overrides
Later `enable()` and `disable()` calls override earlier ones. This lets you create broad rules with specific exceptions.
```pythonmcp.enable(tags={"api"}, only=True) # Allow all api-taggedmcp.disable(keys={"tool:api_admin"}) # Later disable overrides for this tool
# api_admin is disabled because the later disable() overrides the allowlist```
You can always re-enable something that was disabled by adding another `enable()` call after it.
## Server vs Provider
Visibility state operates at two levels: the server and individual providers.
### Server-Level
Server-level visibility state applies to all components from all providers. When you call `mcp.enable()` or `mcp.disable()`, you're filtering the final view that clients see.
```pythonfrom fastmcp import FastMCP
main = FastMCP("Main")main.mount(sub_server, namespace="api")
@main.tool(tags={"internal"})def local_debug() -> str: return "Debug"
# Disable internal tools from ALL sourcesmain.disable(tags={"internal"})```
### Provider-Level
Each provider can add its own visibility transforms. These run before server-level transforms, so the server can override provider-level disables.
```pythonfrom fastmcp import FastMCPfrom fastmcp.server.providers import LocalProvider
# Create provider with visibility controladmin_tools = LocalProvider()
@admin_tools.tool(tags={"admin"})def admin_action() -> str: return "Admin"
@admin_tools.tooldef regular_action() -> str: return "Regular"
# Disable at provider leveladmin_tools.disable(tags={"admin"})
# Server can override if neededmcp = FastMCP("Server", providers=[admin_tools])mcp.enable(names={"admin_action"}) # Re-enables despite provider disable```
Provider-level transforms are useful for setting default visibility that servers can selectively override.
### Layered Transforms
Provider transforms run first, then server transforms. Later transforms override earlier ones, so the server has final say.
```pythonfrom fastmcp import FastMCPfrom fastmcp.server.providers import LocalProvider
provider = LocalProvider()
@provider.tool(tags={"feature", "beta"})def new_feature() -> str: return "New"
# Provider enables feature-taggedprovider.enable(tags={"feature"}, only=True)
# Server disables beta-tagged (runs after provider)mcp = FastMCP("Server", providers=[provider])mcp.disable(tags={"beta"})
# new_feature is disabled (server's later disable overrides provider's enable)```
## Per-Session Visibility
Server-level visibility changes affect all connected clients simultaneously. When you need different clients to see different components, use per-session visibility instead.
Session visibility lets individual sessions customize their view of available components. When a tool calls `ctx.enable_components()` or `ctx.disable_components()`, those rules apply only to the current session. Other sessions continue to see the global defaults. This enables patterns like progressive disclosure, role-based access, and on-demand feature activation.
```pythonfrom fastmcp import FastMCPfrom fastmcp.server.context import Context
mcp = FastMCP("Session-Aware Server")
@mcp.tool(tags={"premium"})def premium_analysis(data: str) -> str: """Advanced analysis available to premium users.""" return f"Premium analysis of: {data}"
@mcp.toolasync def unlock_premium(ctx: Context) -> str: """Unlock premium features for this session.""" await ctx.enable_components(tags={"premium"}) return "Premium features unlocked"
@mcp.toolasync def reset_features(ctx: Context) -> str: """Reset to default feature set.""" await ctx.reset_visibility() return "Features reset to defaults"
# Premium tools are disabled globally by defaultmcp.disable(tags={"premium"})```
All sessions start with `premium_analysis` hidden. When a session calls `unlock_premium`, that session gains access to premium tools while other sessions remain unaffected. Calling `reset_features` returns the session to the global defaults.
### How Session Rules Work
Session rules override global transforms. When listing components, FastMCP first applies global enable/disable rules, then applies session-specific rules on top. Rules within a session accumulate, and later rules override earlier ones for the same component.
```python@mcp.toolasync def customize_session(ctx: Context) -> str: # Enable finance tools for this session await ctx.enable_components(tags={"finance"})
# Also enable admin tools await ctx.enable_components(tags={"admin"})
# Later: disable a specific admin tool await ctx.disable_components(names={"dangerous_admin_tool"})
return "Session customized"```
Each call adds a rule to the session. The `dangerous_admin_tool` ends up disabled because its disable rule was added after the admin enable rule.
### Filter Criteria
The session visibility methods accept the same filter criteria as `server.enable()` and `server.disable()`:
| Parameter | Description ||-----------|-------------|| `names` | Component names or URIs to match || `keys` | Component keys (e.g., `{"tool:my_tool"}`) || `tags` | Tags to match (component must have at least one) || `version` | Version specification to match || `components` | Component types (`{"tool"}`, `{"resource"}`, `{"prompt"}`, `{"template"}`) || `match_all` | If `True`, matches all components regardless of other criteria |
```pythonfrom fastmcp.utilities.versions import VersionSpec
@mcp.toolasync def enable_recent_tools(ctx: Context) -> str: """Enable only tools from version 2.0.0 or later.""" await ctx.enable_components( version=VersionSpec(gte="2.0.0"), components={"tool"} ) return "Recent tools enabled"```
### Automatic Notifications
When session visibility changes, FastMCP automatically sends notifications to that session. Clients receive `ToolListChangedNotification`, `ResourceListChangedNotification`, and `PromptListChangedNotification` so they can refresh their component lists. These notifications go only to the affected session.
When you specify the `components` parameter, FastMCP optimizes by sending only the relevant notifications:
```python# Only sends ToolListChangedNotificationawait ctx.enable_components(tags={"finance"}, components={"tool"})
# Sends all three notifications (no components filter)await ctx.enable_components(tags={"finance"})```
### Namespace Activation Pattern
A common pattern organizes tools into namespaces using tag prefixes, disables them globally, then provides activation tools that unlock namespaces on demand:
```pythonfrom fastmcp import FastMCPfrom fastmcp.server.context import Context
server = FastMCP("Multi-Domain Assistant")
# Finance namespace@server.tool(tags={"namespace:finance"})def analyze_portfolio(symbols: list[str]) -> str: return f"Analysis for: {', '.join(symbols)}"
@server.tool(tags={"namespace:finance"})def get_market_data(symbol: str) -> dict: return {"symbol": symbol, "price": 150.25}
# Admin namespace@server.tool(tags={"namespace:admin"})def list_users() -> list[str]: return ["alice", "bob", "charlie"]
# Activation tools - always visible@server.toolasync def activate_finance(ctx: Context) -> str: await ctx.enable_components(tags={"namespace:finance"}) return "Finance tools activated"
@server.toolasync def activate_admin(ctx: Context) -> str: await ctx.enable_components(tags={"namespace:admin"}) return "Admin tools activated"
@server.toolasync def deactivate_all(ctx: Context) -> str: await ctx.reset_visibility() return "All namespaces deactivated"
# Disable namespace tools globallyserver.disable(tags={"namespace:finance", "namespace:admin"})```
Sessions start seeing only the activation tools. Calling `activate_finance` reveals finance tools for that session only. Multiple namespaces can be activated independently, and `deactivate_all` returns to the initial state.
### Method Reference
- **`await ctx.enable_components(...) -> None`**: Enable matching components for this session- **`await ctx.disable_components(...) -> None`**: Disable matching components for this session- **`await ctx.reset_visibility() -> None`**: Clear all session rules, returning to global defaults
## Client Notifications
When visibility state changes, FastMCP automatically notifies connected clients. Clients supporting the MCP notification protocol receive `list_changed` events and can refresh their component lists.
This happens automatically. You don't need to trigger notifications manually.
```python# This automatically notifies clientsmcp.disable(tags={"maintenance"})
# Clients receive: tools/list_changed, resources/list_changed, etc.```
## Filtering Logic
Understanding the filtering logic helps when debugging visibility state issues.
The `is_enabled()` function checks a component's internal metadata:
1. If the component has `meta.fastmcp._internal.visibility = False`, it's disabled2. If the component has `meta.fastmcp._internal.visibility = True`, it's enabled3. If no visibility state is set, the component is enabled by default
When multiple `enable()` and `disable()` calls are made, transforms are applied in order. **Later transforms override earlier ones**, so the last matching transform wins.
## The Visibility Transform
Under the hood, `enable()` and `disable()` add `Visibility` transforms to the server or provider. The `Visibility` transform marks components with visibility metadata, and the server applies the final filter after all provider and server transforms complete.
```pythonfrom fastmcp import FastMCPfrom fastmcp.server.transforms import Visibility
mcp = FastMCP("Server")
# Using the convenience method (recommended)mcp.disable(names={"secret_tool"})
# Equivalent to:mcp.add_transform(Visibility(False, names={"secret_tool"}))```
Server-level transforms override provider-level transforms. If a component is disabled at the provider level but enabled at the server level, the server-level `enable()` can re-enable it.