Backport: Support for Resource and ResourceTemplate metadata (#1928)

Co-authored-by: Jacem Elwaar <jacem@mcpappsbuilders.com>
This commit is contained in:
Max Isbey 2026-01-22 14:02:25 +00:00 • committed by GitHub
commit d891525958
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -358,6 +358,7 @@ class FastMCP(Generic[LifespanResultT]):
mimeType=resource.mime_type,
icons=resource.icons,
annotations=resource.annotations,
_meta=resource.meta,
)
for resource in resources
]
@ -373,6 +374,7 @@ class FastMCP(Generic[LifespanResultT]):
mimeType=template.mime_type,
icons=template.icons,
annotations=template.annotations,
_meta=template.meta,
)
for template in templates
]
@ -387,7 +389,7 @@ class FastMCP(Generic[LifespanResultT]):
try:
content = await resource.read()
return [ReadResourceContents(content=content, mime_type=resource.mime_type)]
return [ReadResourceContents(content=content, mime_type=resource.mime_type, meta=resource.meta)]
except Exception as e: # pragma: no cover
logger.exception(f"Error reading resource {uri}")
raise ResourceError(str(e))
@ -539,6 +541,7 @@ class FastMCP(Generic[LifespanResultT]):
mime_type: str | None = None,
icons: list[Icon] | None = None,
annotations: Annotations | None = None,
meta: dict[str, Any] | None = None,
) -> Callable[[AnyFunction], AnyFunction]:
"""Decorator to register a function as a resource.
@ -557,6 +560,7 @@ class FastMCP(Generic[LifespanResultT]):
title: Optional human-readable title for the resource
description: Optional description of the resource
mime_type: Optional MIME type for the resource
meta: Optional metadata dictionary for the resource
Example:
@server.resource("resource://my-resource")
@ -615,6 +619,7 @@ class FastMCP(Generic[LifespanResultT]):
mime_type=mime_type,
icons=icons,
annotations=annotations,
meta=meta,
)
else:
# Register as regular resource
@ -627,6 +632,7 @@ class FastMCP(Generic[LifespanResultT]):
mime_type=mime_type,
icons=icons,
annotations=annotations,
meta=meta,
)
self.add_resource(resource)
return fn