Index
Resource
Base class for all resources.
Source code in src/mcp/server/mcpserver/resources/base.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
set_default_name
classmethod
set_default_name(
name: str | None, info: ValidationInfo
) -> str
Set default name from URI if not provided.
Source code in src/mcp/server/mcpserver/resources/base.py
30 31 32 33 34 35 36 37 38 | |
ResourceManager
Manages MCPServer resources.
Source code in src/mcp/server/mcpserver/resources/resource_manager.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
add_resource
Add a resource to the manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource
|
Resource
|
A Resource instance to add. |
required |
Returns:
| Type | Description |
|---|---|
Resource
|
The added resource. If a resource with the same URI already exists, returns the existing resource. |
Source code in src/mcp/server/mcpserver/resources/resource_manager.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
add_template
add_template(
fn: Callable[..., Any],
uri_template: str,
name: str | None = None,
title: str | None = None,
description: str | None = None,
mime_type: str | None = None,
icons: list[Icon] | None = None,
annotations: Annotations | None = None,
meta: dict[str, Any] | None = None,
security: ResourceSecurity = DEFAULT_RESOURCE_SECURITY,
) -> ResourceTemplate
Add a template from a function.
Source code in src/mcp/server/mcpserver/resources/resource_manager.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
get_resource
async
get_resource(
uri: AnyUrl | str,
context: Context[LifespanContextT, RequestT],
) -> Resource | InputRequiredResult
Get resource by URI, checking concrete resources first, then templates.
A template function may return an InputRequiredResult instead of
resource content (the 2026-07-28 multi-round-trip flow); it is passed
through unchanged.
Raises:
| Type | Description |
|---|---|
ResourceNotFoundError
|
If no resource or template matches the URI. |
ResourceError
|
If a matching template fails to create the resource. |
Note
Pydantic's AnyUrl normalises percent-encoding and
resolves .. segments during validation, so a value
constructed as AnyUrl("file:///a/%2E%2E/b") arrives
here as file:///b. The JSON-RPC protocol layer passes
raw str values and is unaffected, but internal callers
wrapping URIs in AnyUrl should be aware that security
checks see the already-normalised form.
Source code in src/mcp/server/mcpserver/resources/resource_manager.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
list_resources
List all registered resources.
Source code in src/mcp/server/mcpserver/resources/resource_manager.py
129 130 131 132 | |
list_templates
list_templates() -> list[ResourceTemplate]
List all registered templates.
Source code in src/mcp/server/mcpserver/resources/resource_manager.py
134 135 136 137 | |
DEFAULT_RESOURCE_SECURITY
module-attribute
DEFAULT_RESOURCE_SECURITY = ResourceSecurity()
Secure-by-default policy: traversal, absolute paths, and null bytes rejected.
ResourceSecurity
dataclass
Security policy applied to extracted resource template parameters.
These checks run after :meth:~mcp.shared.uri_template.UriTemplate.match
has extracted and decoded parameter values. They catch path-traversal
and absolute-path injection regardless of how the value was encoded in
the URI (literal, %2F, %5C, %2E%2E).
Example::
# Opt out for a parameter that legitimately contains ..
@mcp.resource(
"git://diff/{+range}",
security=ResourceSecurity(exempt_params={"range"}),
)
def git_diff(range: str) -> str: ...
Source code in src/mcp/server/mcpserver/resources/templates.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
reject_path_traversal
class-attribute
instance-attribute
reject_path_traversal: bool = True
Reject values containing .. as a path component.
reject_absolute_paths
class-attribute
instance-attribute
reject_absolute_paths: bool = True
Reject values that look like absolute filesystem paths.
reject_null_bytes
class-attribute
instance-attribute
reject_null_bytes: bool = True
Reject values containing NUL (\x00). Null bytes defeat string
comparisons ("..\x00" != "..") and can cause truncation in C
extensions or subprocess calls.
exempt_params
class-attribute
instance-attribute
Parameter names to skip all checks for.
validate
Check all parameter values against the configured policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Mapping[str, str | list[str]]
|
Extracted template parameters. List values (from explode variables) are checked element-wise. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The name of the first parameter that fails, or |
str | None
|
all values pass. |
Source code in src/mcp/server/mcpserver/resources/templates.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
ResourceSecurityError
Bases: ValueError
Raised when an extracted parameter fails :class:ResourceSecurity checks.
Distinct from a simple None non-match so that template
iteration can stop at the first security rejection rather than
falling through to a later, possibly more permissive, template.
Source code in src/mcp/server/mcpserver/resources/templates.py
93 94 95 96 97 98 99 100 101 102 103 104 | |
ResourceTemplate
Bases: BaseModel
A template for dynamically creating resources.
Source code in src/mcp/server/mcpserver/resources/templates.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
from_function
classmethod
from_function(
fn: Callable[..., Any],
uri_template: str,
name: str | None = None,
title: str | None = None,
description: str | None = None,
mime_type: str | None = None,
icons: list[Icon] | None = None,
annotations: Annotations | None = None,
meta: dict[str, Any] | None = None,
context_kwarg: str | None = None,
security: ResourceSecurity = DEFAULT_RESOURCE_SECURITY,
) -> ResourceTemplate
Create a template from a function.
Raises:
| Type | Description |
|---|---|
InvalidUriTemplate
|
If |
Source code in src/mcp/server/mcpserver/resources/templates.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
matches
Check if a URI matches this template and extract parameters.
Delegates to :meth:UriTemplate.match for RFC 6570 extraction,
then applies this template's :class:ResourceSecurity policy
(path traversal, absolute paths).
Returns:
| Type | Description |
|---|---|
dict[str, str | list[str]] | None
|
Extracted parameters on success, or |
dict[str, str | list[str]] | None
|
doesn't match the template. |
Raises:
| Type | Description |
|---|---|
ResourceSecurityError
|
If the URI matches but an extracted
parameter fails security validation. Raising (rather
than returning |
Source code in src/mcp/server/mcpserver/resources/templates.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
create_resource
async
create_resource(
uri: str,
params: dict[str, Any],
context: Context[LifespanContextT, RequestT],
) -> Resource | InputRequiredResult
Create a resource from the template with the given parameters.
An InputRequiredResult returned by the template function is passed
through unchanged (the 2026-07-28 multi-round-trip flow); the retry's
answers arrive on ctx.input_responses, with ctx.request_state
carrying the echoed opaque state.
Raises:
| Type | Description |
|---|---|
ResourceError
|
If creating the resource fails. |
Source code in src/mcp/server/mcpserver/resources/templates.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
BinaryResource
Bases: Resource
A resource that reads from bytes.
Source code in src/mcp/server/mcpserver/resources/types.py
33 34 35 36 37 38 39 40 | |
read
async
read() -> bytes
Read the binary content.
Source code in src/mcp/server/mcpserver/resources/types.py
38 39 40 | |
DirectoryResource
Bases: Resource
A resource that lists files in a directory.
Source code in src/mcp/server/mcpserver/resources/types.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
validate_absolute_path
classmethod
Ensure path is absolute.
Source code in src/mcp/server/mcpserver/resources/types.py
187 188 189 190 191 192 193 | |
list_files
List files in the directory.
Source code in src/mcp/server/mcpserver/resources/types.py
195 196 197 198 199 200 201 202 203 204 205 206 207 | |
read
async
read() -> str
Read the directory listing.
Source code in src/mcp/server/mcpserver/resources/types.py
209 210 211 212 213 214 215 216 | |
FileResource
Bases: Resource
A resource that reads from a file.
Set is_binary=True to read the file as binary data instead of text.
Source code in src/mcp/server/mcpserver/resources/types.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
validate_absolute_path
classmethod
Ensure path is absolute.
Source code in src/mcp/server/mcpserver/resources/types.py
138 139 140 141 142 143 144 | |
set_binary_from_mime_type
classmethod
set_binary_from_mime_type(
is_binary: bool, info: ValidationInfo
) -> bool
Set is_binary based on mime_type if not explicitly set.
Source code in src/mcp/server/mcpserver/resources/types.py
146 147 148 149 150 151 152 153 | |
read
async
Read the file content.
Source code in src/mcp/server/mcpserver/resources/types.py
155 156 157 158 159 160 161 162 | |
FunctionResource
Bases: Resource
A resource that defers data loading by wrapping a function.
The function is only called when the resource is read, allowing for lazy loading of potentially expensive data. This is particularly useful when listing resources, as the function won't be called until the resource is actually accessed.
The function can return: - str for text content (default) - bytes for binary content - other types will be converted to JSON
Source code in src/mcp/server/mcpserver/resources/types.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
read
async
Read the resource by calling the wrapped function.
Source code in src/mcp/server/mcpserver/resources/types.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
from_function
classmethod
from_function(
fn: Callable[..., Any],
uri: str,
name: str | None = None,
title: str | None = None,
description: str | None = None,
mime_type: str | None = None,
icons: list[Icon] | None = None,
annotations: Annotations | None = None,
meta: dict[str, Any] | None = None,
) -> FunctionResource
Create a FunctionResource from a function.
Source code in src/mcp/server/mcpserver/resources/types.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
HttpResource
Bases: Resource
A resource that reads from an HTTP endpoint.
Source code in src/mcp/server/mcpserver/resources/types.py
165 166 167 168 169 170 171 172 173 174 175 176 | |
read
async
Read the HTTP content.
Source code in src/mcp/server/mcpserver/resources/types.py
171 172 173 174 175 176 | |
TextResource
Bases: Resource
A resource that reads from a string.
Source code in src/mcp/server/mcpserver/resources/types.py
23 24 25 26 27 28 29 30 | |
read
async
read() -> str
Read the text content.
Source code in src/mcp/server/mcpserver/resources/types.py
28 29 30 | |