get_hook_example
Generate React Native hook code examples quickly. Specify a hook name to receive a pre-built implementation, ensuring alignment with BluestoneApps coding standards.
Instructions
Get a React Native hook example
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hook_name | Yes | Hook Name |
Implementation Reference
- src/index.ts:298-367 (registration)Full registration of the 'get_hook_example' MCP tool, including description, input schema, and complete inline handler implementation.server.tool( "get_hook_example", "Get a React Native hook example", { hook_name: z.string().describe("Hook Name"), }, async ({ hook_name }) => { if (!hook_name) { return { content: [ { type: "text", text: "Hook name not specified", }, ], }; } try { // First try exact match const result = getExampleContent("hooks", hook_name); if (result.error) { // Try to find by fuzzy match const hooksDir = path.join(CODE_EXAMPLES_DIR, "react-native", "hooks"); const closestMatch = findClosestMatch(hooksDir, hook_name); if (closestMatch) { const fuzzyResult = getExampleContent("hooks", closestMatch); return { content: [ { type: "text", text: fuzzyResult.content?.[0] ?? fuzzyResult.error ?? "Error: No content available", }, ], }; } else { return { content: [ { type: "text", text: `Hook ${hook_name} not found`, }, ], }; } } return { content: [ { type: "text", text: result.content?.[0] ?? result.error ?? "Error: No content available", }, ], }; } catch (err) { console.error(`Error getting hook example ${hook_name}:`, err); return { content: [ { type: "text", text: `Error getting hook example: ${err}`, }, ], }; } }, );
- src/index.ts:301-303 (schema)Input schema for the get_hook_example tool, defining the required 'hook_name' string parameter using Zod.{ hook_name: z.string().describe("Hook Name"), },
- src/index.ts:304-367 (handler)Handler logic for get_hook_example: validates input, attempts exact match for hook example file in resources/code-examples/react-native/hooks, falls back to fuzzy matching using findClosestMatch, reads and returns file content as MCP content block or error message.async ({ hook_name }) => { if (!hook_name) { return { content: [ { type: "text", text: "Hook name not specified", }, ], }; } try { // First try exact match const result = getExampleContent("hooks", hook_name); if (result.error) { // Try to find by fuzzy match const hooksDir = path.join(CODE_EXAMPLES_DIR, "react-native", "hooks"); const closestMatch = findClosestMatch(hooksDir, hook_name); if (closestMatch) { const fuzzyResult = getExampleContent("hooks", closestMatch); return { content: [ { type: "text", text: fuzzyResult.content?.[0] ?? fuzzyResult.error ?? "Error: No content available", }, ], }; } else { return { content: [ { type: "text", text: `Hook ${hook_name} not found`, }, ], }; } } return { content: [ { type: "text", text: result.content?.[0] ?? result.error ?? "Error: No content available", }, ], }; } catch (err) { console.error(`Error getting hook example ${hook_name}:`, err); return { content: [ { type: "text", text: `Error getting hook example: ${err}`, }, ], }; } }, );
- src/index.ts:65-84 (helper)getExampleContent helper function: locates and reads example code files from react-native subcategory directories using findFileInSubdirectories, returns content or error. Used by get_hook_example for fetching hook examples.function getExampleContent(subcategory: string, filename: string): { content?: string[]; path?: string; error?: string } { const searchDir = path.join(CODE_EXAMPLES_DIR, "react-native", subcategory); const filePath = findFileInSubdirectories(searchDir, filename); if (!filePath || !fs.existsSync(filePath)) { return { error: `Example ${filename} not found` }; } try { const content = fs.readFileSync(filePath, 'utf8'); return { content: [content], path: path.relative(BASE_DIR, filePath) }; } catch (err) { console.error(`Error reading example ${filename}:`, err); return { error: `Error reading example ${filename}` }; } }
- src/index.ts:87-109 (helper)findClosestMatch helper: performs substring-based fuzzy matching on filenames in a directory across common JS/TS extensions, returns matching filename without extension. Used by get_hook_example for fallback when exact hook name not found.function findClosestMatch(directory: string, searchName: string, extensions: string[] = ['.js', '.jsx', '.ts', '.tsx']) { if (!fs.existsSync(directory)) return null; let closestMatch = null; for (const ext of extensions) { const files = glob.sync(`${directory}/**/*${ext}`); for (const filePath of files) { const fileName = path.basename(filePath); const fileNameNoExt = path.basename(fileName, path.extname(fileName)); if (fileNameNoExt.toLowerCase().includes(searchName.toLowerCase())) { closestMatch = fileNameNoExt; break; } } if (closestMatch) break; } return closestMatch; }