Diskten Dosya Okuma
Yerel bir dosyayı canlı bir MCP kaynağına dönüştürün.
Diskten Dosya Okuma, CoddyKit'te ücretsiz bir MCP Academy dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, MCP Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. MCP Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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. ✅
Sıkça Sorulan Sorular
“Diskten Dosya Okuma” dersi ücretsiz mi?
Evet — “Diskten Dosya Okuma” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve MCP Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. MCP Academy kursu toplamda 4 dersten oluşur.
“Diskten Dosya Okuma” dersinde ne öğreneceğim?
Yerel bir dosyayı canlı bir MCP kaynağına dönüştürün. MCP Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
MCP Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te MCP Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.
“Diskten Dosya Okuma” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu MCP Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her MCP Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.