Skip to content

Commit 2f561ec

Browse files
authored
Deprecate exclude_args in favor of Depends() (#2621)
* Deprecate exclude_args in favor of Depends() * Add version badge to Hiding Parameters section
1 parent 076ec0c commit 2f561ec

File tree

4 files changed

+18
-30
lines changed

4 files changed

+18
-30
lines changed

docs/servers/tools.mdx

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ def search_products_implementation(query: str, category: str | None = None) -> l
8787
Optional list of icon representations for this tool. See [Icons](/servers/icons) for detailed examples
8888
</ParamField>
8989

90-
<ParamField body="exclude_args" type="list[str] | None">
91-
A list of argument names to exclude from the tool schema shown to the LLM. See [Excluding Arguments](#excluding-arguments) for more information
92-
</ParamField>
93-
9490
<ParamField body="annotations" type="ToolAnnotations | dict | None">
9591
An optional `ToolAnnotations` object or dictionary to add additional metadata about the tool.
9692
<Expandable title="ToolAnnotations attributes">
@@ -345,33 +341,28 @@ Field provides several validation and documentation features:
345341
- `pattern`: Regex pattern for string validation
346342
- `default`: Default value if parameter is omitted
347343

344+
### Hiding Parameters from the LLM
348345

346+
<VersionBadge version="2.14.0" />
349347

348+
To inject values at runtime without exposing them to the LLM (such as `user_id`, credentials, or database connections), use dependency injection with `Depends()`. Parameters using `Depends()` are automatically excluded from the tool schema:
350349

351-
### Excluding Arguments
352-
353-
<VersionBadge version="2.6.0" />
354-
355-
You can exclude certain arguments from the tool schema shown to the LLM. This is useful for arguments that are injected at runtime (such as `state`, `user_id`, or credentials) and should not be exposed to the LLM or client. Only arguments with default values can be excluded; attempting to exclude a required argument will raise an error.
350+
```python
351+
from fastmcp import FastMCP
352+
from fastmcp.dependencies import Depends
356353

357-
**Note:** `exclude_args` will be deprecated in FastMCP 2.14 in favor of dependency injection with `Depends()` for better lifecycle management and more explicit dependency handling. `exclude_args` will continue to work until then.
354+
mcp = FastMCP()
358355

359-
Example with `exclude_args`:
356+
def get_user_id() -> str:
357+
return "user_123" # Injected at runtime
360358

361-
```python
362-
@mcp.tool(
363-
name="get_user_details",
364-
exclude_args=["user_id"]
365-
)
366-
def get_user_details(user_id: str = None) -> str:
367-
# user_id will be injected by the server, not provided by the LLM
368-
...
359+
@mcp.tool
360+
def get_user_details(user_id: str = Depends(get_user_id)) -> str:
361+
# user_id is injected by the server, not provided by the LLM
362+
return f"Details for {user_id}"
369363
```
370364

371-
With this configuration, `user_id` will not appear in the tool's parameter schema, but can still be set by the server or framework at runtime.
372-
373-
For more complex tool transformations, see [Transforming Tools](/patterns/tool-transformation).
374-
365+
See [Custom Dependencies](/servers/context#custom-dependencies) for more details on dependency injection.
375366

376367
## Return Values
377368

src/fastmcp/server/server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,8 +2005,7 @@ def tool(
20052005
output_schema: Optional JSON schema for the tool's output
20062006
annotations: Optional annotations about the tool's behavior
20072007
exclude_args: Optional list of argument names to exclude from the tool schema.
2008-
Note: `exclude_args` will be deprecated in FastMCP 2.14 in favor of dependency
2009-
injection with `Depends()` for better lifecycle management.
2008+
Deprecated: Use `Depends()` for dependency injection instead.
20102009
meta: Optional meta information about the tool
20112010
enabled: Optional boolean to enable or disable the tool
20122011

src/fastmcp/tools/tool.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,9 @@ def from_function(
321321
"""Create a Tool from a function."""
322322
if exclude_args and fastmcp.settings.deprecation_warnings:
323323
warnings.warn(
324-
"The `exclude_args` parameter will be deprecated in FastMCP 2.14. "
325-
"We recommend using dependency injection with `Depends()` instead, which provides "
326-
"better lifecycle management and is more explicit. "
327-
"`exclude_args` will continue to work until then. "
328-
"See https://gofastmcp.com/docs/servers/tools for examples.",
324+
"The `exclude_args` parameter is deprecated as of FastMCP 2.14. "
325+
"Use dependency injection with `Depends()` instead for better lifecycle management. "
326+
"See https://gofastmcp.com/servers/dependencies for examples.",
329327
DeprecationWarning,
330328
stacklevel=2,
331329
)

0 commit comments

Comments
 (0)