Enrutar llamadas a herramientas mediante un LLM
Permita que un modelo decida qué herramienta invocar.
Enrutar llamadas a herramientas mediante un LLM es una lección gratuita de MCP Academy en CoddyKit. Esta es la lección 4 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.
Let the Model Decide
So far you chose which tool to call. The real power comes when you hand the decision to an LLM, letting the model pick the right tool for a request.
The Bridge Pattern
Your client becomes a bridge: it discovers MCP tools, describes them to the model, and runs whatever tool the model asks for.
Translate the Schemas
You convert each MCP tool name, description, and inputSchema into the tool format the model API expects, so the model can see its options.
tools = [
{"name": t.name, "description": t.description,
"input_schema": t.inputSchema}
for t in listed.tools
]Send Tools with the Prompt
You pass that tool list alongside the user message when you call the model. The model now knows exactly what actions are on the table.
msg = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=tools,
messages=messages,
)The Model Requests a Tool
When the model wants an action, its reply contains a tool_use block with the tool name and the arguments it chose.
for block in msg.content:
if block.type == "tool_use":
...Run It Over MCP
You take that requested name and input and forward them to the server with call_tool, the same invocation you already know.
result = await session.call_tool(
block.name, block.input)Return the Result
Send the tool output back to the model as a tool_result so it can read what happened and continue reasoning.
tool_result = {"type": "tool_result",
"tool_use_id": block.id,
"content": result.content[0].text}Loop Until Done
The model may call several tools in a row. You keep the loop running until it stops requesting tools and gives a final answer.
Two Protocols Meet
Your bridge speaks two languages: the model API for reasoning and MCP for tools. The client quietly translates between them.
Why Not Hardcode
Hardcoding tool choice does not scale. Letting the model route means new MCP servers add new abilities with no change to your logic.
Keep a Human in the Loop
For risky actions, pause before running the tool the model picked and ask the user to approve it first. Routing should not mean blind trust.
Quick Check
What does the model send when it wants your client to run a tool?
Recap: The Model in Charge
You wired up the full loop: describe MCP tools to an LLM, run the tool it picks via call_tool, return the result, and repeat until the answer is ready.
Preguntas frecuentes
¿La lección «Enrutar llamadas a herramientas mediante un LLM» es gratis?
Sí — el texto completo de «Enrutar llamadas a herramientas mediante un LLM» 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 «Enrutar llamadas a herramientas mediante un LLM»?
Permita que un modelo decida qué herramienta invocar. 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 4 de 4.
¿Cuánto tiempo toma la lección «Enrutar llamadas a herramientas mediante un LLM»?
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
- Abrir una sesión de cliente
- Descubrir herramientas y recursos
- Invocar herramientas desde el código
- Enrutar llamadas a herramientas mediante un LLM