Spaces:
Running

tool-research-v4 / index.html
evalstate's picture
evalstate HF Staff
Launch v4 scrollable interactive explainer (Responses, Messages & Chat Completions)
eae7b5f verified
Raw History Blame Contribute Delete
20.2 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tool call pipeline: from MCP schema to model tokens and back</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Space+Grotesk:wght@500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<header class="topbar" id="topbar">
<div class="topbar-inner">
<a class="brand" href="#intro">Tool call pipeline</a>
<nav class="topnav" aria-label="Chapters">
<a href="#ch1"><span class="nav-num">1. </span>API request</a>
<a href="#ch2"><span class="nav-num">2. </span>Model prompt</a>
<a href="#ch3"><span class="nav-num">3. </span>Generation</a>
<a href="#ch4"><span class="nav-num">4. </span>Turn 2</a>
<a href="#compare">Compare</a>
</nav>
</div>
</header>
<main id="main">
<section class="band band-base intro-band" id="intro" aria-labelledby="page-title">
<div class="container intro-inner">
<h1 class="display-title" id="page-title">How a tool definition reaches a model, and how its call comes back</h1>
<p class="intro-copy">Models never read your JSON schema directly. An inference server renders your tool into prompt text through a chat template, samples raw tokens until a stop condition, and parses those tokens back into JSON.</p>
<div class="pipeline-map" id="pipeline-map" aria-label="Round-trip tool call pipeline">
<div class="map-row">
<a class="map-node" href="#ch1" data-node="1">
<span class="map-step">1. MCP definition</span>
<code class="map-snippet" id="map-sig-1">search_models · inputSchema</code>
</a>
<svg class="map-arrow" viewBox="0 0 28 16" fill="none" aria-hidden="true">
<line x1="2" y1="8" x2="22" y2="8" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></line>
<polyline points="17 3 23 8 17 13" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></polyline>
</svg>
<a class="map-node" href="#ch1" data-node="2">
<span class="map-step">2. API request</span>
<code class="map-snippet" id="map-sig-2">POST /v1/responses</code>
</a>
<svg class="map-arrow" viewBox="0 0 28 16" fill="none" aria-hidden="true">
<line x1="2" y1="8" x2="22" y2="8" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></line>
<polyline points="17 3 23 8 17 13" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></polyline>
</svg>
<a class="map-node" href="#ch2" data-node="3">
<span class="map-step">3. Rendered prompt</span>
<code class="map-snippet" id="map-sig-3">&lt;tools&gt;{"type":"function"...</code>
</a>
<svg class="map-arrow" viewBox="0 0 28 16" fill="none" aria-hidden="true">
<line x1="2" y1="8" x2="22" y2="8" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></line>
<polyline points="17 3 23 8 17 13" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></polyline>
</svg>
<a class="map-node" href="#ch3" data-node="4">
<span class="map-step">4. Generated tokens</span>
<code class="map-snippet" id="map-sig-4">&lt;tool_call&gt;&lt;function=...</code>
</a>
<svg class="map-arrow" viewBox="0 0 28 16" fill="none" aria-hidden="true">
<line x1="2" y1="8" x2="22" y2="8" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></line>
<polyline points="17 3 23 8 17 13" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></polyline>
</svg>
<a class="map-node" href="#ch3" data-node="5">
<span class="map-step">5. API response</span>
<code class="map-snippet" id="map-sig-5">type: "function_call"</code>
</a>
</div>
<a class="map-return" href="#ch4" aria-label="Jump to Turn 2: tool result appended to prompt">
<svg class="map-return-svg" viewBox="0 0 1200 34" fill="none" preserveAspectRatio="none" aria-hidden="true">
<path d="M 1080 2 C 1080 26, 610 26, 600 4" stroke="currentColor" stroke-width="1.5" stroke-dasharray="4 4" fill="none"></path>
<polyline points="595 10 600 3 606 10" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></polyline>
</svg>
<span class="map-return-label">Turn 2: client executes tool and appends result back into prompt</span>
</a>
</div>
</div>
</section>
<section class="band band-base chapter-band" id="ch1" aria-labelledby="ch1-title">
<div class="container">
<div class="chapter-prose">
<h2 class="chapter-title" id="ch1-title">1. From MCP definition to API request</h2>
<p>An MCP server exposes each tool with name, description, and inputSchema. Before sending a prompt, your client SDK re-wraps that exact schema to match the target API endpoint.</p>
</div>
<div class="ch1-grid">
<div class="pane pane-editor">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title">Your MCP tool</h3>
<span class="pane-status" id="ed-status">Live editor</span>
</div>
<div class="pane-head-right">
<div class="seg" id="ctl-tool-ch1" role="radiogroup" aria-label="Preset MCP tool">
<button type="button" role="radio" data-v="search" aria-checked="true">search_models</button>
<button type="button" role="radio" data-v="ticket" aria-checked="false">create_ticket</button>
</div>
<button type="button" class="text-btn" id="btn-reset-tool" hidden>Reset</button>
</div>
</div>
<div class="pane-subbar" id="ed-subbar">
<span class="subbar-prompt">Edit the JSON below or try:</span>
<button type="button" class="try-btn" id="btn-try-sort">Add a "sort" field</button>
<button type="button" class="try-btn" id="btn-try-limit">Tighten limit max to 10</button>
</div>
<div class="pane-body pane-body-ch1">
<div class="ed-error" id="ed-error" hidden role="alert"></div>
<div class="json-editor" id="mcp-editor" contenteditable="plaintext-only" spellcheck="false" role="textbox" aria-multiline="true" aria-label="MCP tool JSON editor"></div>
</div>
</div>
<div class="flow-gutter" aria-hidden="true">
<svg viewBox="0 0 28 24" fill="none">
<line x1="2" y1="12" x2="22" y2="12" stroke="currentColor" stroke-width="1.75" stroke-linecap="round"></line>
<polyline points="16 6 23 12 16 18" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"></polyline>
</svg>
</div>
<div class="pane">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title">API request</h3>
</div>
<div class="pane-head-right">
<div class="seg" id="ctl-api-ch1" role="radiogroup" aria-label="API format">
<button type="button" role="radio" data-v="responses" aria-checked="true">Responses</button>
<button type="button" role="radio" data-v="messages" aria-checked="false">Messages</button>
<button type="button" role="radio" data-v="chat" aria-checked="false">Chat Completions</button>
</div>
<button type="button" class="icon-btn" id="btn-copy-req" aria-label="Copy API request JSON">Copy</button>
</div>
</div>
<div class="pane-subbar" id="req-transform-bar">
<span class="subbar-note" id="req-transform-text">Adds flat "type": "function", renames inputSchema to parameters, and sends turns in input.</span>
</div>
<div class="pane-body pane-body-ch1">
<pre class="code-well" id="api-request-code"></pre>
</div>
</div>
</div>
<p class="hinge-line">Notice what has not happened yet: regardless of API format, your JSON Schema has not been interpreted for any specific model.</p>
</div>
</section>
<div class="model-scope" id="model-scope">
<div class="rail-sentinel" id="rail-sentinel" aria-hidden="true"></div>
<nav class="model-rail" id="model-rail-wrap" aria-label="Select model">
<div class="container rail-inner">
<div class="rail-scroll" id="rail-scroll" role="tablist" aria-label="Open-weight models">
<div class="rail-tabs" id="model-rail"></div>
<div class="rail-indicator" id="rail-indicator" aria-hidden="true"></div>
</div>
<div class="rail-more-wrap">
<button type="button" class="more-btn" id="btn-more-models" aria-haspopup="menu" aria-expanded="false" aria-controls="more-menu">More (5) ▾</button>
</div>
</div>
</nav>
<div class="more-menu" id="more-menu" role="menu" hidden></div>
<section class="band band-alt chapter-band" id="ch2" aria-labelledby="ch2-title">
<div class="container">
<div class="chapter-prose">
<h2 class="chapter-title" id="ch2-title">2. From API request to rendered prompt</h2>
<p>Language models do not consume JSON objects; they consume a single sequence of text and control tokens. Before tokenization, the inference server runs the model's chat template to flatten messages and tools into the rendered prompt.</p>
</div>
<div class="callout-bar" id="ch2-callout">
<div class="callout-top">
<strong class="callout-model" id="ch2-model-head">Qwen3.8-27B · XML tags</strong>
<button type="button" class="fields-toggle" id="btn-fields-toggle" aria-expanded="false" aria-controls="ch2-fields-drawer">All 15 schema fields kept ▾</button>
</div>
<p class="callout-prose" id="ch2-model-cap"></p>
<p class="callout-dropped" id="ch2-dropped-inline" hidden></p>
<div class="fields-drawer" id="ch2-fields-drawer" hidden></div>
</div>
<div class="ch2-grid">
<div class="pane">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title">Template source</h3>
</div>
<div class="pane-head-right" id="ch2-left-actions"></div>
</div>
<div class="pane-subbar" id="ch2-tmpl-subbar" hidden></div>
<div class="pane-body pane-body-ch2" id="ch2-tmpl-body"></div>
</div>
<div class="pane">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title" id="ch2-right-title">The prompt Qwen3.8 sees</h3>
</div>
<div class="pane-head-right">
<label class="toggle-check"><input type="checkbox" id="chk-only-tools"><span>Focus on tool regions</span></label>
<button type="button" class="icon-btn" id="btn-copy-prompt" aria-label="Copy rendered prompt">Copy</button>
</div>
</div>
<div class="pane-body pane-body-ch2" id="ch2-prompt-body"></div>
</div>
</div>
</div>
</section>
<section class="band band-base chapter-band" id="ch3" aria-labelledby="ch3-title">
<div class="container">
<div class="chapter-prose">
<h2 class="chapter-title" id="ch3-title">3. Model generation: from raw tokens to API response</h2>
<p>Given the rendered prompt, the model samples one token at a time until a stop token fires. The serving engine scans that raw text for model-specific call syntax and translates both the arguments and the stop reason into your API's JSON response.</p>
</div>
<div class="ch3-grid">
<div class="pane">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title">Generated tokens</h3>
</div>
<div class="pane-head-right">
<div class="seg" id="ctl-reply" role="radiogroup" aria-label="Model reply mode">
<button type="button" role="radio" data-v="tool" aria-checked="true">Calls a tool</button>
<button type="button" role="radio" data-v="text" aria-checked="false">Plain text</button>
</div>
</div>
</div>
<div class="pane-subbar pane-subbar-between">
<div class="seg" id="ctl-play" role="radiogroup" aria-label="Token playback">
<button type="button" role="radio" data-v="play" aria-checked="false">Play</button>
<button type="button" role="radio" data-v="step" aria-checked="false">Step</button>
<button type="button" role="radio" data-v="all" aria-checked="true">All</button>
</div>
<span class="stream-count" id="stream-count">0 / 0 pieces</span>
</div>
<div class="pane-body pane-body-ch3" id="ch3-stream-body">
<pre class="code-well" id="token-stream"></pre>
</div>
</div>
<div class="pane">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title">Engine mapping</h3>
</div>
</div>
<div class="pane-body pane-body-ch3 mapping-body" id="ch3-mapping-body"></div>
</div>
<div class="pane">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title">API response</h3>
</div>
<div class="pane-head-right">
<div class="seg" id="ctl-api-ch3" role="radiogroup" aria-label="Response API format">
<button type="button" role="radio" data-v="responses" aria-checked="true">Responses</button>
<button type="button" role="radio" data-v="messages" aria-checked="false">Messages</button>
<button type="button" role="radio" data-v="chat" aria-checked="false">Chat</button>
</div>
<button type="button" class="icon-btn" id="btn-copy-resp" aria-label="Copy API response JSON">Copy</button>
</div>
</div>
<div class="pane-body pane-body-ch3">
<pre class="code-well" id="api-response-code"></pre>
</div>
</div>
</div>
<details class="engine-drawer" id="engine-drawer" open>
<summary class="engine-drawer-head">
<span class="engine-drawer-title">Serving-engine parser and finish-reason source</span>
<span class="engine-drawer-tabs" id="eng-tabs" role="tablist" aria-label="Serving engine">
<button type="button" class="subtab" role="tab" data-eng="vllm" aria-selected="true">vLLM</button>
<button type="button" class="subtab" role="tab" data-eng="sglang" aria-selected="false">SGLang</button>
<button type="button" class="subtab" role="tab" data-eng="llama" aria-selected="false">llama.cpp</button>
<button type="button" class="subtab" role="tab" data-eng="input" aria-selected="false">Input</button>
</span>
</summary>
<div class="engine-drawer-note" id="engine-adapter-note" hidden>
Note: Open-weight engines run tool extraction once in their core parser (shown below in OpenAI finish_reason terms), then project that result onto /v1/responses or /v1/messages adapters.
</div>
<div class="engine-drawer-body" id="engine-source-body"></div>
</details>
</div>
</section>
<section class="band band-alt chapter-band" id="ch4" aria-labelledby="ch4-title">
<div class="container">
<div class="chapter-prose">
<h2 class="chapter-title" id="ch4-title">4. Closing the loop: sending the tool result back</h2>
<p>Your client executes the requested function locally and appends the output to the conversation history. On Turn 2, the chat template serializes both the assistant call and the tool result into the continuation prompt so the model can finish answering.</p>
</div>
<div class="ch4-grid">
<div class="pane">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title" id="ch4-left-title">Client tool result</h3>
<span class="pane-status" id="ch4-left-status">function_call_output</span>
</div>
</div>
<div class="pane-body pane-body-ch4" id="ch4-left-body"></div>
</div>
<div class="pane">
<div class="pane-head">
<div class="pane-head-left">
<h3 class="pane-title" id="ch4-right-title">Turn-2 continuation prompt</h3>
</div>
</div>
<div class="pane-body pane-body-ch4" id="ch4-right-body"></div>
</div>
</div>
</div>
</section>
</div>
<section class="band band-base chapter-band" id="compare" aria-labelledby="compare-title">
<div class="container">
<div class="compare-head">
<div class="compare-head-text">
<h2 class="chapter-title" id="compare-title">How 8 diverse models write the same tool call</h2>
<p class="compare-sub">Select any row to inspect that model's template and generated tokens above.</p>
</div>
<div class="compare-controls">
<div class="seg" id="ctl-compare-scope" role="radiogroup" aria-label="Models shown">
<button type="button" role="radio" data-v="curated" aria-checked="true">Diverse 8</button>
<button type="button" role="radio" data-v="all" aria-checked="false">All 13</button>
</div>
<div class="seg" id="ctl-tool-cmp" role="radiogroup" aria-label="Comparison preset tool">
<button type="button" role="radio" data-v="search" aria-checked="true">search_models</button>
<button type="button" role="radio" data-v="ticket" aria-checked="false">create_ticket</button>
</div>
</div>
</div>
<div class="compare-table-wrap">
<table class="compare-table">
<thead>
<tr>
<th scope="col">Model</th>
<th scope="col">Serialized tool schema</th>
<th scope="col">Call syntax</th>
<th scope="col">Stop token</th>
<th scope="col">Fields kept</th>
</tr>
</thead>
<tbody id="compare-tbody"></tbody>
</table>
</div>
</div>
</section>
</main>
<footer class="site-footer">
<div class="container footer-inner">
<div class="footer-repos" id="footer-repos"></div>
<div class="footer-keys">Keyboard: <code>[</code> / <code>]</code> previous or next model</div>
</div>
</footer>
<script type="module" src="./app.js"></script>
</body>
</html>