Implementar herramientas, recursos y prompts
Cree las tres primitivas para su caso de uso.
Implementar herramientas, recursos y prompts es una lección gratuita de MCP Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de MCP Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de MCP Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎯
Preguntas frecuentes
¿La lección «Implementar herramientas, recursos y prompts» es gratis?
Sí — el texto completo de «Implementar herramientas, recursos y prompts» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de MCP Academy, actualiza a CoddyKit PRO. El curso de MCP Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Implementar herramientas, recursos y prompts»?
Cree las tres primitivas para su caso de uso. Practicas MCP Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar MCP Academy?
No se requiere experiencia previa. MCP Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Implementar herramientas, recursos y prompts»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de MCP Academy?
Sí. Cada lección de MCP Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Delimitar el proyecto y las herramientas
- Implementar herramientas, recursos y prompts
- Probar, inspeccionar y reforzar
- Documentar y publicar el servidor