Skip to main content

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.

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

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

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