0Pricing
MCP Academy · レッスン

ツール、リソース、プロンプトを実装する

ユースケースに必要な3つのプリミティブをすべて構築します。

「ツール、リソース、プロンプトを実装する」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。

「ツール、リソース、プロンプトを実装する」で何を学びますか?

ユースケースに必要な3つのプリミティブをすべて構築します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

MCP Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「ツール、リソース、プロンプトを実装する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMCP Academyレッスンでコードを書いて実行できますか?

はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. プロジェクトとツールの範囲を決める
  2. ツール、リソース、プロンプトを実装する
  3. テスト、調査、強化
  4. サーバーをドキュメント化して公開する
← MCP Academyに戻る