Using Snippets in WebMCP Documentation
A practical guide for documentation authors on how to use reusable snippets to maintain consistency and reduce duplication.Quick Start
What Are Snippets?
Snippets are reusable MDX files containing common code patterns used throughout the WebMCP documentation. Instead of copying and pasting the same code examples across multiple pages, we import them from a central location.Why Use Snippets?
- Single source of truth - Update code once, changes reflect everywhere
- Consistency - All examples follow the same patterns
- Maintainability - Fix bugs or update APIs in one place
- Speed - Faster to write documentation
When to Use Snippets
✅ Use snippets for:- Code that appears 3+ times across docs
- Standard patterns (tool registration, validation, transports)
- Import statements
- Response formats
- Client setup examples
- Page-specific examples with unique context
- One-off code that appears only 1-2 times
- Tutorial walkthroughs where step-by-step explanation is key
Available Snippets
Core Patterns (snippets/core/)
Tool Registration:
register-tool-basic.mdx- Basic tool registration patternregister-tool-with-cleanup.mdx- Tool with cleanup tracking
use-webmcp-basic.mdx- Basic useWebMCP hook usageuse-webmcp-with-state.mdx- Hook with execution state
response-success.mdx- Standard success responseresponse-error.mdx- Error response formatresponse-markdown.mdx- Markdown-formatted response
Import Statements (snippets/imports/)
react-imports.mdx- React WebMCP setup importsvanilla-imports.mdx- Vanilla JavaScript importsclient-imports.mdx- MCP client imports
Validation (snippets/validation/)
Basic:
zod-basic.mdx- Simple Zod schemajson-schema-basic.mdx- Simple JSON Schema
zod-complex.mdx- Complex Zod with multiple constraintszod-nested.mdx- Nested object schemazod-discriminated-union.mdx- Union types for actionsjson-schema-complex.mdx- Complex JSON Schema
Client Setup (snippets/clients/)
Tab Transports:
tab-server-setup.mdx- TabServerTransport configurationtab-client-setup.mdx- TabClientTransport configuration
iframe-parent-setup.mdx- Parent page setupiframe-child-setup.mdx- Iframe child setup
extension-client-setup.mdx- Extension UI clientextension-server-setup.mdx- Extension background server
mcp-client-provider.mdx- McpClientProvider setup
Patterns (snippets/patterns/)
error-handling.mdx- Standard error handlingfetch-api.mdx- API calls with credentialslifecycle-cleanup.mdx- Cleanup with useEffectoptimistic-update.mdx- Instant UI updates
Templates (snippets/templates/)
Complete, production-ready code:
basic-tool-template.mdx- Ready-to-use basic tool (~100 lines)crud-tool-template.mdx- Full CRUD operations (~200 lines)search-tool-template.mdx- Search with filters (~150 lines)vanilla-tool-template.mdx- Vanilla JS tool (~250 lines)multi-tool-component.mdx- Multiple tools component (~180 lines)provider-with-tools.mdx- Context provider pattern (~200 lines)
/snippets/README.md for complete details on each snippet.
How to Use Snippets
Method 1: Direct Display (Recommended)
Display the snippet code block directly in your documentation:Method 2: Reference in Text
Reference the snippet without displaying it:Method 3: Multiple Snippets
Show multiple related snippets:Real-World Examples
Example 1: Replace Validation Code
Before (duplicated code):Example 2: Replace Transport Setup
Before (duplicated code):Example 3: Complete Template
Before (writing from scratch):Migration Checklist
Use this checklist when updating a documentation page:1. Identify Duplicated Patterns
- Search for
registerToolblocks - Search for
useWebMCPblocks - Search for validation schemas
- Search for transport setup code
- Search for response formats
2. Find Matching Snippets
Check/snippets/README.md for available snippets that match your code.
3. Replace with Imports
For each duplicated pattern:- Add import statement at top of file
- Replace code block with snippet component
- Remove original code block
4. Test Locally
- Verify snippet renders correctly
- Check formatting (icons, line numbers, highlighting)
- Ensure code block attributes are preserved
- Test on different pages if snippet is used multiple times
5. Update Related Pages
If the same pattern appears on other pages:- Find all occurrences:
grep -r "pattern" . - Update each occurrence
- Test each page
6. Commit Changes
Best Practices
Do’s ✅
✅ Import at the top of the file:Don’ts ❌
❌ Don’t modify snippet content in the importing file:-
Update the snippet file:
-
Test affected pages:
-
Commit with clear message:
Creating a New Snippet
When you identify a pattern that should become a snippet:-
Verify it appears 3+ times:
-
Choose the right category:
core/- Essential patternsvalidation/- Schema patternsclients/- Transport setuppatterns/- Advanced patternstemplates/- Complete examples
-
Create the snippet file:
*/}attributes
-
Update
/snippets/README.md:- Add to appropriate section
- Document when to use it
-
Use it in docs:
- Replace existing occurrences
- Test thoroughly
Common Patterns
Pattern: Import Multiple Related Snippets
Pattern: Showing Variations
Pattern: Template + Customization Guide
Troubleshooting
Snippet Not Rendering
Problem: Import doesn’t show code block Solutions:- Check import path is absolute:
/snippets/...not./snippets/... - Verify snippet file exists:
ls snippets/category/snippet-name.mdx - Check for syntax errors in snippet file
- Restart dev server:
mintlify dev
Code Block Formatting Lost
Problem: Snippet shows but loses formatting (icons, line numbers) Solution: Check snippet file has correct code block attributes:File Locations
Summary
Using snippets:- Find pattern in
/snippets/README.md - Import at top of MDX file
- Use snippet component in content
- Test with
mintlify dev
- ✅ Single source of truth
- ✅ Consistency across docs
- ✅ Easy updates
- ✅ Faster authoring
- Use for patterns that appear 3+ times
- Keep page-specific context inline
- Test after changes
- Update
/snippets/README.mdif creating new snippets
