Hook decorators

Checked against FrontMCP 1.12.2 on

Every request FrontMCP handles runs through a flow: a fixed list of named stages. tools/call runs the tools:call-tool flow, from parseInput and findTool to execute and finalize. A hook is a method that FrontMCP calls at one of those stages: before it (Will), after success (Did), after any outcome (Finally), around it (Around), or as an extra step inside it (Stage). Hooks live on plugins, providers, and the classes of tools, resources, prompts, agents and jobs. Hooking into Calls teaches them step by step.

@ToolHook.Will(stage, { priority, filter })
async before(ctx: FlowCtxOf<"tools:call-tool">) { /* ... */ }

@ToolHook.Around(stage, { priority, filter })
async around(ctx: FlowCtxOf<"tools:call-tool">, next: () => Promise<void>) { /* ... */ }

Reference

Will, Did, Finally, Around and Stage

Each flow has a set of five decorators, like ToolHook for tools:call-tool. Put one on an async method of a plugin, a provider, or a tool, resource, prompt, agent or job class (where hooks can be declared).

freeze.plugin.ts
import { DynamicPlugin, FlowCtxOf, Plugin, PublicMcpError, ToolHook } from "@frontmcp/sdk";

@Plugin({ name: "freeze" })
export class FreezePlugin extends DynamicPlugin<object> {
  @ToolHook.Will("execute", { filter: (ctx) => !ctx.state.tool?.metadata.annotations?.readOnlyHint })
  async refuseChanges(ctx: FlowCtxOf<"tools:call-tool">) {
    throw new PublicMcpError(`Changes are frozen until Monday, so ${ctx.state.tool?.metadata.name} is unavailable.`);
  }
}

See more examples below.

DecoratorThe method runsSignature
Will(stage, options?)Just before the stage.(ctx) => Promise<void>
Did(stage, options?)Just after the stage, only if the stage succeeded.(ctx) => Promise<void>
Finally(stage, options?)After the stage on success, failure or an early answer, including one from a Will hook. Responses and throws are ignored with a warning; later Finally hooks still run.(ctx) => Promise<void>
Around(stage, options?)Instead of the stage: await next() runs the stage and rejects if it fails. Code before next() runs after the Will hooks, code after it before the Did hooks. Not calling next() skips the stage; calling it again runs it again.(ctx, next) => Promise<void>
Stage(stage, options?)Inside the stage, as a step next to FrontMCP's own: at the default priority, before FrontMCP's step, and with a priority above 0, after it. Inside any Around hooks.(ctx) => Promise<void>

A stage name that isn't in the flow's plan is a TypeScript error, and a hook on one never runs.

Options

OptionTypeDefaultDescription
prioritynumber0The order among hooks of the same kind on the same stage: lower runs first, for Will, Did, Finally and Stage alike. For Around hooks, lower is outer. See the order hooks run in.
before, afterreadonly string[]noneConstraints among hooks of the same stage and kind, before priority is considered. Name a plugin or declaring class, optionally .method. Unknown targets warn; a cycle fails startup with HookOrderCycleError.
filter(ctx) => boolean | Promise<boolean>noneCalled with the same ctx before each run. When it returns false, the hook is skipped; for an Around hook, the stage still runs. It's a plain function in the decorator, so it can't use this.
appliesTo"own-app" | "uncovered-apps" | "all-apps""own-app"For a hook of an app's plugin on tools/call, resources/read or prompts/get. "own-app" runs it for that app's entries. "uncovered-apps" also runs it for the entries of any app that no instance of the same hook covers, from its own plugins or the server's. "all-apps" runs this hook for every app in its scope, including shared tools; other matching instances may run too. Meant for gates that an entry's options ask for: see @Plugin.

Decorator sets and their flows

@frontmcp/sdk exports a decorator set for the flows you're most likely to hook. FlowHooksOf(flow) makes one for any other flow.

ExportFlowRuns forStages, in order
ToolHooktools:call-tooltools/callparseInput, checkSkillsOnlyMode, ensureRemoteCapabilities, checkSurface, findTool, checkEnabled, checkAppLoaded, checkPublicAccess, checkToolAuthorization, checkEntryAuthorities, createTaskIfRequested, createToolCallContext, checkToolCredentials, acquireQuota, acquireSemaphore; then validateInput, execute, validateOutput; then always releaseSemaphore, releaseQuota, applyUI, finalize, fitAnswer, shapeAnswer, meterAnswer
ListToolsHooktools:list-toolstools/listparseInput, ensureRemoteCapabilities, findTools, filterByEnabled, filterByAuthorities, filterByPublicAccess, resolveConflicts, parseTools
ResourceHookresources:read-resourceresources/readparseInput, ensureRemoteCapabilities, findResource, checkEnabled, checkAppLoaded, checkEntryAuthorities, createResourceContext; then execute, validateOutput; then always finalize, fitAnswer
ListResourcesHookresources:list-resourcesresources/listparseInput, ensureRemoteCapabilities, findResources, filterByEnabled, filterByAuthorities, filterSkillResources (since 1.11.1), resolveConflicts, parseResources
ListResourceTemplatesHookresources:list-resource-templatesresources/templates/listparseInput, ensureRemoteCapabilities, findTemplates, filterByEnabled, filterByAuthorities, resolveConflicts, parseTemplates
PromptHookprompts:get-promptprompts/getparseInput, ensureRemoteCapabilities, findPrompt, checkEnabled, checkAppLoaded, checkPublicAccess, checkEntryAuthorities, createPromptContext; then execute, validateOutput; then always finalize
ListPromptsHookprompts:list-promptsprompts/listparseInput, ensureRemoteCapabilities, findPrompts, filterByEnabled, filterByAuthorities, filterByPublicAccess, resolveConflicts, parsePrompts
HttpRouteHookhttp:routeApp HTTP routes. See @HttpRoute.checkIpFilter, findRoute, acquireQuota, checkSameOrigin, authenticatePerson; then handle
HttpHookhttp:requestEvery HTTP request, before it's handledtraceRequest, checkIpFilter, acquireQuota, acquireSemaphore, checkAuthorization, acquireIdentityQuota, router, then the transport stages (handleMcp2026, …) and audit, metrics, releaseSemaphore, releaseQuota, finalize
AgentCallHookagents:call-agentA call to an agent's invoke_<name>, inside its tools:call-tool flow (since 1.8.5). See Hooking an agent's calls.parseInput, findAgent, checkCallDepth (since 1.9), checkEntryAuthorities, checkAgentAuthorization, createAgentContext, acquireQuota, acquireSemaphore; then validateInput, execute, validateOutput; then always releaseSemaphore, releaseQuota, parseOutput, emitCompletion, finalize
JobHookjobs:execute-jobEach attempt of a job: run by execute_job, while the client waits or in the background, each retry, and each workflow step (since 1.9.4). See Hooking a job's attempts.parseInput, checkJobAuthorization, validateInput, admit, createJobContext; then execute, validateOutput; cleanup on interruption, timeout or cancellation; then always updateRunState, finalize
ChannelSendHook, ChannelListHookchannels:send-notification, channels:listEvery event a @Channel sends, and the channels a session subscribes to (since 1.9; before, only runs of those flows you started yourself). Not an event with the channel option: events:publish sends that one. See @Channel.parseInput, resolveMeta, send, finalize; listChannels, finalize
ListEventsHookevents:listevents/list, the events a client may subscribe to (since 1.12.0, like the five rows below). See Hooking the events flows.parseInput, findEvents, filterByAvailability, filterByAuthorities, filterByDiscovery, parseEvents; then always finalize
EventSubscribeHook, EventUnsubscribeHookevents:subscribe, events:unsubscribeevents/subscribe and events/unsubscribe, on a server that delivers events by webhookparseInput, resolveDialect, findEvent, checkAvailability, requireWebhookMode, requireSubscriber, checkEntryAuthorities, validateArguments, authorize, validateDelivery; then findSubscription, enforceLimits, verifyCallback, grantTtl, upsertSubscription, notifySubscribed, startReplay, buildResult; then always finalize. Unsubscribe: parseInput, resolveDialect, requireSubscriber, removeSubscription, buildResult; then always finalize
EventPollHook, EventStreamHookevents:poll, events:streamevents/poll, and the opening of an events/streamparseInput, resolveDialect, findEvent, resolveReader, checkEntryAuthorities, validateArguments, authorize; then renewLease, readEvents (a poll) or openStream (a stream), buildResult; then always finalize
EventPublishHookevents:publishEach occurrence published with this.events.publish(), by a provider's EventPublisher or through the HTTP publish endpointvalidate, bufferOccurrence, pushToStreams; then loadSubscriptions, dropInactive, filterByAudience, reauthorize, match, transform, enforceBodyLimit, enqueue, bridgeToBus, notifyChannelSessions, buildResult
EventDeliverHookevents:deliverOne attempt to deliver an occurrence to a webhook subscription's callbackloadSubscription, buildHeaders; then send, classify, buildResult
EventPublishHttpHookevents:publish-httpA POST to the HTTP publish endpoint, with events.publishEndpoint.enabledcheckIpFilter, acquireQuota, readBody, authenticate, parseBody, checkAuthorities, findEvent, validatePayload; then claimEventId, publish, rememberResult, buildResponse; then always finalize
OAuthAuthorizeHook, OAuthTokenHook, OAuthCallbackHook, OAuthRegisterHookoauth:authorize, oauth:token, oauth:callback, oauth:registerThe OAuth endpoints of local and remote auth. They need a real server, so this page doesn't show them.

