Claude With The Anthropic Api
← All lessons
Lesson 67Claude With The Anthropic Api

Accessing resources

Summary audio

No audio recap for this lesson.

Study notes

Core Concept

  • MCP client needs a function to request resources defined in the MCP server
  • The read_resource function fetches a resource by URI and parses the response based on MIME type

Implementation Steps

Imports Required

  • json module for parsing JSON data
  • AnyUrl from PyDantic for type handling

Function Logic

  • Call await self. session. read_resource() with the resource URI wrapped in AnyUrl
  • Extract the first element from result. contents list
  • Access the mime_type property to determine data format

Response Handling

  • If MIME type is application/json: parse text content using json. loads() and return parsed data
  • Otherwise: return raw text content as-is

Integration

  • The read_resource function is called from other parts of the codebase
  • Enables the CLI application to fetch and insert resource contents into prompts
  • Resources are sent to Claude without requiring tool calls to read them

Testing

  • CLI application can list available resources and select them
  • Selected resource contents are automatically included in prompts sent to Claude

Takeaways

  • MCP client uses read_resource() to fetch resources by URI and parse responses based on MIME type
  • Response handling differs by MIME type: application/json requires json. loads() parsing, other types return raw text
  • Resources are automatically fetched and inserted into prompts sent to Claude without requiring tool calls
Flashcards 8 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 5 questions