> ## Documentation Index
> Fetch the complete documentation index at: https://redop.useagents.site/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Middleware and Hooks

> Add request-aware middleware and lifecycle hooks to a Redop server without changing the tool API.

This tutorial shows how to add shared behavior around your tools.

## What you will add

* one global middleware
* one before hook
* one after hook

## Step 1: Add middleware

```ts theme={null}
import { middleware, Redop } from "@redopjs/redop";

const timing = middleware(async ({ ctx, next }) => {
  (ctx as Record<string, unknown>).startedAt = performance.now();
  return next();
});
```

## Step 2: Add hooks

```ts theme={null}
new Redop({ serverInfo: { name: "middleware-demo" } })
  .use(timing)
  .onBeforeHandle(({ tool }) => {
    console.log("starting", tool);
  })
  .onAfterHandle(({ tool, ctx }) => {
    const ms = performance.now() - ((ctx as Record<string, unknown>).startedAt as number);
    console.log("finished", tool, ms);
  });
```

## How to think about it

* middleware changes or controls execution flow
* before hooks observe work before the handler runs
* after hooks observe successful results and may replace them

## Next

* [Add auth to an HTTP server](/docs/tutorials/add-http-auth)
* [Concept: middleware vs hooks vs plugins](/docs/concepts/composition)
