Module 04 · Building an MCP Server
Scaffolding a TypeScript MCP Server From Scratch
Open lesson + course map
On this lesson
Course outline
Module 1 · Claude Code Fundamentals
Module 2 · CLI Workflows
Module 3 · Model Context Protocol Basics
Module 4 · Building an MCP Server
Build the smallest read-only server first. The official MCP TypeScript SDK and specification evolve, so pin the current supported SDK and follow its repository examples instead of memorizing an old constructor signature.
// concept
Define the Boundary
For a sample product server:
transport: local stdio
capability: get one approved product by SKU
data: synthetic JSON fixture
no network, write, secrets, or arbitrary file paths
result: typed product summary with source versionInitialize a TypeScript project, enable strict checks, add the official SDK at a pinned version, and create source/tests. Validate environment at startup and close cleanly on termination.
// concept
Implement With Schemas
Register get_product with a narrow description and strict SKU input. Server code validates again, retrieves from an allowlisted fixture, and returns controlled text or structured content per current SDK support. Return NOT_FOUND without leaking paths.
Do not let model input become a file name. Map SKU to an in-memory or database record through parameterized access.
// worked_example
Worked Example
The fixture contains PK-A14 with title, PKR price, availability label, and version. A valid call returns those approved fields. ../../.env, unknown properties, long input, and unknown SKU fail safely.
The server writes protocol messages only to the proper transport; diagnostics use stderr or structured logging as required so they do not corrupt stdio communication.
// failure_cases
Failure Cases to Diagnose
6 cases to diagnose
Latest dependency floats
pin and audit the lockfile.
Console diagnostics break stdio
separate protocol and logs.
Schema exists only in description
validate at runtime.
Arbitrary file read from SKU
use controlled lookup.
Stack trace returned to client
map safe errors.
No shutdown behavior
close resources and cancel work.
// concept
Operator Note
Expose a harmless health or version path through the mechanism recommended by the current SDK, or document how operators verify startup without invoking business actions. The response should identify server and capability version, not secrets or environment internals. Build and test on a clean checkout so an undeclared global dependency cannot make the server appear healthy only on the author’s machine.
// pakistan_angle
Pakistan Angle
Use labelled synthetic catalog data. A real Daraz/shop/customer export requires authorization and minimization. Do not embed client credentials in an example server.
Represent PKR amounts exactly and include whether the figure is sample, current source data, or pending verification. The model must not invent tax or delivery.
// hands_on
Hands-On Exercise
5 steps
Write the boundary contract.
Scaffold strict TypeScript with the official SDK.
Implement one read-only typed tool.
Test valid, invalid, traversal, unknown, and shutdown cases.
Record SDK/protocol versions.
// completion_rubric
Completion Rubric
6 checks — tick as you verify
// sources
Sources
3 official sources — check every claim yourself
// check_yourself
Check yourself
4 questions · answers and options are taken word-for-word from this course
1 / 4 · diagnose
Your work shows this failure mode: “Latest dependency floats.” What does the lesson tell you to do about it?