Make the context spill threshold configurable for MCP hooks (#63922)

## Why

MCP hooks could only use the default spill threshold. Hooks that return
larger context, such as a session-start memory hook, need a way to keep
more of that context inline.

## What changed

Allow `mcp_tool` hooks to set `additionalContextLimit` for events that
can emit `additionalContext`. Plaintext context spills to disk above
approximately 2,500 tokens by default. MCP hooks can set a threshold
from 1 to 10,000 tokens. A value of 0 or above 10,000 warns and uses
10,000. Hook trust hashes use the effective threshold.

Expose the setting in app-server configuration requirements and hook
list responses.

## Testing

Cover limit normalization, warnings, trust hashes, and app-server
reporting. Verify that a bundled memory hook with a higher threshold
injects its full context at startup and after compaction.

GitOrigin-RevId: 318704ac1532267b3eb2f138f130b6799863a5c0
This commit is contained in:
Dylan Hurd
2026-11-25 14:03:11 +00:00
committed by copyberry
parent 9f3cb25953
commit e8fb104cd6
23 changed files with 230 additions and 42 deletions
@@ -1070,6 +1070,14 @@ class McpToolConfiguredHookHandler(BaseModel):
model_config = ConfigDict(
populate_by_name=True,
)
additional_context_limit: Annotated[
int | None,
Field(
alias="additionalContextLimit",
description="Approximate token threshold for spilling plaintext `additionalContext` to disk. Defaults to 2,500. For MCP hooks, `0` and values above 10,000 trigger a warning and use 10,000. Command hooks can disable spilling with `0` or set a higher threshold. The MCP ceiling mirrors the current default tool-output budget and may change in the future. Encrypted hook output has a separate byte limit.",
ge=0,
),
] = None
input: dict[str, Any]
on_failure: Annotated[ConfiguredHookFailurePolicy | None, Field(alias="onFailure")] = None
server: str
@@ -9379,7 +9387,7 @@ class HookMetadata1(BaseModel):
int | None,
Field(
alias="additionalContextLimit",
description="Configured `additionalContext` spill threshold. `null` uses 2,500 tokens; `0` disables spilling.",
description="Configured `additionalContext` spill threshold. `null` uses 2,500 approximate tokens. Only command hooks support `0` (no spill) or values above 10,000.",
ge=0,
),
] = None
@@ -9416,7 +9424,7 @@ class HookMetadata2(BaseModel):
int | None,
Field(
alias="additionalContextLimit",
description="Configured `additionalContext` spill threshold. `null` uses 2,500 tokens; `0` disables spilling.",
description="Configured `additionalContext` spill threshold. `null` uses 2,500 approximate tokens. Only command hooks support `0` (no spill) or values above 10,000.",
ge=0,
),
] = None
@@ -9453,7 +9461,7 @@ class PromptHookMetadata(BaseModel):
int | None,
Field(
alias="additionalContextLimit",
description="Configured `additionalContext` spill threshold. `null` uses 2,500 tokens; `0` disables spilling.",
description="Configured `additionalContext` spill threshold. `null` uses 2,500 approximate tokens. Only command hooks support `0` (no spill) or values above 10,000.",
ge=0,
),
] = None
@@ -9488,7 +9496,7 @@ class AgentHookMetadata(BaseModel):
int | None,
Field(
alias="additionalContextLimit",
description="Configured `additionalContext` spill threshold. `null` uses 2,500 tokens; `0` disables spilling.",
description="Configured `additionalContext` spill threshold. `null` uses 2,500 approximate tokens. Only command hooks support `0` (no spill) or values above 10,000.",
ge=0,
),
] = None