check_before_suggesting
Verifies imports, methods, and patterns in code suggestions to prevent AI hallucinations and ensure accuracy before proposing solutions.
Instructions
CRITICAL: AI must use this before suggesting any code to prevent hallucinations
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| imports | Yes | List of imports to verify (e.g., ["react", "useState from react"]) | |
| methods | Yes | List of methods to verify (e.g., ["Array.prototype.findLast", "String.prototype.replaceAll"]) | |
| patterns | Yes | List of patterns to verify (e.g., ["async/await", "error boundaries"]) |
Input Schema (JSON Schema)
{
"properties": {
"imports": {
"description": "List of imports to verify (e.g., [\"react\", \"useState from react\"])",
"items": {
"type": "string"
},
"type": "array"
},
"methods": {
"description": "List of methods to verify (e.g., [\"Array.prototype.findLast\", \"String.prototype.replaceAll\"])",
"items": {
"type": "string"
},
"type": "array"
},
"patterns": {
"description": "List of patterns to verify (e.g., [\"async/await\", \"error boundaries\"])",
"items": {
"type": "string"
},
"type": "array"
}
},
"required": [
"imports",
"methods",
"patterns"
],
"type": "object"
}
Implementation Reference
- src/tools/index.ts:38-58 (handler)MCP tool handler case: parses input arguments using Zod schema, calls the core checkBeforeSuggesting function, and formats the result as MCP content response.case 'check_before_suggesting': { const params = z.object({ imports: z.array(z.string()), methods: z.array(z.string()), patterns: z.array(z.string()).optional(), }).parse(args); const result = await checkBeforeSuggesting( params.imports, params.methods, params.patterns ); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- Core tool execution logic: Validates imports against project dependencies, methods against known compatibility issues, patterns against deprecated/risky usages, and collects issues/warnings.export async function checkBeforeSuggesting( imports: string[], methods: string[], patterns?: string[] ): Promise<HallucinationCheckResult> { const result: HallucinationCheckResult = { safe: true, issues: [], warnings: [], }; // Check imports const projectPath = process.env.PROJECT_PATH || process.cwd(); const packageJsonPath = join(projectPath, 'package.json'); if (existsSync(packageJsonPath)) { const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); const allDeps = { ...packageJson.dependencies, ...packageJson.devDependencies, ...packageJson.peerDependencies, }; for (const imp of imports) { // Extract package name from import const packageName = extractPackageName(imp); if (!isBuiltinModule(packageName) && !allDeps[packageName]) { result.safe = false; result.issues.push({ type: 'import', item: imp, issue: `Package "${packageName}" not found in dependencies`, suggestion: `Check if the package is installed or use a different approach`, }); } } } // Check methods for (const method of methods) { const validation = validateMethod(method); if (!validation.valid) { result.safe = false; result.issues.push({ type: 'method', item: method, issue: validation.issue, suggestion: validation.suggestion, }); } } // Check patterns if (patterns) { for (const pattern of patterns) { const validation = validatePattern(pattern); if (!validation.valid) { result.safe = false; result.issues.push({ type: 'pattern', item: pattern, issue: validation.issue, suggestion: validation.suggestion, }); } } } // Add warnings for common mistakes if (methods.some(m => m.includes('findLast'))) { result.warnings.push( 'Array.prototype.findLast() is not available in all environments. Consider using a polyfill or alternative approach.' ); } if (imports.some(i => i.includes('react') && i.includes('useSyncExternalStore'))) { result.warnings.push( 'useSyncExternalStore is only available in React 18+. Verify the project React version.' ); } return result;
- src/tools/tool-definitions.ts:5-27 (schema)Tool definition object including name, description, and input schema for the check_before_suggesting tool.name: 'check_before_suggesting', description: 'CRITICAL: AI must use this before suggesting any code to prevent hallucinations', inputSchema: { type: 'object', properties: { imports: { type: 'array', items: { type: 'string' }, description: 'List of imports to verify (e.g., ["react", "useState from react"])', }, methods: { type: 'array', items: { type: 'string' }, description: 'List of methods to verify (e.g., ["Array.prototype.findLast", "String.prototype.replaceAll"])', }, patterns: { type: 'array', items: { type: 'string' }, description: 'List of patterns to verify (e.g., ["async/await", "error boundaries"])', }, }, required: ['imports', 'methods', 'patterns'], },
- src/tools/index.ts:27-30 (registration)Registration of tool list handler that returns the toolDefinitions array including check_before_suggesting.server.setRequestHandler(ListToolsRequestSchema, async () => { console.error(`Handling tools/list request, returning ${toolDefinitions.length} tools`); return { tools: toolDefinitions }; });
- src/minimal-server.ts:25-35 (registration)Inline tool registration in minimal server implementation.name: 'check_before_suggesting', description: 'CRITICAL: AI must use this before suggesting any code to prevent hallucinations', inputSchema: { type: 'object', properties: { imports: { type: 'array', items: { type: 'string' } }, methods: { type: 'array', items: { type: 'string' } }, patterns: { type: 'array', items: { type: 'string' } }, }, required: ['imports', 'methods', 'patterns'], },