Overview
Follow these best practices to build robust, performant, and user-friendly AI integrations with WebMCP.Performance Optimization
Keep tool execution fast
Keep tool execution fast
Frontend tools should execute quickly to maintain responsive UI:
Use React.useMemo for agent creation
Use React.useMemo for agent creation
Only recreate agents when tools actually change:
Optimize tool list updates
Optimize tool list updates
Avoid unnecessary re-renders when tool list updates:
Error Handling
Handle errors gracefully
Handle errors gracefully
Use the
onError callback to handle errors:Handle connection failures
Handle connection failures
Always check connection status before using tools:
Provide user feedback
Provide user feedback
Show loading and error states:
Input Validation
Validate inputs with Zod
Validate inputs with Zod
Use Zod schema validation in your inputSchema:
Provide clear error messages
Provide clear error messages
Make validation errors helpful:
Output Formatting
Return structured content
Return structured content
The
useWebMCP hook automatically formats your return value:Use formatOutput for better AI context
Use formatOutput for better AI context
Help the AI understand results better:
Tool Design
Use clear naming conventions
Use clear naming conventions
Follow verb-noun format with domain prefix:
Write descriptive tool descriptions
Write descriptive tool descriptions
Help the AI understand when to use each tool:
Set appropriate tool annotations
Set appropriate tool annotations
Use annotations to guide AI behavior:
Security
Validate permissions
Validate permissions
Check user permissions before executing sensitive operations:
Sanitize inputs
Sanitize inputs
Never trust input data, even from validated schemas:
Limit rate and scope
Limit rate and scope
Prevent abuse with rate limiting:
Testing
Test tool registration
Test tool registration
Verify tools register correctly:
Mock tool handlers
Mock tool handlers
Test components that use tools:
