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

Defining resources

Summary audio

No audio recap for this lesson.

Study notes

Overview

  • Resources allow MCP servers to expose data to clients
  • Define one resource for each distinct read operation
  • Accessed through the MCP client via read resource requests

Resource Types

Direct (Static) Resources

  • Have a static URI that never changes
  • Example: docs://documents
  • Same URI every time it's accessed

Templated Resources

  • Contain one or more parameters in the URI
  • Example: docs://documents/{doc_id}
  • Parameters are automatically parsed by the Python MCP SDK
  • Parameter names become keyword arguments in the handler function
  • Allow customization and variety in what clients can request

Implementation Flow

  • Client types trigger a read resource request
  • Request includes a URI (the "address" of the resource)
  • MCP server receives request, matches URI to handler function
  • Server executes function and returns result in read resource result message
  • Client receives data and uses it (e. g. , display in autocomplete)

Defining Resources in Python MCP SDK

Decorator Syntax

  • Use @mcp. resource() decorator
  • First argument: URI string
  • Second argument: MIME type (hints to client about data format)

MIME Types

  • application/json - for structured JSON data
  • text/plain - for plain text content
  • SDK automatically serializes return values to strings

Function Parameters

  • Templated resource parameters automatically become keyword arguments
  • Parameter name in URI matches the function argument name
  • Example: {doc_id} in URI → doc_id parameter in function

Practical Example

  • List documents resource: Direct resource returning all available document names for autocomplete
  • Fetch document resource: Templated resource returning contents of a specific document by ID
  • Error handling: Validate requested resource exists before returning data

Takeaways

  • Resources expose data to clients through URIs; define one resource per distinct read operation
  • Resources can be static (fixed URI) or templated (parameterized URI with dynamic values)
  • Use @mcp. resource() decorator with URI string and MIME type to define resources in Python MCP SDK
  • Templated URI parameters automatically become function keyword arguments (e. g. , {doc_id} → doc_id parameter)
  • Client requests trigger server to match URI to handler function, execute it, and return result in appropriate format
Flashcards 10 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 5 questions