LESSON 10 / 18·2 min readIntermediate

Build a Custom TypeScript MCP Client

Must knowConcept lesson
01 Understand02 Recall03 Prepare

Client responsibilities

  • Connect the transport.
  • Initialize and negotiate capabilities.
  • Discover tools, resources, and prompts.
  • Orchestrate tool calls.
  • Validate results and present them safely.
  • Handle cancellation, timeouts, and cleanup.

Install the stable v2 client package:

Shell
npm install @modelcontextprotocol/client

The high-level client exposes helpers such as listing tools and calling a tool. Choose the exact transport constructor from the current SDK documentation because local stdio and remote HTTP setups differ.

Safe orchestration loop

  1. Fetch the server's tools.
  2. Provide an allowed subset to the model.
  3. Validate the model's request.
  4. Obtain user approval for sensitive actions.
  5. Execute the tool call with a timeout.
  6. Validate the result schema.
  7. Mark the result as untrusted context and provide it to the model.
  8. Enforce a maximum number of steps.

Infinite-loop prevention

  • Set a maximum number of tool calls per turn.
  • Detect repeated identical calls.
  • Enforce an overall deadline and token budget.
  • Define explicit terminal states.
  • Retry only transient, idempotent operations.
PAUSE & RECALL

Can you explain it without the notes?

Build a Custom TypeScript MCP Client

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Client responsibilities

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Authentication, Authorization, and OAuth

Adapted from 20_MCP_SERVER.md · “10. Build a Custom TypeScript MCP Client”. Examples are study fragments; framework behavior can depend on your version.