Skip to content

feat: Add server plugin hooks in DefaultRequestHandler - #835

Merged
bartek-gralewicz merged 3 commits into
mainfrom
bgralewicz/server_call_hooks
Nov 17, 2026
Merged

bartek-gralewicz merged 3 commits into
mainfrom
bgralewicz/server_call_hooks

Conversation

@bartek-gralewicz

Copy link
Copy Markdown
Collaborator

Description

Wires the server hooks from @a2a-js/sdk/plugins (#826) into DefaultRequestHandler. Plugins are registered through a new plugins option.

Example

const timing: A2APlugin = {
  name: 'timing',
  beforeHandlerRun: (req) => void req.context.state.set('start', Date.now()),
  afterHandlerRun: (res) => {
    if (res.isFinal) console.log(res.method, Date.now() - (res.context.state.get('start') as number), 'ms');
  },
};

new DefaultRequestHandler(agentCard, taskStore, executor, undefined, undefined, undefined, undefined, undefined, {
  plugins: [timing],
});```

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🧪 Code Coverage

⬇️ Download Full Report

Base PR Delta
src/plugins/manager.ts 98.9% 100% 🟢 +1.10%
src/server/request_handler/default_request_handler.ts 86.66% 86.9% 🟢 +0.24%
src/server/request_handler/server_plugin_hooks.ts (new) — 100% —
Total 84.81% 84.95% 🟢 +0.14%

Generated by coverage-comment.yml

@bartek-gralewicz
bartek-gralewicz merged commit d0e458f into main Nov 17, 2026
19 checks passed
@bartek-gralewicz
bartek-gralewicz deleted the bgralewicz/server_call_hooks branch November 17, 2026 14:59
bartek-gralewicz added a commit that referenced this pull request Nov 20, 2026
# Description

Runs the execution hooks from `@a2a-js/sdk/plugins` (#826) around every
`AgentExecutor.execute` call made by `DefaultRequestHandler`. They use
the same `plugins` option as the server hooks (#835).

## Behavior

- `beforeExecution` runs before the agent. If it throws, the agent does
not run, plugins that already ran get `afterExecution` with the error,
and the handler's usual failure path publishes a FAILED task.
- The agent runs inside `aroundExecution`. The agent runs detached from
the request, so this is where a plugin can scope it, for example to make
the agent's outbound calls children of an OTel span.
- `afterExecution` runs when the agent returns or throws, with the error
if there is one. The error is rethrown, so the handler's existing
failure handling is unchanged.
- `onExecutionEvent` runs for each event published on the task's bus
while the agent runs. The agent gets the original bus. Events the
handler publishes after the agent throws (the synthetic FAILED task and
status) are not reported.
- `cancelTask` runs without hooks.
- With no execution hooks registered, the handler uses the executor as
given.
bartek-gralewicz added a commit that referenced this pull request Nov 20, 2026
# Description

Calls `onTaskEvent` from `@a2a-js/sdk/plugins` (#826) after every
successful task write made by `DefaultRequestHandler`. The hook uses the
same `plugins` option as the server hooks (#835).

```ts
const transitions: A2APlugin = {
  name: 'transitions',
  onTaskEvent: ({ taskId, fromState, toState }) => {
    if (fromState !== toState) console.log(taskId, fromState, '->', toState);
  },
};
```

## Behavior

- `ResultManager` saves the task, then calls `onTaskEvent` with the
task, the event that caused the write, and the state stored before it:
- a `task` event reports the stored state, or `undefined` when it
creates the task;
  - a `statusUpdate` reports the stored state;
- an `artifactUpdate` reports the same state as `fromState` and
`toState`.
- Nothing is reported when no write happens (message events, updates to
terminal tasks, events for unknown tasks) or when the store write
throws.
- The handler passes its plugins to every `ResultManager` it creates:
`sendMessage`, `sendMessageStream`, and both cancel paths. That includes
the direct CANCELED write when no agent is running.
- A follow-up message to an existing task appends it to the history and
saves it directly. That write is reported with no `event` and an
unchanged state.
- `onTaskEvent` runs after the per-task write lock is released, so a
hook may write to the same task, for example to cancel it, without
deadlocking. The states in each report are captured inside the lock, so
they match that write. Reports from one request arrive in write order.
Reports from concurrent requests on the same task may interleave.
- Errors thrown by `onTaskEvent` are logged and ignored.

`ResultManager` is public, so `plugins` is an optional third constructor
argument and existing callers are unaffected. The fields match
a2a-python's `TaskEventInfo`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants