0Pricing
MCP Academy · درس

تنفيذ الأدوات والموارد والتعليمات

أنشئوا البدائيات الثلاث جميعها لحالة الاستخدام.

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

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

Build All Three Primitives

With a plan in hand, you now write real code. This lesson wires up your tools, resources, and prompts into one working server. 🛠️

Set Up the Backend

First, give the server something to act on. A tiny store, like a list or a SQLite file, holds the notes every primitive will share.

import sqlite3

db = sqlite3.connect("notes.db")
db.execute("CREATE TABLE IF NOT EXISTS notes(id INTEGER PRIMARY KEY, title TEXT, body TEXT)")

Write the Add Tool

Your first tool creates a note. The decorator publishes it, and the type hints turn the arguments into a schema the model can fill.

@mcp.tool()
def add_note(title: str, body: str) -> str:
    db.execute("INSERT INTO notes(title, body) VALUES (?, ?)", (title, body))
    db.commit()
    return "Saved."

Write the Search Tool

Add a second tool that searches notes by keyword. Returning a short list of matches keeps the result easy for the model to use.

@mcp.tool()
def search_notes(query: str) -> list[str]:
    rows = db.execute("SELECT title FROM notes WHERE body LIKE ?", (f"%{query}%",))
    return [r[0] for r in rows]

Describe Tools Clearly

Give each tool a plain docstring. That text becomes its description, and a clear one helps the model pick the right tool at the right time.

Expose a Resource

Now serve read-only context. A resource uri lets the model fetch one note without changing anything in your store.

@mcp.resource("note://{note_id}")
def get_note(note_id: int) -> str:
    row = db.execute("SELECT body FROM notes WHERE id = ?", (note_id,)).fetchone()
    return row[0] if row else "Not found."

Capture the URI Variable

The braces in the uri become a function argument. MCP pulls note_id straight from the path, so each read targets exactly one record.

Register a Prompt

Add a reusable prompt the user can trigger. It frames a request so callers get consistent, high-quality instructions every time.

@mcp.prompt()
def summarize(note_id: int) -> str:
    return f"Read note://{note_id} and summarize it in three bullet points."

Return Useful Text

Keep every result concise and human-readable. Short, clear strings save model tokens and make the user-facing output far easier to read.

Mix, But Keep Roles Clear

Tools act, resources read, and prompts instruct. Keeping each role distinct means clients and the model always know what to expect from each.

Wire the Entry Point

End the file by running the server so a client can connect. The run call starts the loop and hands control to the transport.

if __name__ == "__main__":
    mcp.run()

Quick Check

Let us check the resource uri detail.

Recap

You implemented all three primitives: tools that act, a resource that reads, and a prompt that instructs. The capstone server now does real work. 🎯

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

هل درس «تنفيذ الأدوات والموارد والتعليمات» مجاني؟

نعم — نص درس «تنفيذ الأدوات والموارد والتعليمات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. تحديد نطاق المشروع والأدوات
  2. تنفيذ الأدوات والموارد والتعليمات
  3. الاختبار والفحص وتعزيز الأمان
  4. توثيق الخادم ونشره
← العودة إلى MCP Academy