Content-Length: 283580 | pFad | https://github.com/modelcontextprotocol/python-sdk/issues/1320

C9 mcp/resource issue when test with Claude Desktop · Issue #1320 · modelcontextprotocol/python-sdk · GitHub
Skip to content

mcp/resource issue when test with Claude Desktop #1320

@barryzhounb

Description

@barryzhounb

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://github.com/{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://github.com/greeting.txt","description":"A sample text resource named greeting","mimeType":"text/plain"},{"name":"help","title":"Help Documentation","uri":"file://github.com/help.txt","description":"A sample text resource named help","mimeType":"text/plain"},{"name":"about","title":"About This Server","uri":"file://github.com/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

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/modelcontextprotocol/python-sdk/issues/1320

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy