0Pricing
MCP Academy · Lesson

Read a File from Disk

Turn a local file into a live MCP resource.

Read a File from Disk is a free MCP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Fixed to Live

Returning a hardcoded string is fine, but real servers expose actual files. You can read a file from disk and serve its contents live.

Read Inside the Handler

Your resource function simply opens the file and returns its text. Because it runs on each request, the client always gets the current content. 📂

A Basic File Resource

Open the file, read it, and return the string. The resource now reflects whatever is saved on disk at read time.

@mcp.resource("file:///notes.txt")
def notes():
    with open("notes.txt") as f:
        return f.read()

Use Pathlib Cleanly

The pathlib library reads small files in one line and handles paths safely across systems, which keeps your handler short and clear.

from pathlib import Path

@mcp.resource("file:///notes.txt")
def notes():
    return Path("notes.txt").read_text()

Handle Missing Files

A file can vanish or be renamed. Wrap the read so a missing file returns a clear error message instead of crashing the server.

try:
    return Path(p).read_text()
except FileNotFoundError:
    raise ValueError("File not found")

Mind the Encoding

Text files have an encoding. Read with utf-8 explicitly so accented and non-English characters survive the trip to the model.

Path("notes.txt").read_text(encoding="utf-8")

Watch the File Size

Returning a huge file floods the model with tokens and may hit limits. Keep served files small, or read only the part the model truly needs.

Never Trust the Path

If a path comes from input, an attacker could request files outside your folder. Always confine reads to a safe base directory you control.

Resolve Within a Base

Compute an absolute path and check it stays inside your allowed root. This blocks path traversal tricks like climbing up with dot-dot.

base = Path("data").resolve()
target = (base / name).resolve()
if base not in target.parents: raise ValueError("Denied")

Read-Only by Nature

Resources only read; they never write. Serving a file as a resource exposes its contents without ever letting the model modify the file.

Verify the Live Read

Change the file on disk, then read the URI again. Seeing the new text proves your server reads live rather than caching stale content.

Quick Check

Security check on serving files.

Recap

Read a file inside the handler so content stays live. Set utf-8, handle missing files, watch size, and confine paths to a safe root. ✅

Frequently asked questions

Is the “Read a File from Disk” lesson free?

Yes — the full text of “Read a File from Disk” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Read a File from Disk”?

Turn a local file into a live MCP resource. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Read a File from Disk” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Resource URIs Explained
  2. Serve a Static Text Resource
  3. Read a File from Disk
  4. Set the Right MIME Type
← Back to MCP Academy