mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-20 20:44:17 +02:00
Add docs
This commit is contained in:
parent
9c25cc6454
commit
233c4fb3fb
4 changed files with 125 additions and 16 deletions
|
|
@ -53,6 +53,30 @@ product_search_tool = Tool.from_tool(
|
|||
|
||||
mcp.add_tool(product_search_tool)
|
||||
```
|
||||
|
||||
<Tip>
|
||||
When you transform a tool, the original tool remains registered on the server. To avoid confusing an LLM with two similar tools, you can disable the original one:
|
||||
|
||||
```python
|
||||
from fastmcp import FastMCP
|
||||
from fastmcp.tools import Tool
|
||||
|
||||
mcp = FastMCP()
|
||||
|
||||
# The original, generic tool
|
||||
@mcp.tool
|
||||
def search(query: str, category: str = "all") -> list[dict]:
|
||||
...
|
||||
|
||||
# Create a more domain-specific version
|
||||
product_search_tool = Tool.from_tool(search, ...)
|
||||
mcp.add_tool(product_search_tool)
|
||||
|
||||
# Disable the original tool
|
||||
search.disable()
|
||||
```
|
||||
</Tip>
|
||||
|
||||
Now, clients see a tool named `find_products` with a clear, domain-specific purpose and relevant tags, even though it still uses the original generic `search` function's logic.
|
||||
|
||||
### Parameters
|
||||
|
|
|
|||
|
|
@ -147,7 +147,32 @@ def data_analysis_prompt(
|
|||
- **`name`**: Sets the explicit prompt name exposed via MCP.
|
||||
- **`description`**: Provides the description exposed via MCP. If set, the function's docstring is ignored for this purpose.
|
||||
- **`tags`**: A set of strings used to categorize the prompt. Clients *might* use tags to filter or group available prompts.
|
||||
- **`enabled`**: A boolean to enable or disable the prompt (defaults to `True`). See [Disabling Prompts](#disabling-prompts) for more information.
|
||||
### Disabling Prompts
|
||||
|
||||
<VersionBadge version="2.8.0" />
|
||||
|
||||
You can control the visibility and availability of prompts by enabling or disabling them. Disabled prompts will not appear in the list of available prompts, and attempting to call a disabled prompt will result in an "Unknown prompt" error.
|
||||
|
||||
By default, all prompts are enabled. You can disable a prompt upon creation using the `enabled` parameter in the decorator:
|
||||
|
||||
```python
|
||||
@mcp.prompt(enabled=False)
|
||||
def experimental_prompt():
|
||||
"""This prompt is not ready for use."""
|
||||
return "This is an experimental prompt."
|
||||
```
|
||||
|
||||
You can also toggle a prompt's state programmatically after it has been created:
|
||||
|
||||
```python
|
||||
@mcp.prompt
|
||||
def seasonal_prompt(): return "Happy Holidays!"
|
||||
|
||||
# Disable and re-enable the prompt
|
||||
seasonal_prompt.disable()
|
||||
seasonal_prompt.enable()
|
||||
```
|
||||
### Asynchronous Prompts
|
||||
|
||||
FastMCP seamlessly supports both standard (`def`) and asynchronous (`async def`) functions as prompts.
|
||||
|
|
@ -191,6 +216,8 @@ async def generate_report_request(report_type: str, ctx: Context) -> str:
|
|||
|
||||
For full documentation on the Context object and all its capabilities, see the [Context documentation](/servers/context).
|
||||
|
||||
|
||||
|
||||
## Server Behavior
|
||||
|
||||
### Duplicate Prompts
|
||||
|
|
|
|||
|
|
@ -94,6 +94,33 @@ def get_application_status() -> dict:
|
|||
- **`description`**: Explanation of the resource (defaults to docstring).
|
||||
- **`mime_type`**: Specifies the content type (FastMCP often infers a default like `text/plain` or `application/json`, but explicit is better for non-text types).
|
||||
- **`tags`**: A set of strings for categorization, potentially used by clients for filtering.
|
||||
- **`enabled`**: A boolean to enable or disable the resource (defaults to `True`). See [Disabling Resources](#disabling-resources) for more information.
|
||||
|
||||
### Disabling Resources
|
||||
|
||||
<VersionBadge version="2.8.0" />
|
||||
|
||||
You can control the visibility and availability of resources and templates by enabling or disabling them. Disabled resources will not appear in the list of available resources or templates, and attempting to read a disabled resource will result in an "Unknown resource" error.
|
||||
|
||||
By default, all resources are enabled. You can disable a resource upon creation using the `enabled` parameter in the decorator:
|
||||
|
||||
```python
|
||||
@mcp.resource("data://secret", enabled=False)
|
||||
def get_secret_data():
|
||||
"""This resource is currently disabled."""
|
||||
return "Secret data"
|
||||
```
|
||||
|
||||
You can also toggle a resource's state programmatically after it has been created:
|
||||
|
||||
```python
|
||||
@mcp.resource("data://config")
|
||||
def get_config(): return {"version": 1}
|
||||
|
||||
# Disable and re-enable the resource
|
||||
get_config.disable()
|
||||
get_config.enable()
|
||||
```
|
||||
|
||||
### Accessing MCP Context
|
||||
|
||||
|
|
|
|||
|
|
@ -169,27 +169,58 @@ def search_products_implementation(query: str, category: str | None = None) -> l
|
|||
|
||||
- **`name`**: Sets the explicit tool name exposed via MCP.
|
||||
- **`description`**: Provides the description exposed via MCP. If set, the function's docstring is ignored for this purpose.
|
||||
- **`tags`**: A set of strings used to categorize the tool. Clients *might* use tags to filter or group available tools.
|
||||
- **`tags`**: A set of strings to categorize the tool. Clients *might* use tags to filter or group available tools.
|
||||
- **`enabled`**: A boolean to enable or disable the tool (defaults to `True`). See [Disabling Tools](#disabling-tools) for more information.
|
||||
- **`exclude_args`**: A list of argument names to exclude from the tool schema shown to the LLM. See [Excluding Arguments](#excluding-arguments) for more information.
|
||||
|
||||
### Excluding Arguments
|
||||
|
||||
- **`exclude_args`**:
|
||||
<VersionBadge version="2.6.0" />
|
||||
A list of argument names to exclude 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.
|
||||
|
||||
<VersionBadge version="2.6.0" />
|
||||
|
||||
Example:
|
||||
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
|
||||
@mcp.tool(
|
||||
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
|
||||
...
|
||||
```
|
||||
Example:
|
||||
|
||||
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.
|
||||
```python
|
||||
@mcp.tool(
|
||||
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
|
||||
...
|
||||
```
|
||||
|
||||
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).
|
||||
|
||||
### Disabling Tools
|
||||
|
||||
<VersionBadge version="2.8.0" />
|
||||
|
||||
You can control the visibility and availability of tools by enabling or disabling them. This is useful for feature flagging, maintenance, or dynamically changing the toolset available to a client. Disabled tools will not appear in the list of available tools returned by `list_tools`, and attempting to call a disabled tool will result in an "Unknown tool" error, just as if the tool did not exist.
|
||||
|
||||
By default, all tools are enabled. You can disable a tool upon creation using the `enabled` parameter in the decorator:
|
||||
|
||||
```python
|
||||
@mcp.tool(enabled=False)
|
||||
def maintenance_tool():
|
||||
"""This tool is currently under maintenance."""
|
||||
return "This tool is disabled."
|
||||
```
|
||||
|
||||
You can also toggle a tool's state programmatically after it has been created:
|
||||
|
||||
```python
|
||||
@mcp.tool
|
||||
def dynamic_tool():
|
||||
return "I am a dynamic tool."
|
||||
|
||||
# Disable and re-enable the tool
|
||||
dynamic_tool.disable()
|
||||
dynamic_tool.enable()
|
||||
```
|
||||
|
||||
### Async Tools
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue