Odczytywanie pliku z dysku
Przekształć lokalny plik w aktywny zasób MCP.
Odczytywanie pliku z dysku to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej MCP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MCP Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. ✅
Często zadawane pytania
Czy lekcja „Odczytywanie pliku z dysku” jest bezpłatna?
Tak — pełny tekst „Odczytywanie pliku z dysku” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu MCP Academy, przejdź na CoddyKit PRO. Kurs MCP Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Odczytywanie pliku z dysku”?
Przekształć lokalny plik w aktywny zasób MCP. Ćwiczysz MCP Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć MCP Academy?
Nie wymagamy żadnego doświadczenia. MCP Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Odczytywanie pliku z dysku”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji MCP Academy?
Tak. Każda lekcja MCP Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wyjaśnienie identyfikatorów URI zasobów
- Udostępnianie statycznego zasobu tekstowego
- Odczytywanie pliku z dysku
- Ustawianie właściwego typu MIME