These practices apply when you own the website and can integrate WebMCP tools directly into your codebase. For userscript development, see Managing Userscripts.
Tool Design Principles
Use Descriptive, Consistent Names
Follow a clear naming convention for all your tools:Good Names
products_searchcart_add_itemuser_get_profileorders_list_recent
Avoid
doStuffaction1helperprocessData
domain_verb_noun or verb_noun format:
Provide Detailed Descriptions
Help AI agents understand when and how to use your tools by including everything they need to know in the description:- What the tool does and when to use it
- What data it returns and in what format
- When to use it vs similar tools
- Any important limitations or constraints
- Prerequisites or dependencies on other tools
- If this tool will modify the available tool list
Design Powerful, Consolidated Tools
Create consolidated tools that handle related operations rather than many single-purpose tools:- Good: Consolidated Tools
- Avoid: Too Many Single-Purpose Tools
- Reduces context consumption (fewer tool definitions)
- Modern AI models handle complex tools effectively
- Fewer tools to maintain and document
- Simpler tool discovery for AI agents
- More efficient use of available context window
Input Validation
Use Zod for Type-Safe Validation
Parameter descriptions (via
.describe()) are sent to the AI model’s context. Be detailed and specific!.describe() on every parameter to tell the model exactly what it needs to know:
Validate Business Logic Constraints
Go beyond type checking to enforce business rules:Provide Helpful Error Messages
Response Format
Use Markdown Instead of JSON
AI models work better with markdown-formatted responses than JSON. Markdown is more readable and easier for models to incorporate into natural language responses.
- Good: Markdown
- Avoid: JSON
- More natural for AI to read and present to users
- Better formatting in chat interfaces
- Easier for models to extract specific information
- More human-readable in logs and debugging
Include Helpful Context in Responses
Provide information that helps the AI understand and present the results:Use Specific Error Messages
Help AI agents understand what went wrong with clear, formatted error messages:Security Best Practices
For comprehensive security guidance including authentication, authorization, input validation, prompt injection protection, and multi-website threat models, see the dedicated Security Guide.
Performance Optimization
For comprehensive performance guidelines including tool registration patterns, tool limits, lazy registration, timeouts, and memory management, see the Performance Guidelines.
Optimistic Updates for Voice Models
Voice models and real-time interactions work best with instant tool responses. Implement optimistic updates by operating on in-app state rather than waiting for async API calls:- Good: Optimistic Update
- Avoid: Blocking on API
- Instant tool responses for voice and real-time interactions
- Better user experience with immediate feedback
- Voice models can chain multiple operations smoothly
- Reduced latency in multi-step workflows
- Maintain local application state (Redux, Zustand, React Context)
- Update state synchronously before returning from tool
- Queue background sync operations
- Handle sync failures gracefully with retry logic
Testing & Quality Assurance
Test Tool Registration
Verify tools are properly registered:Test Tool Execution
Verify tool handlers work correctly:Test with Real AI Agents
Use the MCP-B Extension to test with actual AI:1
Install MCP-B Extension
Get it from the Chrome Web Store
2
Load your website
Navigate to your development site where tools are registered
3
Verify tool discovery
Open the extension and confirm your tools appear in the available tools list
4
Test with natural language
Ask the AI agent to use your tools: “Search for laptops under $1000”
5
Verify results
Check that the AI correctly interprets tool responses and presents them to the user
Tool Organization
Group Related Tools
Organize tools logically in your codebase:Use Consistent Prefixes
Group tools by domain using name prefixes:Reference Related Tools in Descriptions
Remember: Tool descriptions go into the model’s context. Reference other tools by name to help the model understand workflows and dependencies.
- A tool should be called before this one
- This tool’s execution will register/unregister other tools
- Data from another tool is needed as input
- Multiple tools work together in a common workflow
Framework Integration
React with Hooks
UseuseWebMCP for component-scoped tools:
Vue with Composition API
Vanilla JavaScript
Documentation
Write for the Model, Not Developers
Put all important information in the tool description and parameter descriptions:Create Tool Catalog for Developers
Maintain a reference document for your development team:Use OpenAPI/JSON Schema
Export tool schemas for documentation:Monitoring & Analytics
Log Tool Usage
Track which tools are being called:Monitor Performance
Track tool execution time:Version Management
Version Your Tools
Include version info in tool names or metadata:Deprecate Gracefully
Warn when tools will be removed:Quick Reference
What goes into model context
What goes into model context
- Tool name, description, and input schema ONLY
- JSDoc and code comments are NOT sent to the model
- Use detailed descriptions and parameter
.describe()methods - Reference other tools by name in descriptions
- Mention if tool execution changes the tool list
Tool design
Tool design
- Prefer consolidated tools over many single-purpose tools
- Reduces context consumption
- Use
domain_verb_nounnaming pattern - Be specific and descriptive in all metadata
Tool descriptions
Tool descriptions
- Include what the tool does and when to use it
- Describe return data format
- List prerequisites and dependencies on other tools
- Mention if this tool registers/unregisters other tools
- Use parameter
.describe()for detailed parameter info
Performance for voice models
Performance for voice models
- Use optimistic updates - update local state first
- Return instantly, sync to backend in background
- Don’t block on async API calls
- Maintain in-app state for fast operations
Response format
Response format
- Return markdown strings instead of JSON objects
- Markdown is more readable for AI models
- Better for natural language presentation
- Include helpful context and formatting
Input validation
Input validation
- Prefer Zod schemas for TypeScript projects
- Use
.describe()on every parameter (goes to model) - Validate both types and business logic
- Provide helpful error messages
Security
Security
- Validate user authentication
- Check authorization for protected resources
- Sanitize all inputs
- Rate limit tool calls
- Never expose sensitive data
Testing
Testing
- Unit test registration and execution
- Test error handling
- Test with real AI agents using MCP-B Extension
Additional Resources
Core Concepts
Learn about WebMCP architecture and design
Quick Start
Get started with your first tool
Security Guide
Security best practices and guidelines
API Reference
Complete API documentation
React Integration
Using WebMCP with React
Examples Repository
See real-world implementations
