liveBook · Manning

MCP resource

As explained in Hugging Face in Action

An MCP resource is a server-provided data source that an MCP client can inspect or access through the Model Context Protocol.

An MCP resource is a server-provided data source that an MCP client or tool can inspect or access. Resources supply external data—such as files, web pages, configuration, or database records—for an LLM to process, while Tools perform actions and Prompts guide interactions. section 13.1.1 section 13.1.4

Registering resources in Python

Use the MCP resource decorator to expose data through URI-like identifiers. A text resource can validate a path, read the file, and return its contents; a configuration resource can return a static value; a PDF resource can extract text with PyMuPDF. Identifiers such as text://{file_path}, config://app, and pdf://{file_path} tell clients how to access each resource. section 13.2.6

Listing 13.2 Adding Resources to the MCP server
# server.py

# ==============================================
# resources, tools, and prompts to be added here

#==========
# Resources
#==========
@mcp.resource("text://{file_path}")  
def get_file(file_path: str) -> str:
    actual_path = os.path.abspath(file_path)                           #1
    if not os.path.exists(actual_path):
        raise FileNotFoundError(f"Error: File '{actual_path}' not found!")  
    with open(actual_path, "r", encoding="utf-8") as file:
        return file.read()

@mcp.resource("config://app")
def get_config() -> str:
    """Static configuration data"""
    return "Version 1.1"

@mcp.resource("pdf://{file_path}")
def get_pdf_data(file_path: str) -> str:  
    text = ""
    actual_path = os.path.abspath(file_path)
    if not os.path.exists(actual_path):
        raise FileNotFoundError(f"Error: File '{actual_path}' not found!")
    with fitz.open(actual_path) as doc:
        for page in doc:
            text += page.get_text() + "\n"
    return text

Inspect the complete server listing to see path validation, file reading, static configuration, PDF extraction, and URI-based registration. section 13.2.6

Test resources with the MCP Inspector:

  • Connect to the running server and select List Resources to inspect parameterless resources such as config://app. section 13.2.9
  • Select List Templates for parameterized resources, enter a file path, and choose Read Resource to retrieve text or PDF content. section 13.2.9

How Hugging Face in Action covers it

How deep it goes
Explains resource roles, URI-like registration, path validation, file and PDF extraction, and Inspector testing without detailing protocol internals.
Examples
Demonstrates text-file retrieval, static configuration, PDF extraction, URI templates, and MCP Inspector reads.
What it assumes
Assumes a running MCP server and introduces resource creation with the official Python SDK and MCP Inspector.
What you can do after
Build it from scratch by registering Python functions as resources and validating them with the MCP Inspector.
How it connects
Connects Resources with Tools, Prompts, MCP servers, MCP clients, and the Model Context Protocol.

Also explained in

Compare how 4 books explain MCP resource →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.