Skip to content

SEP-2282: Server-Declared Behavioural Hooks#2282

Open
heyhayes wants to merge 3 commits intomodelcontextprotocol:mainfrom
heyhayes:sep/server-declared-hooks
Open

SEP-2282: Server-Declared Behavioural Hooks#2282
heyhayes wants to merge 3 commits intomodelcontextprotocol:mainfrom
heyhayes:sep/server-declared-hooks

Conversation

@heyhayes
Copy link
Copy Markdown

Summary

This SEP proposes a new hooks field in MCP ServerCapabilities that allows servers to declare lifecycle hooks — context injections triggered by client-side events such as session start, post-tool-use, and session end.

The core observation: all three major AI coding clients (Claude Code, Codex CLI, Gemini CLI) have independently built client-side hook systems, but MCP servers have no way to participate in them through the protocol. Servers that need behavioural integration — like memory systems that want to remind agents to store learnings at natural breakpoints — must currently reverse-engineer each client's hook configuration format separately.

Key design decisions

  • Context injection, not code execution: Hooks inject text into the agent's context. The agent decides what to do. This sidesteps the trust/sandboxing problem entirely.
  • Declarative: Hooks are JSON data in ServerCapabilities, not callbacks or executable code.
  • Capability negotiation: Clients declare which events they support; servers only declare hooks for supported events. Fully backward compatible — existing clients ignore the field.
  • Priority levels: suggestion, important, required — clients control which levels they honour.
  • Dynamic context: Hooks can optionally call a server tool and inject its result, enabling patterns like "search my memory at session start."

Evidence

Built from real-world experience with Annal, a semantic memory MCP server. The annal install CLI currently writes hook configs into three different client settings files (190 lines of client-specific code). With static SERVER_INSTRUCTIONS alone, an agent stored only 2 memories across a full implementation session. With a post-commit hook injecting a one-line reminder, storage behaviour changed immediately.

Seeking sponsor

Looking for a maintainer sponsor to shepherd this through review. The proposal touches on server capabilities, client-server negotiation, and agent behaviour — happy to iterate on any aspect.

🤖 Generated with Claude Code

heyhayes and others added 3 commits February 21, 2026 15:46
Propose a new `hooks` field in MCP ServerCapabilities that allows servers
to declare lifecycle hooks — context injections triggered by client-side
events (session start, post-tool-use, session end, etc.). This enables
servers to participate in the behavioural lifecycle that all three major
AI coding clients have independently built, without client-specific code.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Run prettier on SEP markdown and npm run generate:seps to produce
the required .mdx and docs.json entries.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@heyhayes heyhayes requested a review from a team as a code owner February 21, 2026 15:50
@localden localden added proposal SEP proposal without a sponsor. SEP labels Feb 21, 2026
@olaservo
Copy link
Copy Markdown
Member

I feel that calling this "hooks" might be misleading since they're more like 'just in time instructions' or 'lifecycle scoped instructions.' The existing client hooks (Claude Code, Codex CLI, Gemini CLI) execute shell commands deterministically, and the SEP's own motivation is built on a bash script that reliably fired and produced output. Am I understanding this SEP correctly, or are the hooks in this proposal actually meant to do the same?

@heyhayes
Copy link
Copy Markdown
Author

I feel that calling this "hooks" might be misleading since they're more like 'just in time instructions' or 'lifecycle scoped instructions.' The existing client hooks (Claude Code, Codex CLI, Gemini CLI) execute shell commands deterministically, and the SEP's own motivation is built on a bash script that reliably fired and produced output. Am I understanding this SEP correctly, or are the hooks in this proposal actually meant to do the same?

@olaservo Hey,
The way I see it is we can nudge the LLMs in the right places to do certain actions (after tool call, after sub agent finishes, etc.) — not execute code deterministically like client hooks do. The contract is "here's context the model should have at this moment," not "here's a script to run."

The problem I'm solving concretely: I am developing a semantic memory server (Annal) and agents naturally search it when they hit knowledge gaps, but they forget to store what they've learned because there's no lifecycle point where the server can remind them. Right now I'm working around it with git post-commit hooks that shell out, but that only catches commit boundaries — everything between commits is lost. If the server could inject "review what you just learned and store anything worth preserving" after a subagent returns, the memory quality problem largely solves itself.

Happy to rename throughout the SEP to something like "lifecycle instructions or JIT instructions" if that makes the intent clearer.

Best

David

@localden localden changed the title SEP: Server-Declared Behavioural Hooks SEP-2282: Server-Declared Behavioural Hooks Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

proposal SEP proposal without a sponsor. SEP

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants