Prompts are part of the Model Context Protocol (MCP) specification. WebMCP implements prompts for browser environments, enabling web applications to expose conversational templates to AI agents.
Overview
| Concept | Description |
|---|---|
| Purpose | Generate pre-formatted messages for AI interactions |
| Registration | Via registerPrompt() or provideContext() |
| Arguments | Optional schema-validated parameters |
| Output | Array of role-based messages (user/assistant) |
When to Use Prompts
Standardized Interactions
Create consistent conversation starters across your application
Template Messages
Generate parameterized messages with validated arguments
Workflow Guidance
Guide AI agents through specific interaction patterns
Context Injection
Inject application-specific context into AI conversations
Basic Registration
Register a simple prompt without arguments:Prompts with Arguments
UseargsSchema to accept validated parameters:
Multi-Message Prompts
Return multiple messages to establish conversation context:How AI Agents Use Prompts
When an AI agent wants to use a prompt, it callsgetPrompt() through the MCP protocol. This invokes your registered get() handler:
The
getPrompt() method is called by AI agents through the MCP protocol, not directly from your application code. Your get() handler is invoked automatically when an agent requests the prompt.Listing Available Prompts
Dynamic vs Static Registration
- Dynamic (registerPrompt)
- Static (provideContext)
Use Use for:
registerPrompt() for prompts that may be added/removed at runtime:- Context-dependent prompts
- Feature-specific interactions
- Prompts tied to component lifecycle
Schema Validation
Prompts support both JSON Schema and Zod for argument validation:- JSON Schema
- Zod
Best Practices
Use descriptive names
Use descriptive names
Choose prompt names that clearly indicate purpose. Use kebab-case for multi-word names:
code-reviewinstead ofcodeReviewbug-reportinstead ofreport
Write clear descriptions
Write clear descriptions
Descriptions help AI agents decide when to use a prompt:
Validate all arguments
Validate all arguments
Always use
argsSchema for prompts with parameters. Include descriptions for each property to help AI agents provide correct values.Keep prompts focused
Keep prompts focused
Each prompt should serve a single purpose. Create multiple prompts instead of one complex multi-purpose prompt.
Test with real AI agents
Test with real AI agents
Verify prompts work correctly with actual AI integrations. Check that:
- Arguments are passed correctly
- Messages are formatted as expected
- AI responses are appropriate
