|
| 1 | +import { useEffect } from 'react' |
| 2 | + |
| 3 | +export function WebMCP() { |
| 4 | + useEffect(() => { |
| 5 | + if (!navigator.modelContext) return |
| 6 | + |
| 7 | + import(/* webpackChunkName: "snippets-registry" */ '../lib/snippets-registry').then(({ snippets }) => { |
| 8 | + navigator.modelContext.registerTool({ |
| 9 | + name: 'list_snippets', |
| 10 | + description: |
| 11 | + 'List all web performance measurement snippets available on this site. Returns metadata without code.', |
| 12 | + inputSchema: { type: 'object', properties: {} }, |
| 13 | + execute: () => |
| 14 | + snippets.map(({ id, category, title, description, url }) => ({ |
| 15 | + id, |
| 16 | + category, |
| 17 | + title, |
| 18 | + description, |
| 19 | + url, |
| 20 | + })), |
| 21 | + }) |
| 22 | + |
| 23 | + navigator.modelContext.registerTool({ |
| 24 | + name: 'get_snippet', |
| 25 | + description: |
| 26 | + 'Get the full JavaScript code for a specific web performance snippet by its ID.', |
| 27 | + inputSchema: { |
| 28 | + type: 'object', |
| 29 | + properties: { |
| 30 | + id: { |
| 31 | + type: 'string', |
| 32 | + description: |
| 33 | + 'Snippet ID (e.g. "LCP", "CLS", "TTFB", "INP"). Use list_snippets to discover available IDs.', |
| 34 | + }, |
| 35 | + }, |
| 36 | + required: ['id'], |
| 37 | + }, |
| 38 | + execute: ({ id }) => { |
| 39 | + const snippet = snippets.find((s) => s.id === id) |
| 40 | + if (!snippet) { |
| 41 | + return { error: `Snippet "${id}" not found. Use list_snippets to see available IDs.` } |
| 42 | + } |
| 43 | + return snippet |
| 44 | + }, |
| 45 | + }) |
| 46 | + |
| 47 | + navigator.modelContext.registerTool({ |
| 48 | + name: 'search_snippets', |
| 49 | + description: |
| 50 | + 'Search web performance snippets by category and/or keyword. Returns metadata without code.', |
| 51 | + inputSchema: { |
| 52 | + type: 'object', |
| 53 | + properties: { |
| 54 | + category: { |
| 55 | + type: 'string', |
| 56 | + description: |
| 57 | + 'Filter by category. One of: CoreWebVitals, Loading, Interaction, Media, Resources.', |
| 58 | + }, |
| 59 | + query: { |
| 60 | + type: 'string', |
| 61 | + description: 'Keyword to filter by title or description.', |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + execute: ({ category, query } = {}) => { |
| 66 | + let results = snippets |
| 67 | + |
| 68 | + if (category) { |
| 69 | + results = results.filter((s) => s.category === category) |
| 70 | + } |
| 71 | + |
| 72 | + if (query) { |
| 73 | + const q = query.toLowerCase() |
| 74 | + results = results.filter( |
| 75 | + (s) => |
| 76 | + s.title.toLowerCase().includes(q) || |
| 77 | + s.description.toLowerCase().includes(q) || |
| 78 | + s.id.toLowerCase().includes(q) |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + return results.map(({ id, category, title, description, url }) => ({ |
| 83 | + id, |
| 84 | + category, |
| 85 | + title, |
| 86 | + description, |
| 87 | + url, |
| 88 | + })) |
| 89 | + }, |
| 90 | + }) |
| 91 | + }) |
| 92 | + }, []) |
| 93 | + |
| 94 | + return null |
| 95 | +} |
0 commit comments