Skip to content

Commit eb83a33

Browse files
feat: Allow ResourceContents objects to be returned directly from read_resource handlers
Updated the low-level server's read_resource decorator to accept TextResourceContents and BlobResourceContents objects directly, in addition to the existing ReadResourceContents. This provides more flexibility for resource handlers to construct and return properly typed ResourceContents objects with full control over all properties. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7a933fe commit eb83a33

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,14 @@ async def handler(_: Any):
311311

312312
def read_resource(self):
313313
def decorator(
314-
func: Callable[[AnyUrl], Awaitable[str | bytes | Iterable[ReadResourceContents]]],
314+
func: Callable[
315+
[AnyUrl],
316+
Awaitable[
317+
str
318+
| bytes
319+
| Iterable[ReadResourceContents | types.TextResourceContents | types.BlobResourceContents]
320+
],
321+
],
315322
):
316323
logger.debug("Registering handler for ReadResourceRequest")
317324

@@ -346,7 +353,10 @@ def create_content(data: str | bytes, mime_type: str | None):
346353
content = create_content(data, None)
347354
case Iterable() as contents:
348355
contents_list = [
349-
create_content(content_item.content, content_item.mime_type) for content_item in contents
356+
content_item
357+
if isinstance(content_item, types.ResourceContents)
358+
else create_content(content_item.content, content_item.mime_type)
359+
for content_item in contents
350360
]
351361
return types.ServerResult(
352362
types.ReadResourceResult(

0 commit comments

Comments
 (0)