fastmcp 2.2.3__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.
Files changed (146) hide show
  1. {fastmcp-2.2.3 → fastmcp-2.2.5}/PKG-INFO +2 -2
  2. {fastmcp-2.2.3 → fastmcp-2.2.5}/README.md +1 -1
  3. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/servers/context.mdx +69 -15
  4. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/servers/prompts.mdx +12 -21
  5. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/servers/resources.mdx +45 -27
  6. fastmcp-2.2.5/docs/servers/tools.mdx +666 -0
  7. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/cli/cli.py +36 -3
  8. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/prompts/prompt.py +43 -4
  9. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/prompts/prompt_manager.py +14 -3
  10. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/resources/resource.py +12 -2
  11. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/resources/resource_manager.py +20 -5
  12. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/resources/template.py +44 -5
  13. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/resources/types.py +55 -11
  14. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/server/openapi.py +31 -61
  15. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/server/proxy.py +37 -20
  16. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/server/server.py +31 -3
  17. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/tools/tool.py +22 -6
  18. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/utilities/func_metadata.py +16 -4
  19. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/resources/test_resource_template.py +77 -0
  20. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/server/test_openapi.py +137 -56
  21. fastmcp-2.2.5/tests/server/test_server.py +736 -0
  22. fastmcp-2.2.3/tests/server/test_server.py → fastmcp-2.2.5/tests/server/test_server_interactions.py +678 -746
  23. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/test_func_metadata.py +68 -0
  24. fastmcp-2.2.3/docs/servers/tools.mdx +0 -341
  25. {fastmcp-2.2.3 → fastmcp-2.2.5}/.cursor/rules/core-mcp-objects.mdc +0 -0
  26. {fastmcp-2.2.3 → fastmcp-2.2.5}/.github/ISSUE_TEMPLATE/bug.yml +0 -0
  27. {fastmcp-2.2.3 → fastmcp-2.2.5}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  28. {fastmcp-2.2.3 → fastmcp-2.2.5}/.github/ISSUE_TEMPLATE/enhancement.yml +0 -0
  29. {fastmcp-2.2.3 → fastmcp-2.2.5}/.github/release.yml +0 -0
  30. {fastmcp-2.2.3 → fastmcp-2.2.5}/.github/workflows/publish.yml +0 -0
  31. {fastmcp-2.2.3 → fastmcp-2.2.5}/.github/workflows/run-static.yml +0 -0
  32. {fastmcp-2.2.3 → fastmcp-2.2.5}/.github/workflows/run-tests.yml +0 -0
  33. {fastmcp-2.2.3 → fastmcp-2.2.5}/.gitignore +0 -0
  34. {fastmcp-2.2.3 → fastmcp-2.2.5}/.pre-commit-config.yaml +0 -0
  35. {fastmcp-2.2.3 → fastmcp-2.2.5}/LICENSE +0 -0
  36. {fastmcp-2.2.3 → fastmcp-2.2.5}/Windows_Notes.md +0 -0
  37. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/assets/demo-inspector.png +0 -0
  38. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/clients/client.mdx +0 -0
  39. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/clients/transports.mdx +0 -0
  40. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/docs.json +0 -0
  41. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/getting-started/installation.mdx +0 -0
  42. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/getting-started/quickstart.mdx +0 -0
  43. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/getting-started/welcome.mdx +0 -0
  44. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/patterns/composition.mdx +0 -0
  45. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/patterns/contrib.mdx +0 -0
  46. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/patterns/decorating-methods.mdx +0 -0
  47. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/patterns/fastapi.mdx +0 -0
  48. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/patterns/openapi.mdx +0 -0
  49. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/patterns/proxy.mdx +0 -0
  50. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/servers/fastmcp.mdx +0 -0
  51. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/snippets/version-badge.mdx +0 -0
  52. {fastmcp-2.2.3 → fastmcp-2.2.5}/docs/style.css +0 -0
  53. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/complex_inputs.py +0 -0
  54. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/desktop.py +0 -0
  55. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/echo.py +0 -0
  56. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/memory.py +0 -0
  57. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/mount_example.py +0 -0
  58. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/readme-quickstart.py +0 -0
  59. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/sampling.py +0 -0
  60. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/screenshot.py +0 -0
  61. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/simple_echo.py +0 -0
  62. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/README.md +0 -0
  63. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/pyproject.toml +0 -0
  64. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/__init__.py +0 -0
  65. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/__main__.py +0 -0
  66. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/hub.py +0 -0
  67. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/lights/__init__.py +0 -0
  68. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/lights/hue_utils.py +0 -0
  69. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/lights/server.py +0 -0
  70. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/py.typed +0 -0
  71. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/src/smart_home/settings.py +0 -0
  72. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/smart_home/uv.lock +0 -0
  73. {fastmcp-2.2.3 → fastmcp-2.2.5}/examples/text_me.py +0 -0
  74. {fastmcp-2.2.3 → fastmcp-2.2.5}/justfile +0 -0
  75. {fastmcp-2.2.3 → fastmcp-2.2.5}/pyproject.toml +0 -0
  76. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/__init__.py +0 -0
  77. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/cli/__init__.py +0 -0
  78. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/cli/claude.py +0 -0
  79. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/client/__init__.py +0 -0
  80. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/client/base.py +0 -0
  81. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/client/client.py +0 -0
  82. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/client/roots.py +0 -0
  83. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/client/sampling.py +0 -0
  84. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/client/transports.py +0 -0
  85. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/README.md +0 -0
  86. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/bulk_tool_caller/README.md +0 -0
  87. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/bulk_tool_caller/__init__.py +0 -0
  88. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py +0 -0
  89. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/bulk_tool_caller/example.py +0 -0
  90. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/mcp_mixin/README.md +0 -0
  91. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/mcp_mixin/__init__.py +0 -0
  92. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/mcp_mixin/example.py +0 -0
  93. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/contrib/mcp_mixin/mcp_mixin.py +0 -0
  94. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/exceptions.py +0 -0
  95. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/prompts/__init__.py +0 -0
  96. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/py.typed +0 -0
  97. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/resources/__init__.py +0 -0
  98. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/server/__init__.py +0 -0
  99. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/server/context.py +0 -0
  100. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/settings.py +0 -0
  101. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/tools/__init__.py +0 -0
  102. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/tools/tool_manager.py +0 -0
  103. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/utilities/__init__.py +0 -0
  104. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/utilities/decorators.py +0 -0
  105. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/utilities/logging.py +0 -0
  106. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/utilities/openapi.py +0 -0
  107. {fastmcp-2.2.3 → fastmcp-2.2.5}/src/fastmcp/utilities/types.py +0 -0
  108. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/__init__.py +0 -0
  109. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/cli/test_run.py +0 -0
  110. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/client/__init__.py +0 -0
  111. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/client/test_client.py +0 -0
  112. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/client/test_roots.py +0 -0
  113. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/client/test_sampling.py +0 -0
  114. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/conftest.py +0 -0
  115. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/contrib/__init__.py +0 -0
  116. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/contrib/test_bulk_tool_caller.py +0 -0
  117. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/contrib/test_mcp_mixin.py +0 -0
  118. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/prompts/__init__.py +0 -0
  119. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/prompts/test_base.py +0 -0
  120. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/prompts/test_prompt_manager.py +0 -0
  121. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/resources/__init__.py +0 -0
  122. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/resources/test_file_resources.py +0 -0
  123. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/resources/test_function_resources.py +0 -0
  124. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/resources/test_resource_manager.py +0 -0
  125. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/resources/test_resources.py +0 -0
  126. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/server/__init__.py +0 -0
  127. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/server/test_file_server.py +0 -0
  128. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/server/test_import_server.py +0 -0
  129. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/server/test_lifespan.py +0 -0
  130. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/server/test_mount.py +0 -0
  131. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/server/test_proxy.py +0 -0
  132. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/server/test_run_server.py +0 -0
  133. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/test_servers/fastmcp_server.py +0 -0
  134. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/test_servers/sse.py +0 -0
  135. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/test_servers/stdio.py +0 -0
  136. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/tools/__init__.py +0 -0
  137. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/tools/test_tool_manager.py +0 -0
  138. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/__init__.py +0 -0
  139. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/openapi/__init__.py +0 -0
  140. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/openapi/conftest.py +0 -0
  141. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/openapi/test_openapi.py +0 -0
  142. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/openapi/test_openapi_advanced.py +0 -0
  143. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/openapi/test_openapi_fastapi.py +0 -0
  144. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/test_decorated_function.py +0 -0
  145. {fastmcp-2.2.3 → fastmcp-2.2.5}/tests/utilities/test_logging.py +0 -0
  146. {fastmcp-2.2.3 → fastmcp-2.2.5}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastmcp
