0Pricing
MCP Academy · Lesson

Wrap a REST API as Tools

Turn external endpoints into callable tools.

Wrap a REST API as Tools is a free MCP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🎯

Frequently asked questions

Is the “Wrap a REST API as Tools” lesson free?

Yes — the full text of “Wrap a REST API as Tools” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Wrap a REST API as Tools”?

Turn external endpoints into callable tools. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Wrap a REST API as Tools” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Expose SQL Queries Safely
  2. Wrap a REST API as Tools
  3. Pool Connections in Lifespan
  4. Cache & Rate-Limit Upstreams
← Back to MCP Academy