LESSON 09 / 18·2 min readIntermediate

Reuse Existing APIs, Services, and Databases

Must knowConcept lesson
01 Understand02 Recall03 Prepare
Architecture at a glanceFollow the arrows to see how the parts connect.

Preparing the architecture diagram…

Read the full diagram flow
flowchart TB
  REST[REST controller] --> OS[Order service]
  MCP[MCP tool handler] --> OS
  OS --> R[Order repository]
  R --> DB[(PostgreSQL)]
  OS --> P[Authorization policy]

The REST controller and MCP handler are thin adapters. Business rules remain in the service layer. The repository uses parameterized queries.

TypeScript
async function lookupOrder(ctx: AuthContext, orderId: string) {
  const order = await orderRepository.findById(orderId);
  if (!order) return null;
  if (order.customerId !== ctx.customerId && !ctx.roles.includes("support")) {
    throw new ForbiddenError();
  }
  return order;
}

Production controls

  • Reuse connection pools; do not create a new pool for every tool call.
  • Use parameterized SQL; do not concatenate model-generated SQL.
  • Use bounded filters and cursor-based pagination.
  • Use external API timeouts, retry budgets, and circuit breakers where useful.
  • Perform writes with an idempotency key and an audit record.
PAUSE & RECALL

Can you explain it without the notes?

Reuse Existing APIs, Services, and Databases

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

Check your explanation

Recommended architecture

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Build a Custom TypeScript MCP Client

Adapted from 20_MCP_SERVER.md · “9. Reuse Existing APIs, Services, and Databases”. Examples are study fragments; framework behavior can depend on your version.