MCP servers and clients in TypeScript · Chapter 6 of 16

Resources

By the end of this chapter you can offer data from an MCP server as resources: static ones and templates with a variable in the URI, each with a description and a mime type. You can read them from a client, fail properly when one does not exist, and say when something should be a resource and when a tool. 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

Rajat's assistant still gets the return policy wrong. Ask the lab model with nothing in front of it:

$ lab ask "Within how many days can packaged products be returned?"
You can return any product within 30 days with the bill for a full refund.

The policy says 7 days. Nobody needs a tool to fix this: the model does not have to decide anything or do anything. The application should read the policy and put it in front of the model before it answers. Gopal wants the same for his restock planner, which never talks to a model at all: it downloads each store's stock every night and works on the file.

Simran draws the line for the team: tools are actions the model asks for; resources are data the application reads. MCP has both, and a server that offers only tools makes every app go through the model to reach plain data.

What you build: 4 tasks

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

  1. Task 1

    resources.ts: the policy and the stores

    In resources.ts, export createServer(), which builds and returns an McpServer (any name), served with serveStdio only when the file is run. Register two static resources, each with a description:

    bazaar://policy, the text of /workspace/data/policy.md exactly as it is, with the mime type text/markdown;

    bazaar://stores, a JSON array of every store in /workspace/data/bazaar.db, sorted by id, each an object with id, area and manager, with the mime type application/json.

    The mime type goes in the resource's metadata, which the listing shows, and on the part a read returns.

  2. Task 2

    resources.ts: a template for every store

    Add two resource templates, each with a description:

    bazaar://stores/{store_id}, the store's row from the stores table (every column) as a JSON object, mime type application/json;

    bazaar://stores/{store_id}/stock.csv, the store's stock as CSV, mime type text/csv: the header product_id,name,quantity,reorder_level, then one line per product the store carries, sorted by product id.

    For a store that does not exist, such as BB99, both throw ResourceNotFoundError with a message that names the store; the client's read then rejects with the protocol error -32602.

  3. Task 3

    reader.ts: an app that reads resources

    In reader.ts, start resources.ts over stdio with the SDK's Client (process.execPath) and print:

    one line per resource it lists, Resource: <uri> (<mime type>);

    one line per resource template, Template: <uri template> (<mime type>);

    then read bazaar://stores/BB11/stock.csv, count the products whose quantity is below their reorder level, and print BB11 has <n> products below the reorder level.

    Close the client at the end. Do not open the database or the data folder in this file.

  4. Task 4 · interview question

    Interview: tool-or-resource.ts, which is which

    tool-or-resource.ts lists eight of Bazaar Bhai's capabilities. Export an object ANSWERS with the eight keys given there; each value is a pair: 'tool' or 'resource', and a one-line reason (a few words at least).

    A tool is something the model decides to call, with arguments it chooses, and anything that changes data; a resource is data the application reads by URI and puts in front of the model or the user.

What you will learn

  • Resources: data with a URI, a name, a description and a mime type.
  • Static resources and templates ({store_id} in the URI).
  • Reading them from a client, and what comes back.
  • Not found versus a crash: rejected promises, not tool results.
  • Tool or resource: who decides to use it, and whether it changes anything.

More in The protocol

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