Skip to content

Interface: MiddlewareContext

@providerprotocol/agents


@providerprotocol/agents / index / MiddlewareContext

Defined in: src/middleware/types.ts:18

Context passed to middleware functions during agent execution.

This context object provides access to the agent, input message, current state, and a metadata map for sharing data between middleware functions in the chain.

The metadata map is mutable and can be used to pass data between before and after hooks of the same middleware, or between different middleware in the chain. Use namespaced keys (e.g., _myMiddleware_key) to avoid collisions.

Middleware for the middleware interface that receives this context

agent: AgentRef

Defined in: src/middleware/types.ts:24

Reference to the agent being executed.

Provides access to agent identity and configuration.


input: Message

Defined in: src/middleware/types.ts:31

The user input message that triggered this execution.

This is the original message passed to the agent’s generate method.


metadata: Map<string, unknown>

Defined in: src/middleware/types.ts:58

Mutable metadata map for sharing data between middleware hooks.

Common uses include storing timing information in before and reading it in after, or passing computed values down the middleware chain.

// In before hook
context.metadata.set('_myMiddleware_startTime', Date.now());
// In after hook
const startTime = context.metadata.get('_myMiddleware_startTime') as number;

state: AgentState

Defined in: src/middleware/types.ts:40

The current agent state, including conversation history.

While the state object itself is accessible, modifications should be handled through proper state management rather than direct mutation.