Defining Resources

Resources in MCP servers allow you to expose data to clients, similar to GET request handlers in a typical HTTP server.

Resources provide a clean way to expose read-only data from your MCP server, making it easy for clients to fetch information without the complexity of tool calls. They’re perfect for scenarios where you need to fetch information rather than perform actions.

How Resources Work

Resources follow a request-response pattern.

  1. When your client needs data, it sends a ReadResourceRequest with a URI to identify which resource it wants.
  2. The MCP server processes this request and returns the data in a ReadResourceResult.

Sequence diagram: the user types “What’s in the @…”, our code asks the MCP client for a list of document names for the autocomplete, and the client sends a ReadResourceRequest with the docs://documents URI to the MCP server

Types of Resources

There are two types of resources:

  • Direct resources
  • Templated resources

Direct resources

Direct resources have static URIs that never change. They’re perfect for operations that don’t need parameters.

@mcp.resource(
    "docs://documents", # URI doesn't contain any params
    mime_type="application/json"
)
def list_docs() -> list[str]:
    return list(docs.keys())

Templated resources

Templated resources include parameters in their URIs. The Python SDK automatically parses these parameters and passes them as keyword arguments to your function.

@mcp.resource(
    "docs://documents/{doc_id}", # URI contain one or more params
    mime_type="text/plain"
)
def fetch_doc(doc_id: str) -> str:
    if doc_id not in docs:
        raise ValueError(f"Doc with id {doc_id} not found")
    return docs[doc_id]

Implementation Details

Resources can return any type of data - strings, JSON, binary data, etc. Use the mime_type parameter to give clients a hint about what kind of data you’re returning:

  • "application/json" for structured data
  • "text/plain" for plain text
  • "application/pdf" for binary files

The MCP Python SDK automatically serializes your return values. You don’t need to manually convert objects to JSON strings - just return the data structure and let the SDK handle serialization.

Testing Your Resources

Start your server with:

uv run mcp dev mcp_server.py

Then connect to the inspector in your browser. You’ll see two sections:

  • Resources - Lists your direct/static resources
  • Resource Templates - Lists your templated resources

截屏2026-10-22 21.39.26

docs