3
- Version: 2.2.3
3
+ Version: 2.2.5
4
4
  Summary: The fast, Pythonic way to build MCP servers.
5
5
  Project-URL: Homepage, https://gofastmcp.com
6
6
  Project-URL: Repository, https://github.com/jlowin/fastmcp
@@ -141,7 +141,7 @@ FastMCP aims to be:
141
141
  ### Servers
142
142
  - **Create** servers with minimal boilerplate using intuitive decorators
143
143
  - **Proxy** existing servers to modify configuration or transport
144
- - **Compose** servers by into complex applications
144
+ - **Compose** servers into complex applications
145
145
  - **Generate** servers from OpenAPI specs or FastAPI objects
146
146
 
147
147
  ### Clients
@@ -112,7 +112,7 @@ FastMCP aims to be:
112
112
  ### Servers
113
113
  - **Create** servers with minimal boilerplate using intuitive decorators
114
114
  - **Proxy** existing servers to modify configuration or transport
115
- - **Compose** servers by into complex applications
115
+ - **Compose** servers into complex applications
116
116
  - **Generate** servers from OpenAPI specs or FastAPI objects
117
117
 
118
118
  ### Clients
@@ -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 tools.
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 tool functions, including:
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 tool function, simply add a parameter to your function signature and type-hint it as `Context`. FastMCP will automatically inject the context instance when your tool is called.
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 - tools that don't need it can omit the parameter.
69
- - Context is only available within tool functions during a request; attempting to use context methods outside a request will raise errors.
70
- - Context methods are async, so your tool function usually needs to be async as well.
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 tool execution during a request.
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 tool execution
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 tools, notify the client about the progress of the operation. This allows clients to display progress indicators and provide a better user experience.
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 tools to access files, configuration, or dynamically generated content.
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 tool needs to leverage the LLM's capabilities to process data or generate responses.
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 Other Components
282
+ ## Using Context in Different Components
283
283
 
