The Anatomy of a Tool

Open ten business functions in a typical codebase and you will find ten ways of checking input, ten shapes of return value and a few names that explain nothing. Nobody planned it that way. It accumulated. LVD avoids the drift by building every tool from the same template, so a colleague, an API route or an AI model can always predict what it will find.

One factory, five parts

In an LVD system, tools are not written freehand. A single factory function, createTool, accepts a configuration object and hands back two things: a serialisable descriptor that protocol layers use to register the tool, and a callable handler that does the work at runtime. The configuration has a slot for each of the five parts of a well-formed tool.

  1. Name. A unique identifier that starts with a verb. Protocol layers route on it and agents select by it.
  2. Description. One plain sentence saying what the tool does and when to reach for it. It is written for people and for models.
  3. Schemas. Two Layer 1 definitions: one for inbound parameters, one for the shape of the result payload.
  4. Handler. An asynchronous function holding the business logic.
  5. Annotations. Optional metadata for protocol layers and agents.
export const { tool, handler } = createTool({
  name: "cancelRide",
  description: "Cancel a booked ride before the driver arrives.",
  schemas: { parameters: CancelRideParams, data: RideSchema },
  annotations: { readOnlyHint: false, idempotentHint: true },
  handler: async ({ rideId }) => {
    const ride = await rideRepository.cancel(rideId);
    return { text: `Ride ${rideId} cancelled`, data: ride };
  },
});

Nothing in that file mentions HTTP, a prompt or a message format. It declares a capability, and the layers above decide how to present it.

The handler never validates

The rule that surprises people most is that a handler contains no input checking at all. Validation is delegated entirely to the Schema layer. The wrapper that createTool puts around your handler parses the raw arguments against the parameters schema first. If they do not fit, a structured error is thrown before any business logic runs.

The consequence is a guarantee you can lean on: by the time the handler starts, its arguments are already well typed. There is no defensive code at the top of the function, and no chance of one tool being strict while its neighbour is lenient. Because the definition lives in Layer 1, the same contract can also describe the tool to a REST client or a model.

Two results in one

A handler returns an object with two fields. text is a human-readable summary, suitable for a chat window or a log line. data is a strongly typed payload that matches the data schema.

That dual shape lets each protocol layer pick what suits its audience. A REST endpoint takes data and sends JSON. An MCP server wraps text in content blocks for an agent. The tool itself never learns who is asking, which is exactly what you want from a layer that sits beneath both of them.

Annotations are hints, not promises

The fifth part is the most easily misread. The Model Context Protocol specification defines a handful of standard annotations. The book highlights four: readOnlyHint (the tool does not modify state), idempotentHint (repeated calls have the same effect), openWorldHint (the tool reaches entities outside the system) and title (a display name).

They inform how a caller retries and caches, but they do not create contractual obligations. Treat them as a courtesy to the caller, not a lock on the door. A tool marked read-only that quietly writes to a table is a bug in the tool, and no annotation will catch it for you.

The annotation set is also a place where a domain can extend the standard. The book's trading example adds its own flags and lets them travel through the registry into protocol descriptors, which we look at in the article on testing tools.

The takeaway

A tool is a name, a description, two schemas, a handler and some hints, produced by one factory. That uniformity is what lets the layers above treat tools as interchangeable parts: they can list them, call them and describe them without reading any of them. The next question is where all these tools should live on disk, which the article on organising business logic answers. For the layer itself, see Layer 4: Tools.

What the book covers

  • The createTool factory in pseudocode and its TypeScript reference implementation
  • How parameter validation is delegated to Schema-layer definitions
  • The standard MCP annotations and when callers should trust them
  • Latency-critical tool design and the five-step handler pattern
  • Worked tools from the TradeX trading platform