Parameterized Database Lookup Tools
An on-demand lookup tool will allow the LLM to search currencies by name or code without loading the full list. Use a parameterized query (placeholder ?) to avoid prompt injection, and apply a row limit so responses stay bounded.
Este ejercicio forma parte del curso
Introduction to Model Context Protocol (MCP)
Instrucciones del ejercicio
- Define a tool called
lookup_currencies()that finds rows incurrencieswherenameorcodecontainsprefix(case-insensitive). - Use a parameterized SQL query to insert
prefixinto the?placeholders; useLIMIT 50to limit the number of rows returned.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# Add lookup_currencies(prefix): find rows where name or code contains prefix
@mcp.____()
def ____(____: str) -> str:
"""Find currencies whose code or name contains the given prefix."""
try:
# Use parameterized query and LIMIT 50
cursor = conn.execute(
"SELECT code, name FROM currencies WHERE name LIKE ? OR code LIKE ? LIMIT ____",
(f"%{____}%", f"%{____}%")
)
rows = cursor.fetchall()
return "\n".join(f"{row['code']} - {row['name']}" for row in rows)
except sqlite3.Error as e:
return f"Database error: {e}"
print(lookup_currencies("Euro"))