Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Deprecate exclude_args in favor of Depends() (#2621)

Jeremiah Lowin2f561ec98d0b
diff --git a/docs/servers/tools.mdx b/docs/servers/tools.mdx
index 89291c3c3..2a5be722c 100644
--- a/docs/servers/tools.mdx
+++ b/docs/servers/tools.mdx
@@ -87,10 +87,6 @@ def search_products_implementation(query: str, category: str | None = None) -> l
Optional list of icon representations for this tool. See [Icons](/servers/icons) for detailed examples
</ParamField>
-<ParamField body="exclude_args" type="list[str] | None">
- A list of argument names to exclude from the tool schema shown to the LLM. See [Excluding Arguments](#excluding-arguments) for more information
-</ParamField>
-
<ParamField body="annotations" type="ToolAnnotations | dict | None">
An optional `ToolAnnotations` object or dictionary to add additional metadata about the tool.
<Expandable title="ToolAnnotations attributes">
@@ -345,33 +341,28 @@ Field provides several validation and documentation features:
- `pattern`: Regex pattern for string validation
- `default`: Default value if parameter is omitted
+### Hiding Parameters from the LLM
+<VersionBadge version="2.14.0" />
+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:
-### Excluding Arguments
-
-<VersionBadge version="2.6.0" />
-
-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.
+```python
+from fastmcp import FastMCP
+from fastmcp.dependencies import Depends
-**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.
+mcp = FastMCP()
-Example with `exclude_args`:
+def get_user_id() -> str:
+ return "user_123" # Injected at runtime
-```python
- name="get_user_details",
- exclude_args=["user_id"]
-)
-def get_user_details(user_id: str = None) -> str:
- # user_id will be injected by the server, not provided by the LLM
- ...
+def get_user_details(user_id: str = Depends(get_user_id)) -> str:
+ # user_id is injected by the server, not provided by the LLM
+ return f"Details for {user_id}"
```
-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.
-
-For more complex tool transformations, see [Transforming Tools](/patterns/tool-transformation).
-
+See [Custom Dependencies](/servers/context#custom-dependencies) for more details on dependency injection.
## Return Values
diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.py
index 6178b849a..5a34a32f7 100644
--- a/src/fastmcp/server/server.py
+++ b/src/fastmcp/server/server.py
@@ -2005,8 +2005,7 @@ class FastMCP(Generic[LifespanResultT]):
output_schema: Optional JSON schema for the tool's output
annotations: Optional annotations about the tool's behavior
exclude_args: Optional list of argument names to exclude from the tool schema.
- Note: `exclude_args` will be deprecated in FastMCP 2.14 in favor of dependency
- injection with `Depends()` for better lifecycle management.
+ Deprecated: Use `Depends()` for dependency injection instead.
meta: Optional meta information about the tool
enabled: Optional boolean to enable or disable the tool
diff --git a/src/fastmcp/tools/tool.py b/src/fastmcp/tools/tool.py
index 4b6ce6026..191f4737a 100644
--- a/src/fastmcp/tools/tool.py
+++ b/src/fastmcp/tools/tool.py
@@ -321,11 +321,9 @@ class FunctionTool(Tool):
"""Create a Tool from a function."""
if exclude_args and fastmcp.settings.deprecation_warnings:
warnings.warn(
- "The `exclude_args` parameter will be deprecated in FastMCP 2.14. "
- "We recommend using dependency injection with `Depends()` instead, which provides "
- "better lifecycle management and is more explicit. "
- "`exclude_args` will continue to work until then. "
- "See https://gofastmcp.com/docs/servers/tools for examples.",
+ "The `exclude_args` parameter is deprecated as of FastMCP 2.14. "
+ "Use dependency injection with `Depends()` instead for better lifecycle management. "
+ "See https://gofastmcp.com/servers/dependencies for examples.",
DeprecationWarning,
stacklevel=2,
)
diff --git a/tests/server/test_tool_exclude_args.py b/tests/deprecated/test_exclude_args.py
similarity index 100%
rename from tests/server/test_tool_exclude_args.py
rename to tests/deprecated/test_exclude_args.py