Hook Types Reference#

CPEX ships with built-in hooks for common AI and application operations. Each hook defines a typed payload (the data your plugin receives) and a result type (what you return). You can also register custom hooks.


Tool Hooks#

Intercept tool/function calls before execution and inspect results after.

tool_pre_invoke#

Fires before a tool is executed.

FieldTypeDescription
namestrTool name
argsdict[str, Any]Tool arguments

Payload: ToolPreInvokePayload | Result: ToolPreInvokeResult

Use cases: block dangerous tools, sanitize arguments, inject defaults, rate limiting.

tool_post_invoke#

Fires after a tool returns its result.

FieldTypeDescription
namestrTool name
resultAnyTool execution result

Payload: ToolPostInvokePayload | Result: ToolPostInvokeResult

Use cases: redact PII from results, validate output format, log responses.


Prompt Hooks#

Intercept prompt template operations before fetching and after rendering.

prompt_pre_fetch#

Fires before a prompt template is fetched and rendered.

FieldTypeDescription
prompt_idstrPrompt template ID
argsdict[str, str]Template arguments

Payload: PromptPrehookPayload | Result: PromptPrehookResult

Use cases: validate prompt arguments, inject context, block prompts by ID.

prompt_post_fetch#

Fires after a prompt template is rendered.

FieldTypeDescription
prompt_idstrPrompt template ID
resultAnyRendered prompt result (messages, description)

Payload: PromptPosthookPayload | Result: PromptPosthookResult

Use cases: filter rendered content, log completions, modify output messages.


Resource Hooks#

Intercept resource access before fetching and after retrieval.

resource_pre_fetch#

Fires before a resource is fetched.

FieldTypeDescription
uristrResource URI
metadatadict[str, Any]Request metadata

Payload: ResourcePreFetchPayload | Result: ResourcePreFetchResult

Use cases: validate URIs, enforce access control, log resource access.

resource_post_fetch#

Fires after a resource is retrieved.

FieldTypeDescription
uristrResource URI
contentAnyFetched resource content

Payload: ResourcePostFetchPayload | Result: ResourcePostFetchResult

Use cases: redact content, validate size limits, scan for sensitive data.


Agent Hooks#

Intercept agent invocations and inspect agent responses.

agent_pre_invoke#

Fires before an agent is invoked.

FieldTypeDescription
agent_idstrAgent identifier
messageslist[Any]Conversation messages
toolslist[str] | NoneAvailable tools
modelstr | NoneModel override
system_promptstr | NoneSystem instructions
parametersdict[str, Any]LLM parameters (temperature, max_tokens, etc.)

Payload: AgentPreInvokePayload | Result: AgentPreInvokeResult

Use cases: enforce model restrictions, filter messages, inject system instructions, limit available tools.

agent_post_invoke#

Fires after an agent responds.

FieldTypeDescription
agent_idstrAgent identifier
messageslist[Any]Response messages
tool_callslist[dict] | NoneTool invocations made by agent

Payload: AgentPostInvokePayload | Result: AgentPostInvokeResult

Use cases: audit agent outputs, content moderation, log tool call patterns.


HTTP Hooks#

Intercept HTTP request processing and implement custom authentication.

http_pre_request#

Fires before any request processing (middleware layer).

FieldTypeDescription
pathstrHTTP path
methodstrHTTP method (GET, POST, etc.)
client_hoststr | NoneClient IP address
client_portint | NoneClient port
headersHttpHeaderPayloadHTTP headers

Payload: HttpPreRequestPayload | Result: HttpPreRequestResult

Use cases: request logging, rate limiting, IP filtering, header injection.

http_post_request#

Fires after request processing completes.

Extends HttpPreRequestPayload with:

FieldTypeDescription
response_headersHttpHeaderPayload | NoneResponse headers
status_codeint | NoneHTTP status code

Payload: HttpPostRequestPayload | Result: HttpPostRequestResult

Use cases: response logging, latency tracking, error rate monitoring.

http_auth_resolve_user#

Fires during user authentication (auth layer).

