MCP Authentication and Best Practices

An agent will call any tool you list for it, as often as it likes, on behalf of whoever it is talking to. The MCP layer is the doorman for that traffic. The trouble at this door is rarely exotic: either the check on the way in is slightly wrong, or a failure on the way out is reported in a form the caller cannot use.

The door: verify once, pass an identity inward

In LVD, the MCP layer (L6, ν = 0.9) sits above the API layer and exposes Tools to models. The book requires every MCP connection to be authenticated, so that only agents you have authorised can invoke tools, and treats the mechanism as a close relative of the one at the API layer, adapted to the MCP transport.

A sound shape for that check has two steps. Verify the credential at the edge, and convert what it says into a typed identity. Everything behind the door then reads the identity from request context instead of picking at a raw token again.

export function requireCaller(req: Request, res: Response, next: NextFunction): void {
  let caller: Caller;
  try {
    const token = bearerToken(req.get("authorization")); // throws if absent or malformed
    caller = parseCaller(verifyToken(token));            // throws on bad signature, expiry or claims
  } catch {
    res.status(401).json({ error: "authentication failed" });
    return;
  }
  res.locals.caller = caller;
  next(); // outside the try: a failure further down must not be reported as a 401
}

The comment on the last line is the point of the example. Put next() inside the try and the guard is also guarding everything that runs after it. In a Koa-style chain, await next() rejects with whatever the later handlers throw, and in some frameworks a synchronous throw can unwind through the call as well. Either way, a bug in a tool handler would arrive at the client as "authentication failed", and the on-call engineer would spend an hour looking in the wrong place.

The rule generalises: a try block should wrap only the code whose failure you mean to translate.

A failure the agent can read

A model reads your error as text and decides what to do next. That makes the shape of a failure part of the interface. The book separates two kinds. A protocol-level error means the exchange itself broke. A tool-level error means the tool ran and could not do what was asked, and it is reported inside a normal response with an error flag set (isError: true).

The flag matters because of the two alternatives. An exception that surfaces as a transport failure looks like infrastructure trouble, so the agent gives up or retries blindly. A plain-text message with no flag looks like success, and a cheerful model will tell the user the booking was cancelled when it was not. With the flag, the agent is able to retry, ask the user a question, or choose a different tool.

return {
  isError: true,
  content: [{ type: "text", text: "Booking B-1044 was not found. Ask the user to confirm the reference." }],
};

Write that message for a reader who cannot see your code. Say what was wrong with the input and what a valid next step looks like. Leave out stack traces, SQL and file paths: the model does not need them, and the user may end up seeing them.

Why these two belong together

Both are about translation at a boundary. Authentication translates a credential into an identity or a refusal; error handling translates a failure inside a Tool into something a model can reason about. L6 is an adapter, and the cleaner its translations, the less the layers below it need to know about protocols. The neighbouring layer's equivalent is covered in the authentication sublayer article, and the overall role of L6 in the MCP layer article. For how adapters like this are tested, see testing the protocol layers.

The takeaway

Authenticate in a small try that ends before next(), carry a typed identity inward, and report tool failures as flagged results that tell the model what to do next.

What the book covers

  • Session management that carries the authenticated user's context across a multi-call conversation
  • Forwarding the caller's token to the API layer in a distributed deployment
  • Naming conventions that help an agent choose the right tool
  • Four performance practices for MCP, including cached tool discovery