MCP servers and clients in TypeScript · Chapter 9 of 16
MCP meets the model
By the end of this chapter you can write the part of a host that connects a model to MCP servers: turn MCP tools into tools the model understands, run the calls it asks for through an MCP client, send the results back, and repeat until it answers, with a limit for when it never does. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Clients and production
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of MCP servers and clients in TypeScript are free. This one comes with the all labs plan.
Why you need this
Since chapter 1 you have heard the same sentence: the model never speaks MCP, the host does. Rajat's assistant is a host, and so far nobody has written that part. His team wired three tools by hand last month, and when the stock server grew a fourth, the assistant did not know about it for a week.
Simran wants the assistant to offer the model whatever the server lists, the day it lists it. That is the point of discovery: the host asks the server what it has, every time it connects, and describes it to the model. Gopal wants one more thing: when a tool fails, the assistant should say why, not make something up.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
bridge.ts: MCP tools as OpenAI tools
In bridge.ts, export:
toOpenAI(tool), which takes one MCP tool (from listTools) and returns OpenAI's tool format, { type: 'function', function: { name, description, parameters } }, with the tool's name, its description (an empty string when it has none) and its input schema, as it is, as the parameters;
async toolsForModel(client), which lists the client's tools and returns them all through toOpenAI, in order.
- Task 2
one-turn.ts: one question, one round trip
In one-turn.ts, start bazaar-server.ts over stdio and ask lab-chat Where is PO-10187? with the server's tools offered (use bridge.ts). Run every tool call the model asks for through the MCP client, append the model's message and one tool message per call (its tool_call_id, and the result's text as the content), ask lab-chat again with the same tools, and print the final answer.
Close the client at the end. Do not open the database in this file.
- Task 3
agent.ts: the loop
In agent.ts, export async function ask(question, client, { model, maxSteps }), with model 'lab-chat' and maxSteps 5 by default: it offers the client's tools to the model with the question, and while the model asks for tools, runs each call over MCP and sends each result's text back as a tool message (failed results too, so the model can read them), then asks again. It returns the model's answer.
A step is one request to the model: after maxSteps requests without an answer, it returns No answer after <maxSteps> steps.
Keep anything that runs inside if (import.meta.main), so the check can import ask.
- Task 4 · interview question
Interview: fix-me.ts, a host that loses the results
fix-me.ts answers Where is PO-10187? and Is PO-12345 delivered? with bazaar-server.ts's tools, printing one answer per question. Fix its three bugs, changing as little as you can:
it sends each result's structured content, an object, as the tool message's content, where the model expects text;
its tool messages have no tool_call_id;
when a tool fails, it sends nothing back.
typecheck finds none of them: a cast hides two.
What you will learn
- A tool call in one paragraph: what the model sends, what you send back.
- MCP tools as OpenAI tools: moving three fields.
- The round trip: model, host, client, server, and back.
- The loop, a step limit, and errors sent back to the model.
- Several servers and tools with the same name.
More in Clients and production
Build "MCP meets the model" yourself
Bazaar Bhai, a chain of 40 kirana stores in Jaipur, exposes its stock and orders once, to every AI tool. You build it in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of MCP servers and clients in TypeScript are free. This one comes with the all labs plan.