Skip to content
mentoor

Topic 2 of 5 · 5 min read

Turning model tool calls into MCP requests

Goal: Write the loop that maps a model's tool call to a tools/call request and feeds the result back to the model in the right shape.

Your client is a translator: it turns each MCP tool into the format your model's API expects, and turns each tool call the model makes into a tools/call request.

Check yourself · 1 of 3

Which MCP tool field becomes input_schema in a Claude tool definition?

Choose one answer

Why it matters

MCP tools and model tool definitions look alike but are not the same objects. An MCP tool has name, description and inputSchema. Claude expects input_schema, OpenAI wants a function wrapper with parameters, and Gemini uses function declarations. A client that gets the mapping wrong sends the model a broken tool or loses the result on the way back.

How to do it

  1. Convert definitions. For each MCP tool, copy the name, the description and the JSON Schema into the target API's shape. Keep a map from the name the model sees to the server and original tool name.
  2. Run the loop. Send the user message and the converted tools to the model. If the reply contains tool calls, run each one; if not, you have the final answer.
  3. Call the server. Send tools/call with name and arguments. The arguments come straight from the model, so they can be wrong. Expect the server to reject bad ones.
  4. Map the result back. A result has a content list. Items can be text, image (base64 data with a mimeType) or other types. A server may also return structuredContent, a JSON object. Put the text in the tool-result message. Pass an image only if your model API accepts images in tool results. Keep structuredContent as JSON text when the model needs the fields.
  5. Keep the pairing. Return each result with the id of the call it answers, or the model API will refuse the next request.

Before and after

// Before: drops everything but the first item
const out = await client.callTool({ name, arguments: args });
messages.push({ role: "user", content: [{
  type: "tool_result", tool_use_id: call.id,
  content: out.content[0].text,
}]});
// After: keep all parts, keep the error flag
const out = await client.callTool({ name, arguments: args });
const parts = out.content.map((c) =>
  c.type === "text" ? { type: "text", text: c.text }
  : c.type === "image" ? { type: "image",
      source: { type: "base64", media_type: c.mimeType, data: c.data } }
  : { type: "text", text: `[unsupported ${c.type} content]` });
messages.push({ role: "user", content: [{
  type: "tool_result", tool_use_id: call.id,
  content: parts, is_error: out.isError === true,
}]});

What goes wrong

  • Reading only the first content item. A tool returns a chart image and a caption, and the model sees only the caption.
  • Losing the error flag. A failed call is presented to the model as a normal result.
  • Mixing up ids. With parallel calls, results returned in the wrong order under the wrong id give the model false facts.

Examples

One tool, three shapes

MCP tool: { name: "get_weather", description: "...", inputSchema: {...} }.

Claude format: the same name and description, with the schema under input_schema.

OpenAI format: { type: "function", name: "get_weather", parameters: {...} }.

The schema body is identical in each. Only the wrapper and the key name change.

An image result

A screenshot tool returns content: [{ type: "image", data: "iVBOR...", mimeType: "image/png" }]. The client sends it to a vision-capable model as a base64 image block. For a model that cannot read images in tool results, the client replaces it with a text note, so the model knows something was dropped.

Key points

  • Convert each MCP tool into the target API's format, and keep a map back to the server and tool name.
  • tools/call takes the tool name and the arguments the model produced, which may be invalid.
  • A result's content list can hold text, images and other items, so do not read only the first one.
  • Pass the isError flag on as the API's error marker so the model knows the call failed.
  • Return every result with the id of the tool call it answers.

Common mistakes

  • Passing the MCP inputSchema under the wrong key for the model API.
  • Forwarding only the first content item of a tool result.
  • Dropping isError, so a failure looks like a success to the model.
  • Sending parallel-call results back with mismatched ids.

Check yourself · 2 of 3

A tools/call result has two content items: a text caption and an image. What should the client do?

Choose one answer

Check yourself · 3 of 3

The model calls a tool with count set to the text "ten", but the tool schema says count is an integer. What should the client do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic