get_example
Retrieve working code examples for Minecraft modding tasks with explanations and metadata. Use this tool to get concrete implementations for features like registering items, creating block entities, or handling networking.
Instructions
Get code examples for Minecraft modding topics. Returns complete, working code snippets with full context including explanations, source documentation, and metadata. Use this when you need concrete code examples for implementing features.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| topic | Yes | Topic or pattern to get examples for (e.g., 'register item', 'block entity', 'mixin', 'networking', 'custom armor'). Can be free-form text. | |
| language | No | Programming language (e.g., 'java', 'json', 'groovy') | java |
| loader | No | Mod loader to filter by | |
| minecraft_version | No | Target Minecraft version (e.g., '1.21.4', '1.21.10'). Latest: use 'latest' | |
| category | No | Documentation category to filter by | |
| limit | No | Maximum number of examples to return (1-10) |
Implementation Reference
- src/tools/getExample.ts:22-125 (handler)The primary handler function that implements the core logic of the 'get_example' tool. It validates parameters, queries the ExampleService for relevant code examples based on topic, language, loader, Minecraft version, category, and limit, handles no-results cases with suggestions, formats output for AI consumption, and returns structured CallToolResult.export async function handleGetExample(params: GetExampleParams): Promise<CallToolResult> { try { const { topic, language = 'java', loader, minecraftVersion, category, limit = 5 } = params; // Validate topic if (!topic || !topic.trim()) { return { content: [ { type: 'text', text: 'Error: Topic parameter is required. Please specify what you want examples for (e.g., "register item", "block entity", "networking").', }, ], isError: true, }; } const exampleService = new ExampleService(); if (params.minecraftVersion === 'latest') { params.minecraftVersion = exampleService.getLatestMinecraftVersion(); } // Validate and clamp limit const finalLimit = Math.min(Math.max(limit, 1), 10); console.error(`[get_example] Searching for: "${topic}" (${language}, limit: ${finalLimit})`); // Get examples (synchronous - uses SQLite) const examples = await exampleService.getExamples({ topic, language, loader, minecraftVersion, category, limit: finalLimit, }); // Handle no results if (examples.length === 0) { exampleService.close(); let message = `No code examples found for "${topic}"`; if (language !== 'java') { message += ` in ${language}`; } if (loader) { message += ` for ${loader}`; } if (minecraftVersion) { message += ` (version ${minecraftVersion})`; } message += '.\n\n**Suggestions:**\n'; message += '- Try using more general search terms (e.g., "item" instead of "custom item registration")\n'; message += '- Remove version or loader filters\n'; message += '- Check if the topic is covered in the Fabric documentation\n'; message += '- Try searching with the `search_fabric_docs` tool first\n'; console.error(`[get_example] No results found for "${topic}"`); return { content: [ { type: 'text', text: message, }, ], }; } // Format results for AI const formattedOutput = exampleService.formatForAI(examples); // Close after formatting exampleService.close(); console.error(`[get_example] Returning ${examples.length} example(s) for "${topic}"`); return { content: [ { type: 'text', text: formattedOutput, }, ], }; } catch (error) { console.error('[get_example] Error:', error); return { content: [ { type: 'text', text: `Error retrieving examples: ${error instanceof Error ? error.message : 'Unknown error'}\n\nThe documentation database may not be initialized. Please ensure the indexer has been run with \`npm run index-docs\`.`, }, ], }; } }
- src/tools/getExample.ts:9-16 (schema)TypeScript interface defining the input parameters for the get_example tool handler.export interface GetExampleParams { topic: string; language?: string; loader?: string; minecraftVersion?: string; category?: string; limit?: number; }
- src/index.ts:92-138 (schema)MCP tool input schema definition for 'get_example', specifying parameters, types, descriptions, defaults, enums, and validation rules.inputSchema: { type: 'object', properties: { topic: { type: 'string', description: "Topic or pattern to get examples for (e.g., 'register item', 'block entity', 'mixin', 'networking', 'custom armor'). Can be free-form text.", }, language: { type: 'string', description: "Programming language (e.g., 'java', 'json', 'groovy')", default: 'java', }, loader: { type: 'string', enum: ['fabric', 'neoforge', 'shared'], description: 'Mod loader to filter by', }, minecraft_version: { type: 'string', description: "Target Minecraft version (e.g., '1.21.4', '1.21.10'). Latest: use 'latest'", }, category: { type: 'string', enum: [ 'getting-started', 'items', 'blocks', 'entities', 'rendering', 'networking', 'data-generation', 'commands', 'sounds', ], description: 'Documentation category to filter by', }, limit: { type: 'number', description: 'Maximum number of examples to return (1-10)', default: 5, minimum: 1, maximum: 10, }, }, required: ['topic'], },
- src/index.ts:88-139 (registration)Tool registration object in BASE_TOOLS array, defining name, description, and input schema for the MCP server ListTools handler.{ name: 'get_example', description: 'Get code examples for Minecraft modding topics. Returns complete, working code snippets with full context including explanations, source documentation, and metadata. Use this when you need concrete code examples for implementing features.', inputSchema: { type: 'object', properties: { topic: { type: 'string', description: "Topic or pattern to get examples for (e.g., 'register item', 'block entity', 'mixin', 'networking', 'custom armor'). Can be free-form text.", }, language: { type: 'string', description: "Programming language (e.g., 'java', 'json', 'groovy')", default: 'java', }, loader: { type: 'string', enum: ['fabric', 'neoforge', 'shared'], description: 'Mod loader to filter by', }, minecraft_version: { type: 'string', description: "Target Minecraft version (e.g., '1.21.4', '1.21.10'). Latest: use 'latest'", }, category: { type: 'string', enum: [ 'getting-started', 'items', 'blocks', 'entities', 'rendering', 'networking', 'data-generation', 'commands', 'sounds', ], description: 'Documentation category to filter by', }, limit: { type: 'number', description: 'Maximum number of examples to return (1-10)', default: 5, minimum: 1, maximum: 10, }, }, required: ['topic'], }, },
- src/index.ts:210-219 (registration)Switch case in CallToolRequestSchema handler that routes 'get_example' calls to the handleGetExample function.case 'get_example': { return await handleGetExample({ topic: (args?.topic as string) || '', language: args?.language as string | undefined, loader: args?.loader as string | undefined, minecraftVersion: args?.minecraft_version as string | undefined, category: args?.category as string | undefined, limit: args?.limit as number | undefined, }); }