Skip to content

refactor(@angular/cli): add MCP test support for custom code examples #30850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/angular/cli/src/commands/mcp/mcp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function createMcpServer(
' Registration of this tool has been skipped.',
);
} else {
registerFindExampleTool(server, path.join(__dirname, '../../../lib/code-examples.db'));
await registerFindExampleTool(server, path.join(__dirname, '../../../lib/code-examples.db'));
}
}

Expand Down
44 changes: 40 additions & 4 deletions packages/angular/cli/src/commands/mcp/tools/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { glob, readFile } from 'node:fs/promises';
import path from 'node:path';
import { z } from 'zod';

/**
Expand All @@ -18,10 +20,20 @@ import { z } from 'zod';
* @param server The MCP server instance.
* @param exampleDatabasePath The path to the SQLite database file containing the examples.
*/
export function registerFindExampleTool(server: McpServer, exampleDatabasePath: string): void {
export async function registerFindExampleTool(
server: McpServer,
exampleDatabasePath: string,
): Promise<void> {
let db: import('node:sqlite').DatabaseSync | undefined;
let queryStatement: import('node:sqlite').StatementSync | undefined;

// Runtime directory of examples uses an in-memory database
if (process.env['NG_MCP_EXAMPLES_DIR']) {
db = await setupRuntimeExamples(process.env['NG_MCP_EXAMPLES_DIR']);
}

suppressSqliteWarning();

server.registerTool(
'find_examples',
{
Expand Down Expand Up @@ -67,11 +79,11 @@ Examples of queries:
},
},
async ({ query }) => {
if (!db || !queryStatement) {
suppressSqliteWarning();

if (!db) {
const { DatabaseSync } = await import('node:sqlite');
db = new DatabaseSync(exampleDatabasePath, { readOnly: true });
}
if (!queryStatement) {
queryStatement = db.prepare('SELECT * from examples WHERE examples MATCH ? ORDER BY rank;');
}

Expand Down Expand Up @@ -172,3 +184,27 @@ function suppressSqliteWarning() {
return originalProcessEmit.apply(process, arguments as any);
};
}

async function setupRuntimeExamples(
examplesPath: string,
): Promise<import('node:sqlite').DatabaseSync> {
const { DatabaseSync } = await import('node:sqlite');
const db = new DatabaseSync(':memory:');

db.exec(`CREATE VIRTUAL TABLE examples USING fts5(content, tokenize = 'porter ascii');`);

const insertStatement = db.prepare('INSERT INTO examples(content) VALUES(?);');

db.exec('BEGIN TRANSACTION');
for await (const entry of glob('*.md', { cwd: examplesPath, withFileTypes: true })) {
if (!entry.isFile()) {
continue;
}

const example = await readFile(path.join(entry.parentPath, entry.name), 'utf-8');
insertStatement.run(example);
}
db.exec('END TRANSACTION');

return db;
}