Spaces:
Running
Running
Jeremiah Lowin commited on
Update resources.mdx (#1334)
Browse files- docs/servers/resources.mdx +8 -10
docs/servers/resources.mdx
CHANGED
|
@@ -502,7 +502,7 @@ def search_resources(query: str, max_results: int = 10, include_archived: bool =
|
|
| 502 |
|
| 503 |
With this template, clients can request `search://python` and the function will be called with `query="python", max_results=10, include_archived=False`. MCP Developers can still call the underlying `search_resources` function directly with more specific parameters.
|
| 504 |
|
| 505 |
-
|
| 506 |
|
| 507 |
```python
|
| 508 |
from fastmcp import FastMCP
|
|
@@ -510,27 +510,25 @@ from fastmcp import FastMCP
|
|
| 510 |
mcp = FastMCP(name="DataServer")
|
| 511 |
|
| 512 |
# Define a user lookup function that can be accessed by different identifiers
|
| 513 |
-
@mcp.resource("users://email/{email}")
|
| 514 |
-
@mcp.resource("users://name/{name}")
|
| 515 |
def lookup_user(name: str | None = None, email: str | None = None) -> dict:
|
| 516 |
"""Look up a user by either name or email."""
|
| 517 |
if email:
|
| 518 |
-
return find_user_by_email(email)
|
| 519 |
elif name:
|
| 520 |
-
return find_user_by_name(name)
|
| 521 |
else:
|
| 522 |
return {"error": "No lookup parameters provided"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
```
|
| 524 |
|
| 525 |
Now an LLM or client can retrieve user information in two different ways:
|
| 526 |
- `users://email/alice@example.com` → Looks up user by email (with name=None)
|
| 527 |
- `users://name/Bob` → Looks up user by name (with email=None)
|
| 528 |
|
| 529 |
-
|
| 530 |
-
- The `name` parameter is only provided when using the `users://name/{name}` template
|
| 531 |
-
- The `email` parameter is only provided when using the `users://email/{email}` template
|
| 532 |
-
- Each parameter defaults to `None` when not included in the URI
|
| 533 |
-
- The function logic handles whichever parameter is provided
|
| 534 |
|
| 535 |
Templates provide a powerful way to expose parameterized data access points following REST-like principles.
|
| 536 |
|
|
|
|
| 502 |
|
| 503 |
With this template, clients can request `search://python` and the function will be called with `query="python", max_results=10, include_archived=False`. MCP Developers can still call the underlying `search_resources` function directly with more specific parameters.
|
| 504 |
|
| 505 |
+
You can also create multiple resource templates that provide different ways to access the same underlying data by manually applying decorators to a single function:
|
| 506 |
|
| 507 |
```python
|
| 508 |
from fastmcp import FastMCP
|
|
|
|
| 510 |
mcp = FastMCP(name="DataServer")
|
| 511 |
|
| 512 |
# Define a user lookup function that can be accessed by different identifiers
|
|
|
|
|
|
|
| 513 |
def lookup_user(name: str | None = None, email: str | None = None) -> dict:
|
| 514 |
"""Look up a user by either name or email."""
|
| 515 |
if email:
|
| 516 |
+
return find_user_by_email(email) # pseudocode
|
| 517 |
elif name:
|
| 518 |
+
return find_user_by_name(name) # pseudocode
|
| 519 |
else:
|
| 520 |
return {"error": "No lookup parameters provided"}
|
| 521 |
+
|
| 522 |
+
# Manually apply multiple decorators to the same function
|
| 523 |
+
mcp.resource("users://email/{email}")(lookup_user)
|
| 524 |
+
mcp.resource("users://name/{name}")(lookup_user)
|
| 525 |
```
|
| 526 |
|
| 527 |
Now an LLM or client can retrieve user information in two different ways:
|
| 528 |
- `users://email/alice@example.com` → Looks up user by email (with name=None)
|
| 529 |
- `users://name/Bob` → Looks up user by name (with email=None)
|
| 530 |
|
| 531 |
+
This approach allows a single function to be registered with multiple URI patterns while keeping the implementation clean and straightforward.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
Templates provide a powerful way to expose parameterized data access points following REST-like principles.
|
| 534 |
|