MCP servers and clients in TypeScript · Chapter 7 of 16

Prompts

By the end of this chapter you can offer prompts from an MCP server: templates a user picks and fills in, with arguments, returning one message or several, with Bazaar Bhai's data already inside. You can list and get them from a client, send them to a model, and say who controls each of MCP's three building blocks. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The protocol
  • 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

Every Monday, forty store managers type nearly the same request into the assistant: "plan the restocking of my store for the week, here is what is low...". Each one writes it a little differently, half of them forget to paste the stock, and some ask for "a week" while others mean ten days. Gopal wants one good request, written once, that every manager can pick from a menu and fill in with a store id.

That is neither a tool (the model does not decide to run it) nor a resource (it is not data to read). It is a prompt: a template the user chooses. Simran shows the team that MCP has a building block for exactly this, and that the server, which owns the data, can put the current stock into the request itself.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    prompts.ts: a restocking plan, ready to send

    In prompts.ts, export createServer(), which builds and returns an McpServer (any name), served with serveStdio only when the file is run. Register the prompt restock_plan with a description and two string arguments: store_id, required, and days, optional ('7' when not given). It returns one user message that:

    names the store's area and its id;

    says the next <days> days;

    lists every product the store has below its reorder level in /workspace/data/bazaar.db, with its id, name, units and reorder level, and no other product.

  2. Task 2

    prompts.ts: an email, already started

    Register the prompt supplier_email with one required string argument, order_id. It returns two messages:

    a user message asking for an email to the order's supplier, with the order id, the product's name, the quantity and the supplier's name;

    an assistant message that starts the email, Dear <supplier name>, so the model carries on from there.

    For an order that does not exist, throw a ProtocolError with the invalid params code (-32602) and a message that names the order id.

  3. Task 3

    use-prompt.ts: from the menu to the model

    In use-prompt.ts, start prompts.ts over stdio with the SDK's Client (process.execPath) and:

    print one line per prompt, its name and its arguments in brackets with ? after an optional one, like restock_plan(store_id, days?);

    get restock_plan for store BB08 over 3 days, then close the client;

    send its messages to lab-chat with the openai package, as they are, each with its role and its text, and print the reply.

    Do not open the database in this file.

  4. Task 4 · interview question

    Interview: fix-me.ts, a prompt that misbehaves

    fix-me.ts's prompt weekly_order(store_id, weeks) asks for an order covering the weeks asked for plus one safety week. A store id must be required and weeks optional, 1 by default. Fix its three bugs, changing as little as you can:

    store_id has a default, so it is optional and a forgotten store silently means BB01;

    asked for 2 weeks it says the next 21 weeks;

    its callback returns something the SDK cannot send as a prompt, so every client gets an error.

    Keep the prompt's name, its arguments' names and its text.

What you will learn

  • registerPrompt: a template with arguments, listed and fetched by name.
  • Arguments are strings on the wire; required unless optional.
  • One message or several: user and assistant messages.
  • Data inside the prompt, from the database, and errors for bad arguments.
  • Who controls what: tools, resources and prompts side by side.

More in The protocol

Build "Prompts" 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.