FieldTypeDescription
credentialsdict | NoneHTTP authorization credentials
headersHttpHeaderPayloadFull request headers
client_hoststr | NoneClient IP
client_portint | NoneClient port

Payload: HttpAuthResolveUserPayload | Result: HttpAuthResolveUserResult

Use cases: custom auth (LDAP, mTLS, external IdP, API key validation).

http_auth_check_permission#

Fires during RBAC permission checks.

FieldTypeDescription
user_emailstrAuthenticated user email
permissionstrRequired permission (e.g., tools.read)
resource_typestr | NoneResource type being accessed
team_idstr | NoneTeam context
is_adminboolWhether user has admin privileges
auth_methodstr | NoneAuthentication method used
client_hoststr | NoneClient IP
user_agentstr | NoneUser agent string

Payload: HttpAuthCheckPermissionPayload | Result: HttpAuthCheckPermissionResult

Use cases: custom authorization, time-based access, IP-based restrictions.


Identity Hooks#

Handle token-based identity resolution and credential delegation for downstream services.

identity_resolve#

Fires on inbound requests to decode, verify, and map a token to a subject identity.

FieldTypeDescription
raw_tokenSecretStrRaw credential (JWT, API key, etc.) — redacted on serialization
sourcestrHow the credential was extracted (bearer, mtls, api_key)
headersdict[str, str]Full HTTP headers
client_hoststr | NoneClient IP
client_portint | NoneClient port

Payload: IdentityPayload | Result: IdentityResolveResult

The result carries an IdentityResult as modified_payload, containing the resolved SubjectExtension or a rejection.

token_delegate#

Fires on outbound calls to exchange or mint a token for a downstream target.

FieldTypeDescription
target_namestrTool, agent, or resource being called
target_typestrEntity type (tool, agent, resource, service)
target_audiencestr | NoneAudience URI for the target
required_permissionslist[str]Permissions needed by the target
trust_domainstr | NoneTrust domain
auth_enforced_bystrWho enforces auth (caller, target, both)
bearer_tokenSecretStr | NoneCaller’s current bearer token

Payload: DelegationPayload | Result: TokenDelegateResult

The result carries a DelegationResult as modified_payload, containing the delegated token and updated delegation chain.


CMF Message Hooks#

Unified hooks that use the Common Message Format for cross-cutting policy evaluation. These parallel the typed hooks above but accept a single MessagePayload wrapping a CMF Message.

HookFires at
cmf.tool_pre_invokeBefore tool execution
cmf.tool_post_invokeAfter tool execution
cmf.llm_inputBefore model/LLM call
cmf.llm_outputAfter model/LLM call
cmf.prompt_pre_fetchBefore prompt fetch
cmf.prompt_post_fetchAfter prompt fetch
cmf.resource_pre_fetchBefore resource fetch
cmf.resource_post_fetchAfter resource fetch

Payload: MessagePayload(message: Message, hook: MessageHookType) | Result: MessageResult

CMF hooks let you write a single plugin that evaluates content at every interception point using one unified interface. See Common Message Format for details.


Summary Table#

HookPayloadCategory
tool_pre_invokeToolPreInvokePayloadTool
tool_post_invokeToolPostInvokePayloadTool
prompt_pre_fetchPromptPrehookPayloadPrompt
prompt_post_fetchPromptPosthookPayloadPrompt
resource_pre_fetchResourcePreFetchPayloadResource
resource_post_fetchResourcePostFetchPayloadResource
agent_pre_invokeAgentPreInvokePayloadAgent
agent_post_invokeAgentPostInvokePayloadAgent
http_pre_requestHttpPreRequestPayloadHTTP
http_post_requestHttpPostRequestPayloadHTTP
http_auth_resolve_userHttpAuthResolveUserPayloadHTTP
http_auth_check_permissionHttpAuthCheckPermissionPayloadHTTP
identity_resolveIdentityPayloadIdentity
token_delegateDelegationPayloadIdentity
cmf.*MessagePayloadCMF

All types are importable from cpex.framework.