WillHookOf(flow), DidHookOf(flow), FinallyHookOf(flow), AroundHookOf(flow) and StageHookOf(flow) each return one of the five decorators, as in @(WillHookOf("resources:read-resource")("execute")).

The eight events sets are new in 1.12.0, with the flow types ListEventsFlow, EventSubscribeFlow, EventUnsubscribeFlow, EventPollFlow, EventStreamFlow, EventPublishFlow, EventDeliverFlow and EventPublishHttpFlow. A server registers an events flow only when it performs that flow's delivery mode, so a hook on events:subscribe never runs on a server with public auth: see the events flows, which also lists what each stage sets. @Duty, @Worker and a workflow's schedule run through no flow, so they have no hooks; the steps of a scheduled workflow run jobs:execute-job, which JobHook hooks.

CompletionHook hooks completion:complete, the completion/complete requests. JobHook, and the ExecuteJobFlow type, are new in 1.9.4: before, jobs ran through no flow of their own. Changed in 1.9: PromptHook, ListPromptsHook and CompletionHook are new, and FlowHooksOf() and FlowCtxOf<> accept the three flows' names. Before, TypeScript rejected FlowHooksOf("prompts:get-prompt"), and code wrote FlowHooksOf<any> with a ctx type of its own.

Stages run in groups. The first group runs until a stage fails or answers; the execute group runs if it got that far; the last group, where the table says "always", runs whether the request worked or not. When a stage fails, its Did hooks and every later stage outside the last group are skipped.

ctx

A hook's first argument is the running flow. Type it with FlowCtxOf<"tools:call-tool">, or the name of the flow you hook. The decorator checks that the method accepts that flow: a structural subset, unknown, any or no parameter works too. Only Around receives next.

Changed in 1.10.16: before, the parameter could claim fields the flow did not have and read undefined at runtime. Those hooks now fail to compile with TS1241.

MemberDescription
ctx.stateWhat the flow knows so far. Fields depend on the flow and on how far it has got: see tools:call-tool state. Read a field as ctx.state.tool or ctx.state.get("tool"); ctx.state.required.tool and ctx.state.getOrThrow("tool") throw if it isn't set. ctx.state.set("tools", list) or ctx.state.set({ ... }) replaces fields, and ctx.state.snapshot() returns a copy.
ctx.respond(result)Ends the request with result, which must be a complete response for the flow, like a whole tools/call result with content, or { result, logs } for a job's attempt. The stages and hooks that haven't run yet are skipped, apart from the closing group. In a Did hook it's ignored. this.respond compares it with a tool's own this.respond().
ctx.fail(error)Ends the request with error, exactly like throwing it.
ctx.get(token)A provider of the server, from @FrontMcp({ providers }). An app's providers aren't found here: in a plugin use this.get(), and in a tool call ctx.state.toolContext.get().
ctx.rawInputThe flow's input before parsing. For http:request, ctx.rawInput.request has method, path, headers, query and body.
ctx.toolName, ctx.toolInputOn tools:call-tool: the resolved tool name (the requested name before lookup), and arguments before input-schema parsing, including changes from Will hooks.
ctx.uri, ctx.promptNameOn resources:read-resource and prompts:get-prompt, respectively: the requested URI or prompt name.
ctx.isClientCall()On tools:call-tool: true when a client called the tool, false for a call a tool, agent or job makes in process, like this.callTool(). The fitAnswer, shapeAnswer and meterAnswer stages act only on a client's call, and a plugin that changes answers can check it the same way. New in 1.11.0.
ctx.requestFlowRequestInfo: { sessionId, clientInfo, metadata, protocol }. The session id is verified; client info comes from the request or its session. Fields are undefined without request context; reading it never throws. Anonymous public sessions may have no protocol.
ctx.notify.listChanged(kind)Broadcasts a list-change notification for "tools", "resources" or "prompts"; returns Promise<void>.
ctx.errorThe run's first failure, Error | undefined, on every flow. Also stored in ctx.state.flowError. Read it from closing stages and Finally hooks.
ctx.scopeLoggerThe server's logger.

