12.2 Agent SDK Hooks, Reusable MCP Catalogs & Team Enablement
Key Takeaways
Hooks run at defined lifecycle events; command/code callbacks can implement programmatic checks, whereas prompt or agent hooks use model judgment.
PreToolUse can deny before execution; PostToolUse cannot undo the operation. Configure and test failure handling, and keep mandatory acceptance checks outside the agent loop.
Use centrally approved MCP integrations, scoped identity and server-side authorization, with managed configuration and controlled dependency updates.
Subagents use separate contexts and configurable tools/modes; context isolation is not a security sandbox. Apply managed restrictions and test actual tool and hook coverage.
Agent SDK setting_sources (Python) or settingSources (TypeScript) selects local configuration sources; current defaults load them, so be explicit and verify effective policies.
12.2 Agent SDK Hooks, Reusable MCP Catalogs & Team Enablement
Once project standards and permission policies are established, reusable hooks, integrations and subagent definitions can help teams apply them consistently. Pair model judgment with programmatic validation and independently enforced runtime boundaries.
Lifecycle Hooks
Claude Code supports command, HTTP, MCP tool, prompt and agent hook handlers where supported by the event. Programmatic hooks can check objective criteria; prompt or agent-based hooks are probabilistic. The Claude Agent SDK exposes programmatic callbacks and can load filesystem hooks.
| Event | Timing | Example |
|---|---|---|
SessionStart | Session begins or resumes | Initialize context or metadata; exit code 2 does not block this event |
UserPromptSubmit | Before prompt submission | Validate input and reject when policy requires it |
PreToolUse | Before a tool call | Check a proposed action and deny unauthorized operations |
PostToolUse | After successful tool execution | Run lint/type checks or record an outcome; cannot undo the action |
Stop | Main agent finishes a turn | Ask agent to continue if required tests fail |
SubagentStop | Subagent finishes responding | Validate or request further work on a delegated result |
In Claude Code command hooks, exit code 2 blocks a blockable event such as PreToolUse; structured denial can also be returned as hookSpecificOutput.permissionDecision set to "deny" with the event name and reason. Use the event-specific schema and exit 0 for a valid structured decision. Stop can return a block decision with a reason to continue work, but a user interrupt, API failure or continuation limit can end a turn. A hook finishing successfully does not establish that all task requirements are met.
Failure Handling and Worked Workflow
Consider a team developing an internal ledger service with read-only test data.
- The test environment provides only the permissions required by the task. A synchronous PreToolUse gate independently checks allowed operations before execution; the database account itself is restricted to read-only access for this workflow.
- PostToolUse after file writes runs the selected non-mutating lint and type checks and returns findings for correction.
- Stop checks whether required tests passed, returning a reason when more work is needed. Guard against unresolved continuation loops; an external CI gate runs the actual tests before accepting changes.
Many failed command hooks do not block the underlying action by default. On supported Claude Code versions, onFailure: "block" on a synchronous pre-action command, HTTP or MCP hook can fail closed; this does not make Stop a fail-closed deployment gate. Agent SDK callback matchers have corresponding onFailure (TypeScript) and on_failure (Python) options on supported versions. Test failure paths and effective permissions. Async hook responses cannot block an action after it proceeds.
As of Agent SDK 0.2, unspecified settings sources load the available local sources. Explicitly select setting_sources=["project"] in Python or settingSources: ["project"] in TypeScript for shared project settings; local-only settings require the local source. An empty list excludes these optional filesystem sources, not automatically all managed restrictions or runtime policies. Configure permissions, callbacks and working environment explicitly. Python SDK SessionStart and SessionEnd are filesystem hooks rather than callback events; TypeScript supports them as callbacks.
Curated Enterprise MCP Integrations
A managed catalog helps teams discover approved tools without installing arbitrary executable dependencies. Prefer vetted remote HTTP integrations when appropriate; local stdio servers may be suitable when reviewed and isolated.
- Use the documented Streamable HTTP transport and optional OAuth-based authorization for remote servers, with scoped credentials and per-user identity where appropriate. OAuth authentication does not by itself enforce record-level authorization; verify tokens and authorize each resource on the server.
- Distribute approved configurations centrally. Claude Code supports a fixed
managed-mcp.jsonconfiguration, provisionedmanagedMcpServers, and managedallowedMcpServers/deniedMcpServersrestrictions. Test the actual matching behavior and effective settings for the deployment surface. - Store secrets securely outside committed
.mcp.jsonand review server dependencies, metadata changes and network scope. A catalog is not a replacement for backend access controls.
Scoped Subagents
Project subagent definitions can live in .claude/agents/, with a description, role prompt and constrained tool list. Delegate focused tasks, such as checking a documentation or migration diff, to preserve main-session context. A subagent may run in a fresh context or a fork of parent context. It is not automatically an isolated process or filesystem boundary.
Subagents use available tool pools, which can be narrowed through tools or disallowedTools, and can have a configured permission mode subject to parent and managed constraints. Apply the relevant managed policies; configure subagent-specific hooks and use SubagentStart/SubagentStop where the lifecycle requires them. Verify that each policy actually applies to the targeted tool and environment. Separate context and a prompt do not substitute for least-privilege runtime authorization.
Exam Trap: PostToolUse cannot prevent a completed mutation, and a Stop hook cannot replace a release gate. Managed rules and constrained tool access must be tested against the real execution environment.
A team permits only read-only database queries in its Claude Code test workflow. Which hook can reject a proposed unauthorized tool call before it runs, in addition to restricting actual database permissions?
PostToolUse after the call completes
Stop after the turn finishes
PreToolUse with a synchronous policy check returning an explicit denial
SessionStart alone
An engineering organization discovers that developers are installing unreviewed community stdio MCP servers via npx to connect Claude Code to Jira and observability dashboards, creating supply-chain and credential-exposure risks. What is the recommended enterprise architecture?
Provide vetted centrally managed remote MCP integrations, scoped per-user authorization at the server, and effective managed server restrictions
Require every developer to commit their personal Jira API token into the repository's .mcp.json file
Disable all MCP support permanently and tell engineers to copy-paste Jira tickets manually
Add a sentence in ~/.claude/CLAUDE.md asking engineers to verify npm packages before running them
A team defines a migration-review subagent with a separate context. Which statement best describes how to preserve organizational security restrictions?
A separate context automatically provides a separate operating-system security boundary
Every subagent has unrestricted access unless its prompt repeats all managed rules
A subagent review result can replace server-side authorization
Use effective managed restrictions and narrowed tool scope; configure applicable subagent/tool hooks and verify runtime permissions
When Claude Code edits Python files in a monorepo, the platform team wants ruff format and mypy to run automatically after every file edit without spending extra LLM tokens to remind Claude, and wants unit tests verified before Claude declares a task finished. Which hook pairing achieves this?
SessionStart for formatting and PreToolUse for unit tests
PostToolUse on Edit/Write for ruff/mypy and Stop for verifying the unit test suite passes
UserPromptSubmit for ruff/mypy and PostToolUse for unit tests
PreToolUse for ruff/mypy and SessionStart for unit tests
Sections you finish are checked off in the contents.