0Pricing
MCP Academy · Lektion

Eine Datei von der Festplatte lesen

Machen Sie aus einer lokalen Datei eine dynamische MCP-Ressource.

Eine Datei von der Festplatte lesen ist eine kostenlose MCP Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des MCP Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. ✅

Häufig gestellte Fragen

Ist die Lektion „Eine Datei von der Festplatte lesen“ kostenlos?

Ja — der vollständige Text von „Eine Datei von der Festplatte lesen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des MCP Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Eine Datei von der Festplatte lesen“?

Machen Sie aus einer lokalen Datei eine dynamische MCP-Ressource. Du übst MCP Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um MCP Academy zu starten?

Keine Vorkenntnisse erforderlich. MCP Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Eine Datei von der Festplatte lesen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser MCP Academy-Lektion Code schreiben und ausführen?

Ja. Jede MCP Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Ressourcen-URIs erklärt
  2. Eine statische Textressource bereitstellen
  3. Eine Datei von der Festplatte lesen
  4. Den richtigen MIME-Typ festlegen
← Zurück zu MCP Academy