babylon-mcp/src/mcp/handlers/editor/search-editor-source.handler.ts
Michael Mainguy e6a3329c9b Add Editor source code tools and expand indexed Babylon.js packages
Adds search_babylon_editor_source and get_babylon_editor_source MCP tools
for searching and retrieving Editor source code. Expands source indexing
to include inspector, viewer, addons, accessibility, node-editor, and
procedural-textures packages. Improves pathToDocId to handle Editor paths
and adds Editor URL construction fallback in getDocumentByPath.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 09:02:20 -06:00

73 lines
2.2 KiB
TypeScript

import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { getSearchInstance } from '../shared/search-instance.js';
import {
formatJsonResponse,
formatNoResultsResponse,
} from '../shared/response-formatters.js';
import { withErrorHandling } from '../shared/error-handlers.js';
export function register(server: McpServer): void {
server.registerTool(
'search_babylon_editor_source',
{
description: 'Search Babylon.js Editor source code files',
inputSchema: {
query: z
.string()
.describe(
'Search query for Editor source code (e.g., "inspector panel", "asset browser")'
),
package: z
.string()
.optional()
.describe('Optional package filter (e.g., "editor", "tools", "website")'),
limit: z
.number()
.optional()
.default(5)
.describe('Maximum number of results to return (default: 5)'),
},
},
withErrorHandling(
async ({ query, package: packageFilter, limit = 5 }) => {
const search = await getSearchInstance();
const options = {
package: packageFilter,
limit,
tableName: 'babylon_editor_source',
};
const results = await search.searchSourceCode(query, options);
if (results.length === 0) {
return formatNoResultsResponse(query, 'Editor source code');
}
const formattedResults = results.map((result, index) => ({
rank: index + 1,
filePath: result.filePath,
package: result.package,
startLine: result.startLine,
endLine: result.endLine,
language: result.language,
codeSnippet:
result.content.substring(0, 500) +
/* c8 ignore next */
(result.content.length > 500 ? '...' : ''),
imports: result.imports,
exports: result.exports,
url: result.url,
relevance: (result.score * 100).toFixed(1) + '%',
}));
return formatJsonResponse({
query,
totalResults: results.length,
results: formattedResults,
});
},
'searching Editor source code'
)
);
}