-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Open
Labels
questionFurther information is requestedFurther information is requested
Description
Question
I have a piece of code for mcp/resource -- list_resource and read_resource. And test it with Claude Desktop, list_resource works, but read_resource doesn't work.
SAMPLE_RESOURCES = {
"greeting": {
"content": "Hello! This is a sample text resource.",
"title": "Welcome Message",
},
"help": {
"content": "This server provides a few sample text resources for testing.",
"title": "Help Documentation",
},
"about": {
"content": "This is the simple-resource MCP server implementation.",
"title": "About This Server",
},
}
@app.list_resources()
async def list_resources() -> list[types.Resource]:
"""List available resources for the MCP server."""
ctx = app.request_context
if ctx is None:
raise ValueError("Request context is not available.")
logger.info("Listing resources for the MCP server.")
resources = [
types.Resource(
uri=FileUrl(f"file:///{name}.txt"),
name=name,
title=SAMPLE_RESOURCES[name]["title"],
description=f"A sample text resource named {name}",
mimeType="text/plain",
)
for name in SAMPLE_RESOURCES.keys()
]
return resources
@app.read_resource()
async def read_resource(uri: AnyUrl) -> types.ReadResourceResult:
if uri.path is None:
raise ValueError(f"Invalid resource path: {uri}")
name = uri.path.replace(".txt", "").lstrip("/")
#if name not in SAMPLE_RESOURCES:
# raise ValueError(f"Unknown resource: {uri}")
result = types.ReadResourceResult(
contents=[
types.TextResourceContents(
uri=uri,
name=name,
title=SAMPLE_RESOURCES[name]["title"],
mimeType="text/plain",
text="Hello! This is a sample text resource.",
)
]
)
return result
Basically,list_resource works and return expected results.
{"jsonrpc":"2.0","id":3,"result":{"resources":[{"name":"greeting","title":"Welcome Message","uri":"file:///greeting.txt","description":"A sample text resource named greeting","mimeType":"text/plain"},{"name":"help","title":"Help Documentation","uri":"file:///help.txt","description":"A sample text resource named help","mimeType":"text/plain"},{"name":"about","title":"About This Server","uri":"file:///about.txt","description":"A sample text resource named about","mimeType":"text/plain"}]}}
But when read_resource, it logs error as following.
{"jsonrpc":"2.0","id":4,"error":{"code":0,"message":"\'tuple\' object has no attribute \'content\'"}}
What's wrong or missing for above code? I suspect it is a bug, anyway, please help double check it.
Additional Context
No response
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested