0Pricing
MCP Academy · درس

إنشاء المحتوى وقت القراءة

احسبوا محتوى المورد عند طلبه.

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

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

Lazy, Not Eager

A dynamic resource doesn't store a fixed body. Its content is computed on demand, only when a client actually asks to read it. ⚡

The Read Triggers the Work

Your resource function runs at the moment of a read request. Nothing happens until a client reads it, so you never waste effort building content no one wants.

Return the Body

Whatever your function returns becomes the resource's content. Return a string and the client receives that exact text as the resource body.

@mcp.resource("time://now")
def now() -> str:
    return datetime.now().isoformat()

Fresh Every Read

Because the body is rebuilt each time, the model always sees current data. Read the clock twice and you get two different timestamps.

Use the URI Variables

Combine read-time building with a template. The captured parameters shape what you compute, so the same function serves tailored content per URI.

@mcp.resource("greeting://{name}")
def greet(name: str) -> str:
    return f"Hello, {name}!"

Pull From Anywhere

At read time you can do real work: query a database, call an API, or read a file. The result of that work is what you return as the body.

@mcp.resource("stats://summary")
def summary() -> str:
    return db.fetch_summary()

Async When You Wait

If building content means waiting on I/O, make the function async. The server can serve other reads while yours awaits a slow network call.

@mcp.resource("feed://latest")
async def latest() -> str:
    return await fetch_feed()

Shape the Returned Text

You decide the format of the body. Returning JSON text is common when the model needs structured data it can reason over cleanly.

import json

def report() -> str:
    return json.dumps({"ok": True})

Keep It Quick

A read should feel snappy. If the work is heavy, cache the result so repeated reads don't redo expensive computation every single time.

Handle Missing Data

What if the requested thing doesn't exist? Raise an error from the function so the client learns the resource couldn't be read, rather than returning junk.

def get_doc(doc_id: str) -> str:
    if doc_id not in store:
        raise ValueError("not found")
    return store[doc_id]

Why This Matters

Read-time building turns a static idea into a live window on your data. The model always reads what is true right now, not a stale snapshot. 🎯

Quick Check

When does the body of a dynamic resource get produced?

Recap

You saw how returning a value from a resource function builds content on read, giving the model fresh, computed data every time. Next: listed vs templated resources. 🚀

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

هل درس «إنشاء المحتوى وقت القراءة» مجاني؟

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

ماذا ستتعلم في «إنشاء المحتوى وقت القراءة»؟

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

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

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

كم من الوقت يستغرق درس «إنشاء المحتوى وقت القراءة»؟

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

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

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

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

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