VereskLearn

Module 7 · MCP

Lesson 7.5

Writing a client

A client sends requests with unique ids, waits for each reply, and checks that the id matches.

Lesson 49 of 622 min read

The problem

Why it matters

Without it

The code sends a request and assumes the next line is its reply. A late message is then read as the wrong answer.

With it

Each request has its own id. The client takes the reply with that id, and a mismatch is reported as an error.

How it works

  • Open the session first

    A client sends initialize first and waits for the reply. The other requests go out only after that reply arrives. The sample does this before it lists the tools.

    1. initialize
    2. Wait for reply
    3. tools/list
    4. tools/call
    The order of requests in one session.
  • Each request gets its own number

    A counter goes up by one for each request, so every request has a unique id. The reply to request 2 carries the id 2. The sample uses the ids 1, 2, and 3.

    CallRequest idReply id
    initialize11
    tools/list22
    tools/call33
    The ids in the sample session.
    let nextId = 1;const first = nextId++;const second = nextId++;console.log(first + ", " + second);

    Output

    1, 2
  • A mismatched id is an error

    The client compares the reply id with the request id. A mismatch means the reply belongs to another request, so the client throws an error. The sample never gets a mismatch, but the check protects every call.

    • Request id 2
    • Reply id 3belongs to another request
    Two ids that do not match.
    const requestId = 2;const replyId = 3;if (replyId !== requestId) {  const message = "id mismatch: got " + replyId;  console.log(message + ", expected " + requestId);}

    Output

    id mismatch: got 3, expected 2
  • The result goes back to the caller

    After the check, the client returns the result field to its caller. The tool list and the call text both come back this way. A reply that fails the check never reaches the caller.

    Returned to the caller

    • The result field
    • The tool list and the call text

    Refused by the client

    • A reply with a wrong id
    • A reply for a request that was never sent
    What the client returns, and what it refuses.

Full code

Read the program

See the full code

Common mistakes

What goes wrong

  • Skipping the id check. A wrong reply is then used as the answer.
  • Reusing an id. Two open requests with one id cannot be told apart.
  • Sending requests before initialize returns. Some servers refuse them.

Use it when and skip it when

Is it the right tool?

Use it when

  • Your app must call tools on a server that it does not own.
  • You are testing a server with a client of your own.

Skip it when

  • The host already provides a client that does what you need.

Words to know

Terms in this lesson

Client
The code that sends requests to one server and reads the replies.
Session
The period after initialize, when other requests may be sent.
Request id
A unique number that links a reply to its request.
Mismatch
A reply whose id does not match the request it answers.
Full code