fastmcp 2.2.4__tar.gz → 2.2.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fastmcp-2.2.4 → fastmcp-2.2.5}/PKG-INFO +1 -1
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/servers/context.mdx +69 -15
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/servers/prompts.mdx +12 -21
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/servers/resources.mdx +40 -29
- fastmcp-2.2.5/docs/servers/tools.mdx +666 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/prompts/prompt.py +43 -4
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/prompts/prompt_manager.py +14 -3
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/resources/resource.py +12 -2
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/resources/resource_manager.py +20 -5
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/resources/template.py +43 -4
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/resources/types.py +55 -11
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/server/openapi.py +19 -3
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/server/proxy.py +37 -20
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/server/server.py +31 -3
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/tools/tool.py +22 -6
- fastmcp-2.2.5/tests/server/test_server.py +736 -0
- fastmcp-2.2.4/tests/server/test_server.py → fastmcp-2.2.5/tests/server/test_server_interactions.py +635 -776
- fastmcp-2.2.4/docs/servers/tools.mdx +0 -341
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.cursor/rules/core-mcp-objects.mdc +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.github/ISSUE_TEMPLATE/bug.yml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.github/ISSUE_TEMPLATE/config.yml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.github/ISSUE_TEMPLATE/enhancement.yml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.github/release.yml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.github/workflows/publish.yml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.github/workflows/run-static.yml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.github/workflows/run-tests.yml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.gitignore +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/.pre-commit-config.yaml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/LICENSE +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/README.md +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/Windows_Notes.md +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/assets/demo-inspector.png +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/clients/client.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/clients/transports.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/docs.json +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/getting-started/installation.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/getting-started/quickstart.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/getting-started/welcome.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/patterns/composition.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/patterns/contrib.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/patterns/decorating-methods.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/patterns/fastapi.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/patterns/openapi.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/patterns/proxy.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/servers/fastmcp.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/snippets/version-badge.mdx +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/docs/style.css +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/complex_inputs.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/desktop.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/echo.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/memory.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/mount_example.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/readme-quickstart.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/sampling.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/screenshot.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/simple_echo.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/README.md +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/pyproject.toml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/__main__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/hub.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/lights/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/lights/hue_utils.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/lights/server.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/py.typed +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/settings.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/smart_home/uv.lock +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/examples/text_me.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/justfile +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/pyproject.toml +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/cli/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/cli/claude.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/cli/cli.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/client/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/client/base.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/client/client.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/client/roots.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/client/sampling.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/client/transports.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/README.md +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/bulk_tool_caller/README.md +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/bulk_tool_caller/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/bulk_tool_caller/example.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/mcp_mixin/README.md +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/mcp_mixin/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/mcp_mixin/example.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/contrib/mcp_mixin/mcp_mixin.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/exceptions.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/prompts/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/py.typed +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/resources/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/server/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/server/context.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/settings.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/tools/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/tools/tool_manager.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/utilities/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/utilities/decorators.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/utilities/func_metadata.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/utilities/logging.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/utilities/openapi.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/src/fastmcp/utilities/types.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/cli/test_run.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/client/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/client/test_client.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/client/test_roots.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/client/test_sampling.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/conftest.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/contrib/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/contrib/test_bulk_tool_caller.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/contrib/test_mcp_mixin.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/prompts/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/prompts/test_base.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/prompts/test_prompt_manager.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/resources/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/resources/test_file_resources.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/resources/test_function_resources.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/resources/test_resource_manager.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/resources/test_resource_template.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/resources/test_resources.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/server/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/server/test_file_server.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/server/test_import_server.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/server/test_lifespan.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/server/test_mount.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/server/test_openapi.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/server/test_proxy.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/server/test_run_server.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/test_servers/fastmcp_server.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/test_servers/sse.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/test_servers/stdio.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/tools/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/tools/test_tool_manager.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/openapi/__init__.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/openapi/conftest.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/openapi/test_openapi.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/openapi/test_openapi_advanced.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/openapi/test_openapi_fastapi.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/test_decorated_function.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/test_func_metadata.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/tests/utilities/test_logging.py +0 -0
- {fastmcp-2.2.4 → fastmcp-2.2.5}/uv.lock +0 -0
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: MCP Context
|
|
3
3
|
sidebarTitle: Context
|
|
4
|
-
description: Access MCP capabilities like logging, progress, and resources within your
|
|
4
|
+
description: Access MCP capabilities like logging, progress, and resources within your MCP objects.
|
|
5
5
|
icon: rectangle-code
|
|
6
6
|
---
|
|
7
7
|
import { VersionBadge } from '/snippets/version-badge.mdx'
|
|
8
8
|
|
|
9
|
-
When defining FastMCP [tools](/servers/tools), your functions might need to interact with the underlying MCP session or access server capabilities. FastMCP provides the `Context` object for this purpose.
|
|
9
|
+
When defining FastMCP [tools](/servers/tools), [resources](/servers/resources), resource templates, or [prompts](/servers/prompts), your functions might need to interact with the underlying MCP session or access server capabilities. FastMCP provides the `Context` object for this purpose.
|
|
10
10
|
|
|
11
11
|
## What Is Context?
|
|
12
12
|
|
|
13
|
-
The `Context` object provides a clean interface to access MCP features within your
|
|
13
|
+
The `Context` object provides a clean interface to access MCP features within your functions, including:
|
|
14
14
|
|
|
15
15
|
- **Logging**: Send debug, info, warning, and error messages back to the client
|
|
16
16
|
- **Progress Reporting**: Update the client on the progress of long-running operations
|
|
@@ -19,9 +19,9 @@ The `Context` object provides a clean interface to access MCP features within yo
|
|
|
19
19
|
- **Request Information**: Access metadata about the current request
|
|
20
20
|
- **Server Access**: When needed, access the underlying FastMCP server instance
|
|
21
21
|
|
|
22
|
-
## Accessing Context
|
|
22
|
+
## Accessing the Context
|
|
23
23
|
|
|
24
|
-
To use the context object within your
|
|
24
|
+
To use the context object within any of your functions, simply add a parameter to your function signature and type-hint it as `Context`. FastMCP will automatically inject the context instance when your function is called.
|
|
25
25
|
|
|
26
26
|
```python
|
|
27
27
|
from fastmcp import FastMCP, Context
|
|
@@ -65,15 +65,15 @@ async def process_file(file_uri: str, ctx: Context) -> str:
|
|
|
65
65
|
|
|
66
66
|
- The parameter name (e.g., `ctx`, `context`) doesn't matter, only the type hint `Context` is important.
|
|
67
67
|
- The context parameter can be placed anywhere in your function's signature.
|
|
68
|
-
- The context is optional -
|
|
69
|
-
- Context is only available
|
|
70
|
-
- Context methods are async, so your
|
|
68
|
+
- The context is optional - functions that don't need it can omit the parameter.
|
|
69
|
+
- Context is only available during a request; attempting to use context methods outside a request will raise errors.
|
|
70
|
+
- Context methods are async, so your function usually needs to be async as well.
|
|
71
71
|
|
|
72
72
|
## Context Capabilities
|
|
73
73
|
|
|
74
74
|
### Logging
|
|
75
75
|
|
|
76
|
-
Send log messages back to the MCP client. This is useful for debugging and providing visibility into
|
|
76
|
+
Send log messages back to the MCP client. This is useful for debugging and providing visibility into function execution during a request.
|
|
77
77
|
|
|
78
78
|
```python
|
|
79
79
|
@mcp.tool()
|
|
@@ -97,14 +97,14 @@ async def analyze_data(data: list[float], ctx: Context) -> dict:
|
|
|
97
97
|
**Available Logging Methods:**
|
|
98
98
|
|
|
99
99
|
- **`ctx.debug(message: str)`**: Low-level details useful for debugging
|
|
100
|
-
- **`ctx.info(message: str)`**: General information about
|
|
100
|
+
- **`ctx.info(message: str)`**: General information about execution
|
|
101
101
|
- **`ctx.warning(message: str)`**: Potential issues that didn't prevent execution
|
|
102
102
|
- **`ctx.error(message: str)`**: Errors that occurred during execution
|
|
103
103
|
- **`ctx.log(level: Literal["debug", "info", "warning", "error"], message: str, logger_name: str | None = None)`**: Generic log method supporting custom logger names
|
|
104
104
|
|
|
105
105
|
### Progress Reporting
|
|
106
106
|
|
|
107
|
-
For long-running
|
|
107
|
+
For long-running operations, notify the client about the progress. This allows clients to display progress indicators and provide a better user experience.
|
|
108
108
|
|
|
109
109
|
```python
|
|
110
110
|
@mcp.tool()
|
|
@@ -137,7 +137,7 @@ Progress reporting requires the client to have sent a `progressToken` in the ini
|
|
|
137
137
|
|
|
138
138
|
### Resource Access
|
|
139
139
|
|
|
140
|
-
Read data from resources registered with your FastMCP server. This allows
|
|
140
|
+
Read data from resources registered with your FastMCP server. This allows functions to access files, configuration, or dynamically generated content.
|
|
141
141
|
|
|
142
142
|
```python
|
|
143
143
|
@mcp.tool()
|
|
@@ -177,7 +177,7 @@ The returned content is typically accessed via `content_list[0].content` and can
|
|
|
177
177
|
|
|
178
178
|
<VersionBadge version="2.0.0" />
|
|
179
179
|
|
|
180
|
-
Request the client's LLM to generate text based on provided messages. This is useful when your
|
|
180
|
+
Request the client's LLM to generate text based on provided messages. This is useful when your function needs to leverage the LLM's capabilities to process data or generate responses.
|
|
181
181
|
|
|
182
182
|
```python
|
|
183
183
|
@mcp.tool()
|
|
@@ -279,6 +279,60 @@ async def advanced_tool(ctx: Context) -> str:
|
|
|
279
279
|
Direct use of `session` or `request_context` requires understanding the low-level MCP Python SDK and may be less stable than using the methods provided directly on the `Context` object.
|
|
280
280
|
</Warning>
|
|
281
281
|
|
|
282
|
-
## Using Context in
|
|
282
|
+
## Using Context in Different Components
|
|
283
283
|
|
|
284
|
-
|
|
284
|
+
All FastMCP components (tools, resources, templates, and prompts) can use the Context object following the same pattern - simply add a parameter with the `Context` type annotation.
|
|
285
|
+
|
|
286
|
+
### Context in Resources and Templates
|
|
287
|
+
|
|
288
|
+
Resources and resource templates can access context to customize their behavior:
|
|
289
|
+
|
|
290
|
+
```python
|
|
291
|
+
@mcp.resource("resource://user-data")
|
|
292
|
+
async def get_user_data(ctx: Context) -> dict:
|
|
293
|
+
"""Fetch personalized user data based on the request context."""
|
|
294
|
+
user_id = ctx.client_id or "anonymous"
|
|
295
|
+
await ctx.info(f"Fetching data for user {user_id}")
|
|
296
|
+
|
|
297
|
+
# Example of using context for dynamic resource generation
|
|
298
|
+
return {
|
|
299
|
+
"user_id": user_id,
|
|
300
|
+
"last_access": datetime.now().isoformat(),
|
|
301
|
+
"request_id": ctx.request_id
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
@mcp.resource("resource://users/{user_id}/profile")
|
|
305
|
+
async def get_user_profile(user_id: str, ctx: Context) -> dict:
|
|
306
|
+
"""Fetch user profile from database with context-aware logging."""
|
|
307
|
+
await ctx.info(f"Fetching profile for user {user_id}")
|
|
308
|
+
|
|
309
|
+
# Example of using context in a template resource
|
|
310
|
+
# In a real implementation, you might query a database
|
|
311
|
+
return {
|
|
312
|
+
"id": user_id,
|
|
313
|
+
"name": f"User {user_id}",
|
|
314
|
+
"request_id": ctx.request_id
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### Context in Prompts
|
|
319
|
+
|
|
320
|
+
Prompts can use context to generate more dynamic templates:
|
|
321
|
+
|
|
322
|
+
```python
|
|
323
|
+
@mcp.prompt()
|
|
324
|
+
async def data_analysis_request(dataset: str, ctx: Context) -> str:
|
|
325
|
+
"""Generate a request to analyze data with contextual information."""
|
|
326
|
+
await ctx.info(f"Generating data analysis prompt for {dataset}")
|
|
327
|
+
|
|
328
|
+
# Could use context to read configuration or personalize the prompt
|
|
329
|
+
return f"""Please analyze the following dataset: {dataset}
|
|
330
|
+
|
|
331
|
+
Request initiated at: {datetime.now().isoformat()}
|
|
332
|
+
Request ID: {ctx.request_id}
|
|
333
|
+
"""
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
<VersionBadge version="2.3.0" />
|
|
337
|
+
|
|
338
|
+
All FastMCP objects now support context injection using the same consistent pattern, making it easy to add session-aware capabilities to all aspects of your MCP server.
|
|
@@ -20,7 +20,7 @@ Prompts provide parameterized message templates for LLMs. When a client requests
|
|
|
20
20
|
|
|
21
21
|
This allows you to define consistent, reusable templates that LLMs can use across different clients and contexts.
|
|
22
22
|
|
|
23
|
-
##
|
|
23
|
+
## Prompts
|
|
24
24
|
|
|
25
25
|
### The `@prompt` Decorator
|
|
26
26
|
|
|
@@ -171,33 +171,24 @@ async def data_based_prompt(data_id: str) -> str:
|
|
|
171
171
|
|
|
172
172
|
Use `async def` when your prompt function performs I/O operations like network requests, database queries, file I/O, or external service calls.
|
|
173
173
|
|
|
174
|
-
###
|
|
174
|
+
### Accessing MCP Context
|
|
175
175
|
|
|
176
|
-
|
|
176
|
+
<VersionBadge version="2.2.5" />
|
|
177
177
|
|
|
178
|
-
|
|
179
|
-
|
|
178
|
+
Prompts can access additional MCP information and features through the `Context` object. To access it, add a parameter to your prompt function with a type annotation of `Context`:
|
|
179
|
+
|
|
180
|
+
```python {6}
|
|
181
|
+
from fastmcp import FastMCP, Context
|
|
182
|
+
|
|
183
|
+
mcp = FastMCP(name="PromptServer")
|
|
180
184
|
|
|
181
185
|
@mcp.prompt()
|
|
182
186
|
async def generate_report_request(report_type: str, ctx: Context) -> str:
|
|
183
|
-
"""Generates a request for a report
|
|
184
|
-
|
|
185
|
-
await ctx.info(f"Generating prompt for report type: {report_type}")
|
|
186
|
-
|
|
187
|
-
# Could potentially use ctx.read_resource to fetch data
|
|
188
|
-
# Or ctx.sample to get additional input from the LLM
|
|
189
|
-
|
|
190
|
-
return f"Please create a {report_type} report based on the available data."
|
|
187
|
+
"""Generates a request for a report."""
|
|
188
|
+
return f"Please create a {report_type} report. Request ID: {ctx.request_id}"
|
|
191
189
|
```
|
|
192
190
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
- **Logging:** `ctx.debug()`, `ctx.info()`, etc.
|
|
196
|
-
- **Resource Access:** `ctx.read_resource(uri)`
|
|
197
|
-
- **LLM Sampling:** `ctx.sample(...)`
|
|
198
|
-
- **Request Info:** `ctx.request_id`, `ctx.client_id`
|
|
199
|
-
|
|
200
|
-
Refer to the [Context documentation](/servers/context) for more details on these capabilities.
|
|
191
|
+
For full documentation on the Context object and all its capabilities, see the [Context documentation](/servers/context).
|
|
201
192
|
|
|
202
193
|
## Server Behavior
|
|
203
194
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: Resources & Templates
|
|
3
|
-
sidebarTitle: Resources
|
|
3
|
+
sidebarTitle: Resources
|
|
4
4
|
description: Expose data sources and dynamic content generators to your MCP client.
|
|
5
5
|
icon: database
|
|
6
6
|
---
|
|
@@ -21,7 +21,7 @@ Resources provide read-only access to data for the LLM or client application. Wh
|
|
|
21
21
|
|
|
22
22
|
This allows LLMs to access files, database content, configuration, or dynamically generated information relevant to the conversation.
|
|
23
23
|
|
|
24
|
-
##
|
|
24
|
+
## Resources
|
|
25
25
|
|
|
26
26
|
### The `@resource` Decorator
|
|
27
27
|
|
|
@@ -95,6 +95,36 @@ def get_application_status() -> dict:
|
|
|
95
95
|
- **`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).
|
|
96
96
|
- **`tags`**: A set of strings for categorization, potentially used by clients for filtering.
|
|
97
97
|
|
|
98
|
+
### Accessing MCP Context
|
|
99
|
+
|
|
100
|
+
<VersionBadge version="2.2.5" />
|
|
101
|
+
|
|
102
|
+
Resources and resource templates can access additional MCP information and features through the `Context` object. To access it, add a parameter to your resource function with a type annotation of `Context`:
|
|
103
|
+
|
|
104
|
+
```python {6, 14}
|
|
105
|
+
from fastmcp import FastMCP, Context
|
|
106
|
+
|
|
107
|
+
mcp = FastMCP(name="DataServer")
|
|
108
|
+
|
|
109
|
+
@mcp.resource("resource://system-status")
|
|
110
|
+
async def get_system_status(ctx: Context) -> dict:
|
|
111
|
+
"""Provides system status information."""
|
|
112
|
+
return {
|
|
113
|
+
"status": "operational",
|
|
114
|
+
"request_id": ctx.request_id
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@mcp.resource("resource://{name}/details")
|
|
118
|
+
async def get_details(name: str, ctx: Context) -> dict:
|
|
119
|
+
"""Get details for a specific name."""
|
|
120
|
+
return {
|
|
121
|
+
"name": name,
|
|
122
|
+
"accessed_at": ctx.request_id
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
For full documentation on the Context object and all its capabilities, see the [Context documentation](/servers/context).
|
|
127
|
+
|
|
98
128
|
|
|
99
129
|
### Asynchronous Resources
|
|
100
130
|
|
|
@@ -201,10 +231,12 @@ mcp.add_resource(special_resource, key="internal://data-v2") # Will be stored a
|
|
|
201
231
|
|
|
202
232
|
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.
|
|
203
233
|
|
|
204
|
-
##
|
|
234
|
+
## Resource Templates
|
|
205
235
|
|
|
206
236
|
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.
|
|
207
237
|
|
|
238
|
+
Resource templates share most configuration options with regular resources (name, description, mime_type, tags), but add the ability to define URI parameters that map to function parameters.
|
|
239
|
+
|
|
208
240
|
Resource templates generate a new resource for each unique set of parameters, which means that resources can be dynamically created on-demand. For example, if the resource template `"user://profile/{name}"` is registered, MCP clients could request `"user://profile/ford"` or `"user://profile/marvin"` to retrieve either of those two user profiles as resources, without having to register each resource individually.
|
|
209
241
|
|
|
210
242
|
Here is a complete example that shows how to define two resource templates:
|
|
@@ -249,11 +281,12 @@ With these two templates defined, clients can request a variety of resources:
|
|
|
249
281
|
|
|
250
282
|
### Wildcard Parameters
|
|
251
283
|
|
|
252
|
-
<VersionBadge version="2.2.
|
|
284
|
+
<VersionBadge version="2.2.4" />
|
|
285
|
+
|
|
286
|
+
<Tip>
|
|
287
|
+
Please note: FastMCP's support for wildcard parameters is an **extension** of the Model Context Protocol standard, which otherwise follows RFC 6570. Since all template processing happens in the FastMCP server, this should not cause any compatibility issues with other MCP implementations.
|
|
288
|
+
</Tip>
|
|
253
289
|
|
|
254
|
-
<Warning>
|
|
255
|
-
Please note: the Model Context Protocol URI standard follows RFC 6570, which does not include support for wildcard parameters. FastMCP extends the template syntax to support wildcards (`{param*}`), and because template matching happens entirely in the FastMCP server, it is not expected that these wildcards will cause compatibility issues with other MCP implementations. However, this can not be guaranteed.
|
|
256
|
-
</Warning>
|
|
257
290
|
|
|
258
291
|
Resource templates support wildcard parameters that can match multiple path segments. While standard parameters (`{param}`) only match a single path segment and don't cross "/" boundaries, wildcard parameters (`{param*}`) can capture multiple segments including slashes. Wildcards capture all subsequent path segments *up until* the defined part of the URI template (whether literal or another parameter). This allows you to have multiple wildcard parameters in a single URI template.
|
|
259
292
|
|
|
@@ -378,28 +411,6 @@ In this stacked decorator pattern:
|
|
|
378
411
|
|
|
379
412
|
Templates provide a powerful way to expose parameterized data access points following REST-like principles.
|
|
380
413
|
|
|
381
|
-
### Custom Template Keys
|
|
382
|
-
|
|
383
|
-
<VersionBadge version="2.2.0" />
|
|
384
|
-
|
|
385
|
-
Similar to resources, you can provide custom keys when directly adding templates:
|
|
386
|
-
|
|
387
|
-
```python
|
|
388
|
-
from fastmcp.resources import ResourceTemplate
|
|
389
|
-
|
|
390
|
-
# Create a template with a function
|
|
391
|
-
template = ResourceTemplate.from_function(
|
|
392
|
-
my_function,
|
|
393
|
-
uri_template="data://{id}/details",
|
|
394
|
-
name="Data Details"
|
|
395
|
-
)
|
|
396
|
-
|
|
397
|
-
# Register with a custom key
|
|
398
|
-
mcp._resource_manager.add_template(template, key="custom://{id}/view")
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
This allows accessing the same template implementation through different URI patterns.
|
|
402
|
-
|
|
403
414
|
## Server Behavior
|
|
404
415
|
|
|
405
416
|
### Duplicate Resources
|