Eine REST-API als Tools verpacken
Machen Sie externe Endpunkte zu aufrufbaren Tools.
Eine REST-API als Tools verpacken ist eine kostenlose MCP Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des MCP Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Turn Endpoints into Tools
Any REST API can become MCP tools. Each endpoint becomes a callable function the model can invoke to fetch or send data. 🌐
Call HTTP from a Tool
Inside a tool you make an ordinary HTTP request, then return the response as text the model can read and reason about.
Prefer an Async Client
Use an async client like httpx so a slow API call does not block other requests while it waits for the network.
import httpx
async with httpx.AsyncClient() as client:
r = await client.get(url)Map Params to Arguments
Expose the endpoint query as tool arguments. The model passes a city or id, and you place it into the request safely.
@mcp.tool()
async def get_weather(city: str) -> str:
...Keep Secrets Server-Side
Store the API key in your server environment, never as a tool argument. The model should never see or send credentials.
key = os.environ["WEATHER_API_KEY"]Check the Status Code
Inspect the HTTP status before trusting the body. A 404 or 500 means you should report a clear error, not garbage data.
r.raise_for_status()Parse and Trim JSON
Most APIs return JSON. Parse it and return only the fields the task needs, so the model is not flooded with noise.
data = r.json()
return data["temp_c"]Give Each Tool One Job
Map one endpoint to one focused tool with a clear name. Granular tools are easier for the model to pick correctly.
Write Helpful Descriptions
Describe what the endpoint does and when to use it. A precise description is how the model knows to call this tool at all.
Handle Network Failures
Wrap the call so a timeout or connection drop returns a readable error message instead of crashing the whole server.
Set Sensible Timeouts
Always pass a timeout so a hung upstream cannot freeze your tool forever waiting on a reply that never comes.
r = await client.get(url, timeout=10.0)Quick Check
Where should an API key live?
Recap: REST as Tools
You wrapped endpoints as async tools: arguments in, server-side keys, status checks, and trimmed JSON out. Next, pool connections. 🎯
Häufig gestellte Fragen
Ist die Lektion „Eine REST-API als Tools verpacken“ kostenlos?
Ja — der vollständige Text von „Eine REST-API als Tools verpacken“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des MCP Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Eine REST-API als Tools verpacken“?
Machen Sie externe Endpunkte zu aufrufbaren Tools. Du übst MCP Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um MCP Academy zu starten?
Keine Vorkenntnisse erforderlich. MCP Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Eine REST-API als Tools verpacken“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser MCP Academy-Lektion Code schreiben und ausführen?
Ja. Jede MCP Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- SQL-Abfragen sicher bereitstellen
- Eine REST-API als Tools verpacken
- Verbindungen während der Lebensdauer bündeln
- Upstreams zwischenspeichern und begrenzen