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

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 format every time

Templated Resources

  • Contain one or more parameters in the URI
  • Example: docs://documents/{doc_id}
  • Parameters are automatically parsed by Python MCP SDK and provided as keyword arguments to functions
  • Keyword argument names match the parameter names in the URI

Implementation Details

Decorator Syntax

  • Use @mcp. resource() decorator
  • First argument: URI (the resource address/route)
  • Second argument: mime_type (hints to client about data format)
  • application/json for structured JSON data
  • text/plain for plain text content

MIME Types

  • Tells the client what type of data is being returned
  • Client uses this to determine how to deserialize the data
  • Examples: application/json, text/plain, binary data

Function Return Values

  • Return native Python data types (lists, strings, dictionaries)
  • Python MCP SDK automatically serializes to string format
  • No need to manually convert to JSON or other formats

Practical Example: Document System

Resource 1 (Direct): List all documents

  • URI: docs://documents
  • MIME type: application/json
  • Returns: list of document names/IDs

Resource 2 (Templated): Fetch single document

  • URI: docs://documents/{doc_id}
  • MIME type: text/plain
  • Returns: document contents as string
  • Includes error handling for non-existent documents

Testing

  • Use MCP Inspector to test resources
  • List direct resources separately from templated resources
  • Verify returned data structure and MIME type

Takeaways

  • Resources expose data to MCP clients through static or templated URIs, with one resource per distinct read operation
  • Templated resources use parameters in the URI (e. g. , docs://documents/{doc_id}) that are automatically parsed and passed as keyword arguments to handler functions
  • Use the @mcp. resource() decorator with a URI and mime_type argument to define resources; the SDK automatically serializes Python return values to strings
  • MIME types (application/json, text/plain, etc. ) tell clients how to deserialize the returned data
  • Test resources with MCP Inspector to verify data structure and MIME type correctness
Flashcards 8 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 5 questions