0Pricing
MCP Academy · درس

قراءة ملف من القرص

حوّلوا ملفًا محليًا إلى مورد MCP حي.

قراءة ملف من القرص درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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

الأسئلة الشائعة

هل درس «قراءة ملف من القرص» مجاني؟

نعم — نص درس «قراءة ملف من القرص» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.

ماذا ستتعلم في «قراءة ملف من القرص»؟

حوّلوا ملفًا محليًا إلى مورد MCP حي. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟

لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «قراءة ملف من القرص»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟

نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. شرح معرّفات URI الخاصة بالموارد
  2. تقديم مورد نصي ثابت
  3. قراءة ملف من القرص
  4. ضبط نوع MIME الصحيح
← العودة إلى MCP Academy