284
- Currently, Context is primarily designed for use within tool functions. Support for Context in other components like resources and prompts is planned for future releases.
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
- ## Defining Prompts
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
- ### The MCP Session
174
+ ### Accessing MCP Context
175
175
 
176
- Prompts can access the MCP features via the `Context` object, just like tools.
176
+ <VersionBadge version="2.2.5" />
177
177
 
178
- ```python
179
- from fastmcp import Context
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 based on available data."""
184
- # Log the request
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
- Using the `ctx` parameter (based on its `Context` type hint), you can access:
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 & Templates
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
- ## Defining Resources
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
- ## Defining Resource Templates
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,15 +281,21 @@ 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.3" />
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>
289
+
253
290
 
254
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.
255
292
 
256
- ```python
293
+ ```python {15, 23}
257
294
  from fastmcp import FastMCP
258
295
 
259
296
  mcp = FastMCP(name="DataServer")
260
297
 
298
+
261
299
  # Standard parameter only matches one segment
262
300
  @mcp.resource("files://{filename}")
263
301
  def get_file(filename: str) -> str:
@@ -265,6 +303,7 @@ def get_file(filename: str) -> str:
265
303
  # Will only match files://<single-segment>
266
304
  return f"File content for: {filename}"
267
305
 
306
+
268
307
  # Wildcard parameter can match multiple segments
269
308
  @mcp.resource("path://{filepath*}")
270
309
  def get_path_content(filepath: str) -> str:
@@ -272,6 +311,7 @@ def get_path_content(filepath: str) -> str:
272
311
  # Can match path://docs/server/resources.mdx
273
312
  return f"Content at path: {filepath}"
274
313
 
314
+
275
315
  # Mixing standard and wildcard parameters
276
316
  @mcp.resource("repo://{owner}/{path*}/template.py")
277
317
  def get_template_file(owner: str, path: str) -> dict:
@@ -371,28 +411,6 @@ In this stacked decorator pattern:
371
411
 
372
412
  Templates provide a powerful way to expose parameterized data access points following REST-like principles.
373
413
 
374
- ### Custom Template Keys
375
-
376
- <VersionBadge version="2.2.0" />
377
-
378
- Similar to resources, you can provide custom keys when directly adding templates:
379
-
380
- ```python
381
- from fastmcp.resources import ResourceTemplate
382
-
383
- # Create a template with a function
384
- template = ResourceTemplate.from_function(
385
- my_function,
386
- uri_template="data://{id}/details",
387
- name="Data Details"
388
- )
389
-
390
- # Register with a custom key
391
- mcp._resource_manager.add_template(template, key="custom://{id}/view")
392
- ```
393
-
394
- This allows accessing the same template implementation through different URI patterns.
395
-
396
414
  ## Server Behavior
397
415
 
398
416
  ### Duplicate Resources