Throwing from a hook, or calling ctx.fail(), fails the request, except in Finally, where the throw is logged and ignored. A PublicMcpError reaches the client with its message and code, PUBLIC_ERROR by default; any other error becomes a SERVER_ERROR, whose message is hidden in production. A Did hook that throws fails the call after the stage ran, so the tool's side effects have already happened.

tools:call-tool state

FieldSet byWhat it is
ctx.state.inputparseInputThe tools/call params as the client sent them: name, arguments and _meta. Changing arguments before validateInput changes what's validated.
ctx.state.authInfo, progressToken, jsonRpcRequestIdparseInputWho's calling, and the request's progress token and JSON-RPC id.
ctx.state.toolfindToolThe tool: metadata is everything from @Tool({ ... }), including keys FrontMCP doesn't know, and owner.id is its app's id.
ctx.state.toolContextcreateToolCallContextThe call's tool instance: input (validated once validateInput has run), output (once execute has run; assign it in a Did("execute") hook to change the result), and get() for the app's providers.
ctx.state.rawOutputvalidateOutputWhat execute() returned. Not set yet in Did("execute"); the output schema is checked later, in finalize.
ctx.state.outputfinalizeThe answer the client gets. Hooks on fitAnswer, shapeAnswer and meterAnswer read it, and replace it with ctx.respond(). The resource and prompt flows keep their answers here too. New in 1.11.0.
ctx.state.flowErrorThe first failed stageThe original error, including one passed to fail(error) or thrown by finalize. ctx.error reads it on every flow.
ctx.state.resultMetaYour hook, with ctx.state.set("resultMeta", { … })An object FrontMCP merges into the result's _meta in finalize. It never becomes part of the tool's data. New in 1.8.6: see Adding to the result's _meta.

Where hooks can be declared

