Introduction To Model Context Protocol
← All lessons
Lesson 10Introduction To Model Context Protocol

Accessing resources

Summary audio

No audio recap for this lesson.

Study notes

Overview

  • MCP client needs ability to request resources defined in the MCP server
  • A single read_resource() function handles all resource requests from the client

Function Implementation

Imports Required

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

Function Logic

  • Takes a URI argument specifying which resource to fetch
  • Calls await self. session. read_resource() with the URI wrapped in AnyUrl
  • Extracts the first item from result. contents list

Response Parsing by MIME Type

  • Check the mime_type property from the resource response
  • If MIME type is application/json: parse text content using json. loads() and return parsed data
  • Otherwise: return raw text content as-is

Resource Type Handling

  • Uses isinstance() check for ResourceTextContents to validate resource type
  • Different MIME types trigger different parsing strategies

Integration

  • The read_resource() function is called by other parts of the application
  • Enables features like document mention/insertion in CLI prompts
  • Allows Claude to receive document contents directly in prompts without needing to call tools

Takeaways

  • MCP client uses a single read_resource() function to fetch any resource from the server by URI
  • Response parsing depends on MIME type: application/json requires json. loads() parsing, other types return raw text
  • The function extracts content from result. contents list and validates it's a ResourceTextContents type
  • Resource reading enables Claude to receive document contents directly in prompts without tool calls
Flashcards 7 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 1 questions