|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 10 | +import { z } from 'zod'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Registers the `find_examples` tool with the MCP server. |
| 14 | + * |
| 15 | + * This tool allows users to search for best-practice Angular code examples |
| 16 | + * from a local SQLite database. |
| 17 | + * |
| 18 | + * @param server The MCP server instance. |
| 19 | + * @param exampleDatabasePath The path to the SQLite database file containing the examples. |
| 20 | + */ |
| 21 | +export async function registerFindExampleTool( |
| 22 | + server: McpServer, |
| 23 | + exampleDatabasePath: string, |
| 24 | +): Promise<void> { |
| 25 | + let db: import('node:sqlite').DatabaseSync | undefined; |
| 26 | + let queryStatement: import('node:sqlite').StatementSync | undefined; |
| 27 | + |
| 28 | + server.registerTool( |
| 29 | + 'find_examples', |
| 30 | + { |
| 31 | + title: 'Find Angular Code Examples', |
| 32 | + description: |
| 33 | + 'Searches for and returns best-practice Angular code examples based on a query from a local set of packaged examples.' + |
| 34 | + ' This should be used before creating any Angular code to ensure that best practices are followed.' + |
| 35 | + ' Results are ranked in order of relevance with most relevant being first.', |
| 36 | + inputSchema: { |
| 37 | + query: z.string().describe('The search query to find Angular code examples.'), |
| 38 | + }, |
| 39 | + }, |
| 40 | + async ({ query }) => { |
| 41 | + if (!db || !queryStatement) { |
| 42 | + suppressSqliteWarning(); |
| 43 | + |
| 44 | + const { DatabaseSync } = await import('node:sqlite'); |
| 45 | + db = new DatabaseSync(exampleDatabasePath, { readOnly: true }); |
| 46 | + queryStatement = db.prepare('SELECT * from examples WHERE examples MATCH ? ORDER BY rank;'); |
| 47 | + } |
| 48 | + |
| 49 | + // Query database and return results as text content |
| 50 | + const content = []; |
| 51 | + for (const exampleRecord of queryStatement.all(query)) { |
| 52 | + content.push({ type: 'text' as const, text: exampleRecord['content'] as string }); |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + content, |
| 57 | + }; |
| 58 | + }, |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +function suppressSqliteWarning() { |
| 63 | + const originalProcessEmit = process.emit; |
| 64 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 65 | + process.emit = function (event: string, error?: unknown): any { |
| 66 | + if ( |
| 67 | + event === 'warning' && |
| 68 | + error instanceof Error && |
| 69 | + error.name === 'ExperimentalWarning' && |
| 70 | + error.message.includes('SQLite') |
| 71 | + ) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, prefer-rest-params |
| 76 | + return originalProcessEmit.apply(process, arguments as any); |
| 77 | + }; |
| 78 | +} |
0 commit comments