Declared onFlowsRuns forthis in the hook
A plugin class, or a provider in its providersAnyAn app's plugin: that app's tool calls, resource reads and prompt gets (and other apps' too, for a hook with appliesTo: "uncovered-apps"), its job attempts, calls to tool names no app has, and every list and HTTP request. A server's plugin: every request and job attempt. An agent's plugin: the calls of the tools the agent's model uses, and its reads of the agent's resources and prompts (since 1.9.4). See @Plugin.The plugin, or the provider
A provider in @App({ providers })AnyLike a plugin on that appThe provider. For a CONTEXT provider, the instance built for that request (since 1.9)
A provider in @FrontMcp({ providers })AnyLike a plugin on the server: every app's requests (since 1.9)The provider
A @Tool classtools:call-tool: an instance method from Did("createToolCallContext") on, a static method on any stageThat tool's callsAn instance method: the call's tool context, this.input, this.get(). A static method: the class
A @Resource or @ResourceTemplate classresources:read-resource: an instance method from Did("createResourceContext") on, a static method on any stageThat resource's readsThe read's resource context, or the class
A @Prompt classprompts:get-prompt: an instance method from Did("createPromptContext") on, a static method on any stageThat prompt's requestsThe request's prompt context, or the class
An @Agent classagents:call-agent: an instance method from Did("createAgentContext") on, a static method on any stageThat agent's callsThe call's agent context, or the class
A @Job classjobs:execute-job: an instance method from Did("createJobContext") on, a static method on any stageThat job's attemptsThe attempt's job context, this.input, this.attempt, or the class

A static hook on an entry class gets the flow as ctx, like any hook, and needs no instance, so it can hook the stages before the entry's instance exists, like parseInput, findTool or checkToolAuthorization: see Hooking the stages before an entry's instance.

Hooks that could never run stop the server from starting, with InvalidHookFlowError (INVALID_HOOK_FLOW): an instance method on an entry class for a stage before its context is built, like Will("findTool") or Will("createToolCallContext") on a tool; list hooks, like ListToolsHook, on an entry class, static or not; and hooks for another flow, like a ToolHook on a @Job class. The message names the class, the method and the stage, and says to declare an early hook as a static method. A provider or a plugin can hook every stage and flow.

Changed in 1.9: before, all of these were accepted and never ran, with no warning, and so were hooks on a provider in @FrontMcp({ providers }) or with scope: ProviderScope.CONTEXT, which run now.

Changed in 1.9.4: static hook methods are new; before, only a provider or a plugin could hook the stages before an entry's instance. And a @Job class's hooks run on its attempts; before, jobs ran through no flow, and any hook on a @Job class stopped the server from starting.

The order hooks run in

For one stage, before and after constraints come first, then ascending priority, then registration order. Without constraints:

  1. Will hooks, lowest priority first.
  2. Around hooks, lowest priority outermost.
  3. The stage itself: Stage steps with priority 0 or less, FrontMCP's own step, then Stage steps with a higher priority.
  4. Did hooks, lowest priority first, if the stage succeeded.
  5. Finally hooks, lowest priority first, on every outcome.

At equal priority, plugin and provider hooks run in registration order, then the flow's own step, then the entry class's hooks (static before instance). Within one class, declaration order breaks ties. The example below shows app providers, app plugins and a server plugin together.

With NODE_ENV=development or DEBUG=1, a flow writes an info log beginning Hook order of 'tools:call-tool' for '…', lowest priority first. It prints once on the first run for each hook owner and entry class with hooks, with a line per stage. Each step includes its priority and any before / after constraints; brackets show what an Around hook wraps. The flow's own steps are included. Named constraints take precedence over numeric priority; hooks without constraints keep their order.

Named priorities

HookPhase, exported by @frontmcp/sdk, names priorities for what a hook does on its stage, in the order they run:

NamePriorityFor hooks that
HookPhase.resolve-100Fill in what later hooks read: a resolved name, a tenant, defaults.
HookPhase.guard100Refuse a call. The Approval plugin hooks at guard.
HookPhase.observe300Watch a call the guards let through: timing, auditing.
HookPhase.shape500Rewrite what the stage hands on, like trimming an answer.

The default priority, 0, runs after resolve and before guard. The gaps leave room for a hook in between, like HookPhase.guard + 10. New in 1.11.0.

Hook traces and observers

With devtools.hookTrace on, a tool, resource or prompt answer lists the hooks that ran for it in _meta["frontmcp/trace"], in order. Each entry is { owner, method, stage, type, ms, outcome }: owner is the plugin's name, else the declaring class's name; outcome is "ok", "respond", "next", "handled", "fail", "abort" or "error". Arguments and answers are never in it. An answer for which no hook ran has no trace key. devtools.hookTrace is on by default only with NODE_ENV=development; set devtools: { hookTrace: false } for tests that compare whole results. New in 1.11.0.

addHookObserver(context, observer) wraps the hooks a request runs from then on. context is the request's FrontMcpContext, such as ctx.state.toolContext.context in a tool hook. observer is (hook, run) => Promise<void>: hook describes the hook (flowName, stage, type, owner, method), and run() runs it, at most once whatever the observer does, with the hook's own outcome. Observers added earlier wrap the ones added later. One that throws is logged once and ignored. New in 1.11.0. Seeing which hooks ran shows both.

Caveats

  • Hooks of an app's plugin or provider on list requests (tools/list, resources/list, resources/templates/list, prompts/list) and on HTTP requests aren't tied to the app: they see and can change every app's entries. To change only the entries the plugin's own call hooks judge, check each one with isEntryGatedBy().
  • Hiding isn't blocking. A list hook changes what's listed; a client that calls a hidden tool by name still reaches it.
  • In an Around hook, the error next() rejects with is FrontMCP's: a ToolExecutionError wrapping what execute() threw, or, when the tool called this.fail(), an internal error whose message is empty. Rethrow it to keep the tool's own error for the client.
  • To give a call a result from an Around hook that skips the stage, assign ctx.state.toolContext.output or call ctx.respond(). ctx.state.set("output", …) isn't read: the call ends with Flow exited without producing output.
  • Tool finalize builds ctx.state.output. Hooks on the following fitAnswer, shapeAnswer and meterAnswer stages can inspect or replace that answer; ctx.state.rawOutput is what execute() returned.
  • HttpHook hooks see requests as they arrive. For 2026-07-28 requests, the flow's closing stages can run before the call they carry is answered, so don't time requests with them.

Usage

Running code before and after a stage

A Will hook runs before its stage, and a Did hook after it if the stage worked. The closing stages, like finalize, run for every call. This plugin records which of its hooks ran:

Open
import { DynamicPlugin, FlowCtxOf, Plugin, ToolHook } from "@frontmcp/sdk";
import { Trace } from "./trace";

type CallCtx = FlowCtxOf<"tools:call-tool">;

@Plugin({ name: "trace", providers: [Trace], exports: [Trace] })
export class TracePlugin extends DynamicPlugin<object> {
  private note(ctx: CallCtx, line: string) {
    if (ctx.state.input?.name !== "show_trace") this.get(Trace).lines.push(line);
  }

  @ToolHook.Will("validateInput")
  async willValidate(ctx: CallCtx) {
    this.note(ctx, "will validateInput");
  }

  @ToolHook.Did("validateInput")
  async didValidate(ctx: CallCtx) {
    this.note(ctx, "did validateInput");
  }

  @ToolHook.Will("execute")
  async willExecute(ctx: CallCtx) {
    this.note(ctx, "will execute");
  }

  @ToolHook.Did("execute")
  async didExecute(ctx: CallCtx) {
    this.note(ctx, "did execute");
  }

  @ToolHook.Did("finalize")
  async didFinalize(ctx: CallCtx) {
    this.note(ctx, "did finalize");
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

For cleanup that must run after a stage on every outcome, use Finally. A closing stage always runs, but its Did hooks still need that stage to succeed.

Cleaning up after success and failure

Use Finally to close a span, timer or lock even if a hook answers early or output validation fails. This plugin reads the typed tool accessors and records the error that finalization sees:

Open
import { App, DynamicPlugin, FlowCtxOf, Plugin, PublicMcpError, Tool, ToolContext, ToolHook, z } from "@frontmcp/sdk";

export const events: string[] = [];
export const missing = new PublicMcpError("Ticket not found.");
export let recorded: Error | undefined;

@Plugin({ name: "outcomes" })
class Outcomes extends DynamicPlugin<object> {
  @ToolHook.Will("execute")
  async cached(ctx: FlowCtxOf<"tools:call-tool">) {
    if (ctx.toolName === "get_ticket" && ctx.toolInput?.id === "cached") {
      ctx.respond({ content: [{ type: "text", text: "Cached ticket" }] });
    }
  }

  @ToolHook.Did("execute")
  async succeeded() { events.push("did execute"); }

  @ToolHook.Finally("execute")
  async attempted() { events.push("finally execute"); }

  @ToolHook.Finally("finalize")
  async close(ctx: FlowCtxOf<"tools:call-tool">) {
    recorded = ctx.error;
    events.push(`finally finalize: ${ctx.error ? "error" : "ok"}`);
  }

  @ToolHook.Finally("finalize", { priority: 10 })
  async brokenCleanup() { throw new Error("Cleanup failed"); }

  @ToolHook.Finally("finalize", { priority: 20 })
  async last() { events.push("last cleanup"); }
}

@Tool({ name: "get_ticket", inputSchema: { id: z.string() }, outputSchema: { title: z.string() } })
class GetTicket extends ToolContext {
  async execute({ id }: { id: string }) {
    if (id === "missing") this.fail(missing);
    const ticket: Record<string, string> = id === "invalid" ? {} : { title: "Cannot log in" };
    return { title: ticket.title };
  }
}

@App({ name: "Help Desk", tools: [GetTicket], plugins: [Outcomes] })
export class HelpDeskApp {}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

A Finally hook cannot replace the outcome by responding, failing or throwing. Values it assigned to flow-state fields before that throw are restored; mutations inside an object aren't undone. Keep cleanup separate from changing results. Changed in 1.10.16: before, fail(error) left a control-flow signal in state.flowError, and a failure in finalize could leave it unset. It now holds the run's first error; later failures do not replace it. An abort records an InternalMcpError with Flow aborted: <reason>.

To read the caller in a hook, use ctx.request.sessionId, ctx.request.clientInfo or ctx.request.metadata. To announce a changed tool list, await ctx.notify.listChanged("tools"). Both work without reaching protected flow members or looking up request-context tokens.

Reading the request and announcing a list change

ctx.request gives a hook the verified session and client information without throwing outside a request. ctx.notify.listChanged() broadcasts through the scope's notification service:

Open
import { App, DynamicPlugin, FlowCtxOf, Plugin, Tool, ToolContext, ToolHook } from "@frontmcp/sdk";

export const seen: { tool: string; hasMetadata: boolean }[] = [];

@Plugin({ name: "refresh" })
class Refresh extends DynamicPlugin<object> {
  @ToolHook.Did("execute")
  async refreshed(ctx: FlowCtxOf<"tools:call-tool">) {
    seen.push({ tool: ctx.toolName, hasMetadata: ctx.request.metadata !== undefined });
    await ctx.notify.listChanged("tools");
  }
}

@Tool({ name: "refresh_tickets", inputSchema: {} })
class RefreshTickets extends ToolContext {
  async execute() { return { refreshed: true }; }
}

@App({ name: "Help Desk", tools: [RefreshTickets], plugins: [Refresh] })
export class HelpDeskApp {}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

Wrapping a stage

An Around hook runs the stage itself, with await next(). It can catch a failure, and it can run the stage again. This one retries tools marked idempotentHint once:

Open
import { DynamicPlugin, FlowCtxOf, Plugin, ToolHook } from "@frontmcp/sdk";

type CallCtx = FlowCtxOf<"tools:call-tool">;

@Plugin({ name: "retry", description: "Retries idempotent tools once when they fail" })
export class RetryPlugin extends DynamicPlugin<object> {
  @ToolHook.Around("execute", { filter: (ctx) => ctx.state.tool?.metadata.annotations?.idempotentHint === true })
  async retryOnce(ctx: CallCtx, next: () => Promise<void>) {
    try {
      await next(); // runs execute()
    } catch (error) {
      ctx.scopeLogger.warn(`${ctx.state.tool?.metadata.name} failed, retrying once`);
      await next(); // runs execute() again; if it fails again, so does the call
    }
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

The filter keeps the hook off charge_invoice, which isn't safe to run twice. Swallowing the error without running the stage again would leave the call with no result: see Troubleshooting.

Answering without running the rest

ctx.respond() ends the request with a result of your own. In Will("findTool") it runs before FrontMCP looks the tool up, so it can answer calls to a tool that doesn't exist, like one that was renamed:

Open
import { DynamicPlugin, FlowCtxOf, Plugin, ToolHook } from "@frontmcp/sdk";

const renamed: Record<string, string> = { find_ticket: "get_ticket", ticket_search: "search_tickets" };

@Plugin({ name: "renames", description: "Tells the model the new name of a renamed tool" })
export class RenamesPlugin extends DynamicPlugin<object> {
  @ToolHook.Will("findTool")
  async pointToNewName(ctx: FlowCtxOf<"tools:call-tool">) {
    const name = ctx.state.input?.name;
    const newName = name ? renamed[name] : undefined;
    if (!newName) return;
    ctx.respond({
      content: [{ type: "text", text: `${name} was renamed to ${newName}. Call ${newName} with the same arguments.` }],
      isError: true,
    });
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

An app's plugin hooks tools/call only for that app's tools, but a name no app has belongs to no app, and FrontMCP runs every app's hooks for it: the plugin is on the help desk app and still answers find_ticket for the whole server. The result you pass is sent as it is: give it content, or the client gets "content": []. Answering from a cache in Will("execute") works the same way: see this.respond.

Adding to the result's _meta

A hook that wants to say something about a result, without changing the tool's data, sets ctx.state.resultMeta. FrontMCP merges it into the result's _meta when the call finishes, and structuredContent and the text stay what the tool returned. The Cache plugin marks a hit this way, with cache: "hit". Here a plugin marks every result as audited:

Open
import { DynamicPlugin, FlowCtxOf, Plugin, ToolHook } from "@frontmcp/sdk";

@Plugin({ name: "audit" })
export class AuditPlugin extends DynamicPlugin<object> {
  @ToolHook.Did("execute")
  async mark(ctx: FlowCtxOf<"tools:call-tool">) {
    ctx.state.set("resultMeta", { audited: true });
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

Seeing which hooks ran

Here the server turns hookTrace on, so each answer lists the hooks that ran for it. The watch plugin also adds an observer, which records every later hook of the call for the tool to return. count_tickets is in an app with no plugins, so its answer has no trace:

Open
import { addHookObserver, FlowCtxOf, HookPhase, Plugin, ToolHook } from "@frontmcp/sdk";

export const SEEN = Symbol("seen");

@Plugin({ name: "watch" })
export class WatchPlugin {
  @ToolHook.Did("createToolCallContext")
  async watch(ctx: FlowCtxOf<"tools:call-tool">) {
    const context = ctx.state.toolContext.context;
    const seen: string[] = [];
    context.set(SEEN, seen);
    addHookObserver(context, async (hook, run) => {
      seen.push(`${hook.owner}.${hook.method}`);
      await run();
    });
  }
}

@Plugin({ name: "audit" })
export class AuditPlugin {
  @ToolHook.Will("execute", { priority: HookPhase.observe })
  async note(ctx: FlowCtxOf<"tools:call-tool">) {
    ctx.scopeLogger.info(`calling ${ctx.toolName}`);
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

Hooking resources and prompts

ResourceHook hooks resources/read, and PromptHook hooks prompts/get. Their state has resource and resourceContext, or prompt and promptContext, in place of tool and toolContext:

Open
import { DynamicPlugin, FlowCtxOf, Plugin, PromptHook, Provider, ResourceHook } from "@frontmcp/sdk";

@Provider({ name: "ReadLog" })
export class ReadLog {
  lines: string[] = [];
}

@Plugin({ name: "reads", providers: [ReadLog], exports: [ReadLog] })
export class ReadsPlugin extends DynamicPlugin<object> {
  @ResourceHook.Did("execute")
  async resourceRead(ctx: FlowCtxOf<"resources:read-resource">) {
    this.get(ReadLog).lines.push(`read ${ctx.state.input?.uri}`);
  }

  @PromptHook.Will("execute")
  async promptRequested(ctx: FlowCtxOf<"prompts:get-prompt">) {
    this.get(ReadLog).lines.push(`prompt ${ctx.state.input?.name} ${JSON.stringify(ctx.state.input?.arguments)}`);
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

Changing what clients list

A hook on a list flow's find… stage can filter what's listed. This one hides every resource whose URI has an internal/ path from resources/list:

Open
import { DynamicPlugin, FlowCtxOf, ListResourcesHook, Plugin } from "@frontmcp/sdk";

const uriOf = (metadata: { uri?: string; uriTemplate?: string }) => metadata.uri ?? metadata.uriTemplate ?? "";

@Plugin({ name: "hide-internal" })
export class HideInternalPlugin extends DynamicPlugin<object> {
  @ListResourcesHook.Did("findResources")
  async hide(ctx: FlowCtxOf<"resources:list-resources">) {
    const resources = ctx.state.resources ?? [];
    ctx.state.set("resources", resources.filter(({ resource }) => !uriOf(resource.metadata).includes("://internal/")));
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

The plugin is on the help desk app, but list flows aren't tied to an app, so it hides the billing app's ledger too. To refuse reads as well, add a ResourceHook.Will("execute") that throws.

Hooks on a tool or a provider

Hooks don't need a plugin. On a @Tool class they run only for that tool, and this is the call's tool context. On a provider in @App({ providers }), they run for every tool of the app:

Declaring hooks without a plugin

Example 1 of 2

On a tool

Open
import { FlowCtxOf, PublicMcpError, Tool, ToolContext, ToolHook, z } from "@frontmcp/sdk";

type Input = { id: string; at: string };

@Tool({
  name: "schedule_callback",
  description: "Schedule a phone call back to the customer",
  inputSchema: { id: z.string(), at: z.string().datetime().describe("When to call, as an ISO date and time") },
})
export class ScheduleCallback extends ToolContext {
  // Runs for this tool only; `this` is the call's ToolContext, so this.input is the validated input
  @ToolHook.Will("execute")
  async refusePastTimes(ctx: FlowCtxOf<"tools:call-tool">) {
    const { at } = this.input as Input;
    if (new Date(at) < new Date("2026-01-01")) throw new PublicMcpError(`${at} is in the past. Pick a time after now.`);
  }

  async execute({ id, at }: Input) {
    return { id, callbackAt: at };
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

Link to this example

A tool's instance methods can only hook stages from Did("createToolCallContext") on: before that, there's no tool instance to run them on. A static method can hook any stage, as the next section shows. A provider's hooks run on an app's provider, a CONTEXT one included, and on a provider in @FrontMcp({ providers }) for every app (where hooks can be declared).

Hooking the stages before an entry's instance

A static hook method on a tool class runs for that tool only, like an instance method, but needs no instance: this is the class, and it can hook the stages before createToolCallContext. Here one cleans up the id the client sent before anything checks it, and another refuses an archived ticket before the tool is built. close_ticket takes the same input and has no hooks of its own:

Open
import { FlowCtxOf, PublicMcpError, Tool, ToolContext, ToolHook, z } from "@frontmcp/sdk";

@Tool({ name: "get_ticket", description: "Get one support ticket by its id", inputSchema: { id: z.string().regex(/^T-\d+$/) } })
export class GetTicket extends ToolContext {
  @ToolHook.Did("parseInput")
  static cleanId(ctx: FlowCtxOf<"tools:call-tool">) {
    const input = ctx.state.required.input;
    const id = input.arguments?.id;
    if (typeof id !== "string") return;
    ctx.state.set("input", { ...input, arguments: { ...input.arguments, id: id.trim().toUpperCase() } });
  }

  @ToolHook.Will("checkToolAuthorization")
  static refuseArchived(ctx: FlowCtxOf<"tools:call-tool">) {
    if (ctx.state.input?.arguments?.id === "T-0") throw new PublicMcpError("T-0 is archived.");
  }

  async execute({ id }: { id: string }) {
    return { id, title: "Cannot log in" };
  }
}

@Tool({ name: "close_ticket", description: "Close a support ticket", inputSchema: { id: z.string().regex(/^T-\d+$/) } })
export class CloseTicket extends ToolContext {
  async execute({ id }: { id: string }) {
    return { id, status: "closed" };
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

A static hook gets the same ctx as a plugin's, so it reads and changes ctx.state the same way: here input, which parseInput sets. It has no this.input or this.get(): this is the class, on a later stage too. With the same priority, an entry's static hooks run after the providers' and plugins' hooks on that stage, as its instance hooks do.

@Resource, @Prompt, @Agent and @Job classes take static hooks the same way, each on its own flow, like PromptHook.Will("findPrompt"), AgentCallHook.Will("findAgent") or JobHook.Did("parseInput"). Hooks on list flows, like ListToolsHook, still stop the server from starting on an entry class, static or not: put them on a plugin or a provider. New in 1.9.4: before, only a provider or a plugin could hook these stages, and the startup error didn't mention static methods (Troubleshooting).

Hooking an agent's calls

A client calls an agent as a tool, invoke_<name>, so a ToolHook runs for it. Inside that call, the agent runs in its own flow, agents:call-agent, whose hooks are AgentCallHook, on a plugin or on the agent class. The tools the agent's model calls are the agent's own: a plugin on the app or the server doesn't hook them, only a plugin in @Agent({ plugins }) does (@Agent):

Open
import { AgentCallHook, DynamicPlugin, FlowCtxOf, Plugin, ToolHook } from "@frontmcp/sdk";

export const calls: string[] = [];

@Plugin({ name: "call-log" })
export class CallLog extends DynamicPlugin<object> {
  @ToolHook.Will("execute")
  async tool(ctx: FlowCtxOf<"tools:call-tool">) {
    calls.push(`tool ${ctx.state.tool?.metadata.name}`);
  }

  @AgentCallHook.Will("execute")
  async agent(ctx: FlowCtxOf<"agents:call-agent">) {
    calls.push(`agent ${ctx.state.agent?.metadata.name}`);
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

The limits of @Agent, like rateLimit, are applied once, by the tool call. Changed in 1.8.5: before, the agents:call-agent flow never ran, so AgentCallHook hooks never did.

Hooking a job's attempts

Each attempt of a job runs the jobs:execute-job flow, whose hooks are JobHook: a call to execute_job while the client waits, a run in the background, each retry, and each step of a workflow. Here a plugin records how each attempt ended, and answers count-open-tickets from a snapshot instead of running it:

Open
import { DynamicPlugin, FlowCtxOf, JobHook, Plugin } from "@frontmcp/sdk";

export const attempts: string[] = [];

@Plugin({ name: "job-audit" })
export class JobAudit extends DynamicPlugin<object> {
  @JobHook.Did("updateRunState")
  async record(ctx: FlowCtxOf<"jobs:execute-job">) {
    const { job, attempt, flowError } = ctx.state;
    attempts.push(`${job?.name} attempt ${attempt}: ${flowError ? flowError.message : "ok"}`);
  }

  @JobHook.Will("execute", { filter: (ctx) => ctx.state.job?.name === "count-open-tickets" })
  async fromSnapshot(ctx: FlowCtxOf<"jobs:execute-job">) {
    ctx.respond({ result: { open: 12 }, logs: ["Read from the 06:00 snapshot"] });
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

ctx.respond() takes the attempt's whole answer, { result, logs }. The result is checked against the job's outputSchema: extra fields are dropped, and a mismatch fails the attempt with INVALID_OUTPUT without retrying. Changed in 1.9.5: before, hook results skipped validation. updateRunState still runs, so the run is completed and Did("updateRunState") hooks see it, like the first hook here. That hook runs for failed attempts too: updateRunState is a closing stage, and ctx.state.flowError is the attempt's error. jobs:execute-job lists the stages and what each one sets.

An app's plugins hook that app's jobs only; a plugin on @FrontMcp hooks every app's. On a @Job class, instance methods hook from Did("createJobContext") on, with this the attempt's job context, and static methods hook any stage. New in 1.9.4: before, jobs ran through no flow, so the closest a hook got was the execute_job tool's tools:call-tool: one call, not each attempt, and only the start of a run in the background.

Hooking the events flows

New in 1.12.0. A hook on events:list changes which events a client is offered, as a hook on tools:list-tools does for tools: ctx.state.events holds { event, delivery } for each one, and event.getTags() reads its tags. A hook on events:publish sees each occurrence in ctx.rawInput.occurrence (eventId, name, timestamp, data) before any subscription receives it, and from match on, ctx.state.deliveries has one entry per webhook subscription the occurrence is for. Throwing from it fails the publish: this.events.publish() rejects with the error, so a tool that awaits it fails with it.

Open
import { DynamicPlugin, EventPublishHook, FlowCtxOf, ListEventsHook, Plugin, PublicMcpError } from "@frontmcp/sdk";

export const published: object[] = [];
export const desk = { frozen: false };

@Plugin({ name: "event-hooks" })
export class EventHooksPlugin extends DynamicPlugin<object> {
  @ListEventsHook.Did("findEvents")
  async hideBetaEvents(ctx: FlowCtxOf<"events:list">) {
    const stable = ctx.state.required.events.filter(({ event }) => !event.getTags().includes("beta"));
    ctx.state.set("events", stable);
  }

  @EventPublishHook.Will("validate")
  async refuseWhileFrozen(ctx: FlowCtxOf<"events:publish">) {
    if (desk.frozen) throw new PublicMcpError(`The desk is frozen, so ${ctx.rawInput.occurrence.name} wasn't published.`);
  }

  @EventPublishHook.Did("buildResult")
  async record(ctx: FlowCtxOf<"events:publish">) {
    const { name, data } = ctx.rawInput.occurrence;
    published.push({ name, data, deliveries: ctx.state.deliveries?.length });
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

The server here has public auth, so it has no webhook subscriptions and deliveries is empty. Hooks on the events flows run for every app's events, whichever app's plugin declares them, like hooks on the list flows. events:publish and events:deliver have no closing stage: put code that must run after a failure too in a Finally hook on the stage you care about. An event with the channel option reaches Claude Code sessions from the notifyChannelSessions stage of events:publish, not through channels:send-notification: ChannelSendHook doesn't see it, and an Around("notifyChannelSessions") hook that doesn't call next() skips it. The two channel flows themselves are as they were in 1.11. @Event describes what each stage decides.

A POST to the HTTP publish endpoint runs events:publish-http, whose finalize stage runs however the request ended. A refusal is the run's failure: a hook on finalize reads it as ctx.error, a PublicMcpError with code EVENT_PUBLISH_REFUSED, the answer's status as statusCode, and HTTP <status>: <error> as its message. A request that publishes, or repeats one that did, ends without an error:

Open
import { DynamicPlugin, EventPublishHttpHook, FlowCtxOf, Plugin, PublicMcpError } from "@frontmcp/sdk";

export const finished: unknown[] = [];

@Plugin({ name: "publish-audit" })
export class PublishAuditPlugin extends DynamicPlugin<object> {
  @EventPublishHttpHook.Finally("finalize")
  async record(ctx: FlowCtxOf<"events:publish-http">) {
    const error = ctx.error as PublicMcpError | undefined;
    finished.push(error ? { code: error.code, statusCode: error.statusCode, message: error.message } : "no error");
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

The 403 of throttle's IP filter and the 429 of its global rate limit, which come before every other check, set ctx.error too. If the rate-limit store cannot be reached, its 503 sets it with message HTTP 503: service_unavailable. With observability, a refusal also ends the request's events/publish-http span with status ERROR. Changed in 1.12.2: before, IP-filter and global rate-limit refusals left ctx.error unset and ended their spans with status OK.

Seeing every HTTP request

HttpHook runs for each HTTP request the server receives, before FrontMCP routes it, so it sees requests of every kind, including ones that fail later. ctx.rawInput.request is the request, with its parsed body:

Open
import { DynamicPlugin, FlowCtxOf, HttpHook, Plugin, Provider } from "@frontmcp/sdk";

@Provider({ name: "RequestCounts" })
export class RequestCounts {
  byMethod: Record<string, number> = {};
}

@Plugin({ name: "requests", providers: [RequestCounts], exports: [RequestCounts] })
export class RequestsPlugin extends DynamicPlugin<object> {
  @HttpHook.Will("router")
  async count(ctx: FlowCtxOf<"http:request">) {
    const method: string = ctx.rawInput.request.body?.method ?? "(not JSON-RPC)";
    const { byMethod } = this.get(RequestCounts);
    byMethod[method] = (byMethod[method] ?? 0) + 1;
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

The call to no_such_tool is counted too: the hook ran before FrontMCP found out the tool doesn't exist. The Playground's own requests, like the one it sends when the example starts, go through the same hook.

The order hooks run in

This example puts every kind of hook on one stage, from an app provider, two app plugins, a server plugin and the tool itself, and records the order they ran in:

Open
import { DynamicPlugin, FlowCtxOf, Plugin, ToolHook } from "@frontmcp/sdk";
import { trace, type CallCtx } from "./trace";

@Plugin({ name: "first" })
export class FirstPlugin extends DynamicPlugin<object> {
  @ToolHook.Will("execute")
  async will(ctx: CallCtx) {
    trace(ctx, "first plugin: Will");
  }

  @ToolHook.Did("execute", { priority: 10 })
  async did(ctx: CallCtx) {
    trace(ctx, "first plugin: Did, priority 10");
  }

  @ToolHook.Around("execute", { priority: 5 })
  async inner(ctx: CallCtx, next: () => Promise<void>) {
    trace(ctx, "first plugin: Around, priority 5, before next()");
    await next();
    trace(ctx, "first plugin: Around, priority 5, after next()");
  }

  @ToolHook.Stage("execute", { priority: 10 })
  async afterTheTool(ctx: CallCtx) {
    trace(ctx, "first plugin: Stage, priority 10");
  }
}

@Plugin({ name: "second" })
export class SecondPlugin extends DynamicPlugin<object> {
  @ToolHook.Will("execute", { priority: -10 })
  async will(ctx: CallCtx) {
    trace(ctx, "second plugin: Will, priority -10");
  }

  @ToolHook.Did("execute")
  async did(ctx: CallCtx) {
    trace(ctx, "second plugin: Did");
  }

  @ToolHook.Around("execute")
  async outer(ctx: CallCtx, next: () => Promise<void>) {
    trace(ctx, "second plugin: Around, before next()");
    await next();
    trace(ctx, "second plugin: Around, after next()");
  }

  @ToolHook.Stage("execute")
  async beforeTheTool(ctx: CallCtx) {
    trace(ctx, "second plugin: Stage");
  }
}

@Plugin({ name: "server-wide" })
export class ServerPlugin extends DynamicPlugin<object> {
  @ToolHook.Will("execute")
  async will(ctx: CallCtx) {
    trace(ctx, "server plugin: Will");
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

The Did lines are in the result because the hooks add them to the same array after the tool returns it and before FrontMCP formats it.

Ordering one plugin before another

A named constraint takes precedence over priority. Here first runs before second even though its priority is higher:

Open
import { App, Plugin, Tool, ToolContext, ToolHook, FlowCtxOf, z } from "@frontmcp/sdk";

@Plugin({ name: "first" })
class First {
  @ToolHook.Will("execute", { before: ["second"], priority: 500 })
  async mark(ctx: FlowCtxOf<"tools:call-tool">) { ctx.state.toolContext.input.marks.push("first"); }
}
@Plugin({ name: "second" })
class Second {
  @ToolHook.Will("execute", { priority: -100 })
  async mark(ctx: FlowCtxOf<"tools:call-tool">) { ctx.state.toolContext.input.marks.push("second"); }
}
@Tool({ name: "hook_order", inputSchema: { marks: z.array(z.string()).default([]) } })
class HookOrder extends ToolContext {
  async execute({ marks }: { marks: string[] }) { return { marks }; }
}
@App({ name: "Desk", tools: [HookOrder], plugins: [Second, First] })
export class Desk {}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

before and after name a plugin's name, or the class that declares the hook, optionally followed by .method, like "second.mark". They order hooks of the same kind on the same stage only. New in 1.11.0.


Troubleshooting

HTTP 403: forbidden, HTTP 429: rate_limited or HTTP 503: service_unavailable

An events:publish-http hook reads one of these messages from ctx.error when the IP filter denies the request, the global rate limit is reached, or its store cannot be reached. The error's code is EVENT_PUBLISH_REFUSED and its statusCode is the HTTP status. Check the server's throttle settings or its rate-limit store. Changed in 1.12.2: before, these refusals left ctx.error unset. See Hooking the events flows.

TS1241: Unable to resolve signature of method decorator when called as an expression

The hook's parameter doesn't accept the flow the decorator supplies. Type it FlowCtxOf<"tools:call-tool"> for ToolHook, or the matching flow for another decorator. Use ctx.toolName, ctx.toolInput and ctx.request instead of declaring protected input or scope members on a structural parameter. Only an Around hook accepts a second next parameter.

Changed in 1.10.16: the decorator now checks this parameter. A helper that returned MethodDecorator should leave its return type inferred (FlowHookDecorator); a generic parameter narrower than the flow is also rejected.

Ignored a throw from a Finally hook of '…': it cannot change the stage's outcome

A Finally hook threw, called ctx.fail() or called ctx.respond(). FrontMCP logs the warning and runs the remaining cleanup hooks, preserving the stage's outcome. Use a Will, Stage or Around hook when you need to answer or refuse a request.

My hook never runs

Check, in order:

  1. The class the hook is on is registered: the plugin in plugins, the provider in @App({ providers }).
  2. It's declared on something whose hooks run (where hooks can be declared). Before 1.9, hooks on a provider in @FrontMcp({ providers }) or on a CONTEXT provider never ran; before 1.8.5, AgentCallHook never ran anywhere.
  3. The stage exists in the flow: a hook on a stage name that isn't in the stage list never runs.
  4. The request reaches the stage. Did hooks are skipped when their stage fails, and a failed stage skips every later stage but the closing ones.
  5. For a plugin on an app: the call is to one of that app's tools, or the job is one of that app's jobs. For every app's, register the plugin on @FrontMcp, or give the hook appliesTo: "uncovered-apps" to reach the apps that don't have the plugin. For a plugin on an agent: the call comes from the agent's model, a tool call or, since 1.9.4, a read of the agent's resources or prompts, not from a client.
  6. filter returns true for this request.

Tool "…" declares hooks that would never run

The server didn't start, with InvalidHookFlowError (INVALID_HOOK_FLOW). The full message names each hook that can't run, and why:

Tool "Early" declares hooks that would never run: tooSoon() (will 'findTool' of tools:call-tool): runs before 'createToolCallContext' builds the instance it runs on; declare it as a static method to run it without an instance. Hooks declared as instance methods on a tool class run on the instance built for each call; static methods run without one, from the first stage of a call. Declare hooks for list flows on a provider or a plugin instead.

An instance method hooks a stage before the entry's instance is built. Make it static, reading ctx.state instead of this, as in Hooking the stages before an entry's instance, or move it to a plugin. For a list flow, like (did 'findTools' of tools:list-tools): list flows build no entry instance, move the hook to a plugin or a provider: static doesn't help there. Resource "…", Prompt "…", Agent "…" and Job "…" read the same way. A hook for another flow, like a ToolHook on a job class, fails with Job "…" has hooks for unsupported flows: … on tools:call-tool.

Flow exited without producing output

The call ended with no result, code FLOW_EXITED_WITHOUT_OUTPUT. An Around hook caught the error next() threw and didn't rethrow it, or returned without calling next() and without setting a result:

Open
import { DynamicPlugin, FlowCtxOf, Plugin, ToolHook } from "@frontmcp/sdk";

@Plugin({ name: "log-errors" })
export class LogErrorsPlugin extends DynamicPlugin<object> {
  @ToolHook.Around("execute")
  async logFailures(ctx: FlowCtxOf<"tools:call-tool">, next: () => Promise<void>) {
    try {
      await next();
    } catch (error) {
      ctx.scopeLogger.error(`${ctx.state.tool?.metadata.name} failed`);
      // 🚩 The error is swallowed, and the call has no result
    }
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

Add throw error; after logging, and the client gets the tool's own error, There's no ticket T-9., again. To answer instead of failing, give the call a result: assign ctx.state.toolContext.output before returning, or call ctx.respond(). ctx.state.set("output", …) isn't read.

Provider "…" is not available from ctx.get() in a hook

ctx.get() only finds the server's providers, the ones in @FrontMcp({ providers }):

Open
import { DynamicPlugin, FlowCtxOf, Plugin, ToolHook } from "@frontmcp/sdk";
import { DeskSettings } from "./desk-settings";

@Plugin({ name: "region" })
export class RegionPlugin extends DynamicPlugin<object> {
  @ToolHook.Will("execute")
  async checkRegion(ctx: FlowCtxOf<"tools:call-tool">) {
    // 🚩 DeskSettings is an app provider: ctx.get() can't find it. this.get(DeskSettings) can.
    const settings = ctx.get(DeskSettings);
    ctx.scopeLogger.info(`region ${settings.region}`);
  }
}

Starting FrontMCP in your browser…

FrontMCP starts when this example comes into view.

In a plugin's hook, use this.get(), which finds the plugin's providers and the app's. In any tools:call-tool hook, ctx.state.toolContext?.get() finds what the tool would.

The client gets "content": [] after ctx.respond()

ctx.respond() sends what you pass as the whole result. Pass a complete tools/call result: { content: [{ type: "text", text }], structuredContent? }.

My Did hook doesn't see failed calls

Did hooks only run when their stage worked. Use Finally("execute") for every outcome of execution, or Finally("finalize") for the outcome of the call, and read ctx.error. Did("finalize") is skipped when finalization itself fails, for example when the output schema refuses the answer.

ctx.respond() in a Did hook does nothing

By the time a Did hook runs, the stage has finished, and FrontMCP ignores ctx.respond() there. In Did("execute"), assign ctx.state.toolContext.output to change the result, as in Hooking into Calls.

The client sees "Internal FrontMCP error" instead of my hook's message

The hook threw an error that isn't a PublicMcpError, and the server runs in production, which hides the messages of other errors. Throw new PublicMcpError("…") for messages the model should read.

The before/after options … form a cycle

Remove one constraint named by HookOrderCycleError. An unmatched target instead logs but no hook on that stage matches it, so it changes nothing; check its plugin or class name and method.

Report a docs issue · Report a security concern privately