Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
341913a
1
Parent(s): 807bad3
update docs
Browse files- docs/servers/resources.mdx +45 -1
docs/servers/resources.mdx
CHANGED
|
@@ -149,7 +149,15 @@ notice_resource = TextResource(
|
|
| 149 |
)
|
| 150 |
mcp.add_resource(notice_resource)
|
| 151 |
|
| 152 |
-
# 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
data_dir_path = Path("./app_data").resolve()
|
| 154 |
if data_dir_path.is_dir():
|
| 155 |
data_listing_resource = DirectoryResource(
|
|
@@ -173,6 +181,22 @@ if data_dir_path.is_dir():
|
|
| 173 |
|
| 174 |
Use these when the content is static or sourced directly from a file/URL, bypassing the need for a dedicated Python function.
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
## Defining Resource Templates
|
| 177 |
|
| 178 |
Resource Templates allow clients to request resources whose content depends on parameters embedded in the URI. Define a template using the **same `@mcp.resource` decorator**, but include `{parameter_name}` placeholders in the URI string and add corresponding arguments to your function signature.
|
|
@@ -289,6 +313,26 @@ In this stacked decorator pattern:
|
|
| 289 |
|
| 290 |
Templates provide a powerful way to expose parameterized data access points following REST-like principles.
|
| 291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
## Server Behavior
|
| 293 |
|
| 294 |
### Duplicate Resources
|
|
|
|
| 149 |
)
|
| 150 |
mcp.add_resource(notice_resource)
|
| 151 |
|
| 152 |
+
# 3. Using a custom key different from the URI
|
| 153 |
+
special_resource = TextResource(
|
| 154 |
+
uri="resource://common-notice",
|
| 155 |
+
name="Special Notice",
|
| 156 |
+
text="This is a special notice with a custom storage key.",
|
| 157 |
+
)
|
| 158 |
+
mcp.add_resource(special_resource, key="resource://custom-key")
|
| 159 |
+
|
| 160 |
+
# 4. Exposing a directory listing
|
| 161 |
data_dir_path = Path("./app_data").resolve()
|
| 162 |
if data_dir_path.is_dir():
|
| 163 |
data_listing_resource = DirectoryResource(
|
|
|
|
| 181 |
|
| 182 |
Use these when the content is static or sourced directly from a file/URL, bypassing the need for a dedicated Python function.
|
| 183 |
|
| 184 |
+
#### Custom Resource Keys
|
| 185 |
+
|
| 186 |
+
When adding resources directly with `mcp.add_resource()`, you can optionally provide a custom storage key:
|
| 187 |
+
|
| 188 |
+
```python
|
| 189 |
+
# Creating a resource with standard URI as the key
|
| 190 |
+
resource = TextResource(uri="resource://data")
|
| 191 |
+
mcp.add_resource(resource) # Will be stored and accessed using "resource://data"
|
| 192 |
+
|
| 193 |
+
# Creating a resource with a custom key
|
| 194 |
+
special_resource = TextResource(uri="resource://special-data")
|
| 195 |
+
mcp.add_resource(special_resource, key="internal://data-v2") # Will be stored and accessed using "internal://data-v2"
|
| 196 |
+
```
|
| 197 |
+
|
| 198 |
+
Note that this parameter is only available when using `add_resource()` directly and not through the `@resource` decorator, as URIs are provided explicitly when using the decorator.
|
| 199 |
+
|
| 200 |
## Defining Resource Templates
|
| 201 |
|
| 202 |
Resource Templates allow clients to request resources whose content depends on parameters embedded in the URI. Define a template using the **same `@mcp.resource` decorator**, but include `{parameter_name}` placeholders in the URI string and add corresponding arguments to your function signature.
|
|
|
|
| 313 |
|
| 314 |
Templates provide a powerful way to expose parameterized data access points following REST-like principles.
|
| 315 |
|
| 316 |
+
### Custom Template Keys
|
| 317 |
+
|
| 318 |
+
Similar to resources, you can provide custom keys when directly adding templates:
|
| 319 |
+
|
| 320 |
+
```python
|
| 321 |
+
from fastmcp.resources import ResourceTemplate
|
| 322 |
+
|
| 323 |
+
# Create a template with a function
|
| 324 |
+
template = ResourceTemplate.from_function(
|
| 325 |
+
my_function,
|
| 326 |
+
uri_template="data://{id}/details",
|
| 327 |
+
name="Data Details"
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
# Register with a custom key
|
| 331 |
+
mcp._resource_manager.add_template(template, key="custom://{id}/view")
|
| 332 |
+
```
|
| 333 |
+
|
| 334 |
+
This allows accessing the same template implementation through different URI patterns.
|
| 335 |
+
|
| 336 |
## Server Behavior
|
| 337 |
|
| 338 |
### Duplicate Resources
|