Back to catalogue
ContextUserPromptExpansion
Inject context on skill invocation
Auto-attach a checklist to any slash command
Injects skill-specific additional context whenever a slash command expands into a prompt. A configurable map lets you attach focused instructions (security checklist, style guide, domain rules) to specific skills without editing their definition.
What does the Inject context on skill invocation hook do?
Inject context on skill invocation is a Claude Code UserPromptExpansion hook. It fires automatically at that lifecycle event — outside the model, so it can't be skipped or forgotten. Auto-attach a checklist to any slash command.
Use cases
- Attach a security checklist automatically when /security-review is invoked
- Inject the team coding style guide when /code-review runs
- Pass domain-specific constraints to any skill without modifying its source
Tags
#skills#context#slash-commands#customization
settings.json fragment
{
"hooks": {
"UserPromptExpansion": [
{
"hooks": [
{
"command": "node $CLAUDE_PROJECT_DIR/.claude/hooks/user-prompt-expansion-skill-context.mjs",
"type": "command"
}
]
}
]
}
}Script · .claude/hooks/user-prompt-expansion-skill-context.mjs
#!/usr/bin/env node
// Injecte du contexte additionnel lors de l'expansion de certains skills (UserPromptExpansion)
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
// Associe un nom de skill au contexte à injecter
const CONTEXT_MAP = {
'code-review': 'Check for security vulnerabilities, adherence to SOLID principles, and the conventions in CLAUDE.md.',
'security-review': 'Follow OWASP Top 10. Flag hardcoded secrets, injection risks, and insecure dependencies.',
};
export function run(input) {
const skill = input.command_name ?? '';
const ctx = CONTEXT_MAP[skill];
if (!ctx) return null;
return {
hookSpecificOutput: {
hookEventName: 'UserPromptExpansion',
additionalContext: ctx,
},
};
}
/* v8 ignore next 5 */
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const input = JSON.parse(readFileSync(0, 'utf8'));
const result = run(input);
if (result) process.stdout.write(JSON.stringify(result));
}