|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as path from 'path'; |
| 5 | +import { FileSystem, Import, JsonFile, type JsonObject, JsonSchema } from '@rushstack/node-core-library'; |
| 6 | +import { Autoinstaller } from '@rushstack/rush-sdk/lib/logic/Autoinstaller'; |
| 7 | +import { RushGlobalFolder } from '@rushstack/rush-sdk/lib/api/RushGlobalFolder'; |
| 8 | +import { RushConfiguration } from '@rushstack/rush-sdk/lib/api/RushConfiguration'; |
| 9 | +import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp'; |
| 10 | + |
| 11 | +import type { IRushMcpPlugin, RushMcpPluginFactory } from './IRushMcpPlugin'; |
| 12 | +import { RushMcpPluginSessionInternal } from './RushMcpPluginSession'; |
| 13 | + |
| 14 | +import rushMcpJsonSchemaObject from '../schemas/rush-mcp.schema.json'; |
| 15 | +import rushMcpPluginSchemaObject from '../schemas/rush-mcp-plugin.schema.json'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Configuration for @rushstack/mcp-server in a monorepo. |
| 19 | + * Corresponds to the contents of common/config/rush-mcp/rush-mcp.json |
| 20 | + */ |
| 21 | +export interface IJsonRushMcpConfig { |
| 22 | + /** |
| 23 | + * The list of plugins that @rushstack/mcp-server should load when processing this monorepo. |
| 24 | + */ |
| 25 | + mcpPlugins: IJsonRushMcpPlugin[]; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Describes a single MCP plugin entry. |
| 30 | + */ |
| 31 | +export interface IJsonRushMcpPlugin { |
| 32 | + /** |
| 33 | + * The name of an NPM package that appears in the package.json "dependencies" for the autoinstaller. |
| 34 | + */ |
| 35 | + packageName: string; |
| 36 | + |
| 37 | + /** |
| 38 | + * The name of a Rush autoinstaller with this package as its dependency. |
| 39 | + * @rushstack/mcp-server will ensure this folder is installed before loading the plugin. |
| 40 | + */ |
| 41 | + autoinstaller: string; |
| 42 | + |
| 43 | + /** |
| 44 | + * The name of the plugin. This is used to identify the plugin in the MCP server. |
| 45 | + */ |
| 46 | + pluginName: string; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Manifest file for a Rush MCP plugin. |
| 51 | + * Every plugin package must contain a "rush-mcp-plugin.json" manifest in the top-level folder. |
| 52 | + */ |
| 53 | +export interface IJsonRushMcpPluginManifest { |
| 54 | + /** |
| 55 | + * A name that uniquely identifies your plugin. |
| 56 | + * Generally this should match the NPM package name; two plugins with the same pluginName cannot be loaded together. |
| 57 | + */ |
| 58 | + pluginName: string; |
| 59 | + |
| 60 | + /** |
| 61 | + * Optional. Indicates that your plugin accepts a config file. |
| 62 | + * The MCP server will load this schema file and provide it to the plugin. |
| 63 | + * Path is typically `<rush-repo>/common/config/rush-mcp/<plugin-name>.json`. |
| 64 | + */ |
| 65 | + configFileSchema?: string; |
| 66 | + |
| 67 | + /** |
| 68 | + * The module path to the plugin's entry point. |
| 69 | + * Its default export must be a class implementing the MCP plugin interface. |
| 70 | + */ |
| 71 | + entryPoint: string; |
| 72 | +} |
| 73 | + |
| 74 | +export class RushMcpPluginLoader { |
| 75 | + private static readonly _rushMcpJsonSchema: JsonSchema = |
| 76 | + JsonSchema.fromLoadedObject(rushMcpJsonSchemaObject); |
| 77 | + private static readonly _rushMcpPluginSchemaObject: JsonSchema = |
| 78 | + JsonSchema.fromLoadedObject(rushMcpPluginSchemaObject); |
| 79 | + |
| 80 | + private readonly _rushWorkspacePath: string; |
| 81 | + private readonly _mcpServer: McpServer; |
| 82 | + |
| 83 | + public constructor(rushWorkspacePath: string, mcpServer: McpServer) { |
| 84 | + this._rushWorkspacePath = rushWorkspacePath; |
| 85 | + this._mcpServer = mcpServer; |
| 86 | + } |
| 87 | + |
| 88 | + public async loadAsync(): Promise<void> { |
| 89 | + const rushMcpFilePath: string = path.join( |
| 90 | + this._rushWorkspacePath, |
| 91 | + 'common/config/rush-mcp/rush-mcp.json' |
| 92 | + ); |
| 93 | + |
| 94 | + if (!(await FileSystem.existsAsync(rushMcpFilePath))) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const rushConfiguration: RushConfiguration = RushConfiguration.loadFromDefaultLocation({ |
| 99 | + startingFolder: this._rushWorkspacePath |
| 100 | + }); |
| 101 | + |
| 102 | + const jsonRushMcpConfig: IJsonRushMcpConfig = await JsonFile.loadAndValidateAsync( |
| 103 | + rushMcpFilePath, |
| 104 | + RushMcpPluginLoader._rushMcpJsonSchema |
| 105 | + ); |
| 106 | + |
| 107 | + if (jsonRushMcpConfig.mcpPlugins.length === 0) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const rushGlobalFolder: RushGlobalFolder = new RushGlobalFolder(); |
| 112 | + |
| 113 | + for (const jsonMcpPlugin of jsonRushMcpConfig.mcpPlugins) { |
| 114 | + // Ensure the autoinstaller is installed |
| 115 | + const autoinstaller: Autoinstaller = new Autoinstaller({ |
| 116 | + autoinstallerName: jsonMcpPlugin.autoinstaller, |
| 117 | + rushConfiguration, |
| 118 | + rushGlobalFolder, |
| 119 | + restrictConsoleOutput: false |
| 120 | + }); |
| 121 | + await autoinstaller.prepareAsync(); |
| 122 | + |
| 123 | + // Load the manifest |
| 124 | + |
| 125 | + // Suppose the autoinstaller is "my-autoinstaller" and the package is "rush-mcp-example-plugin". |
| 126 | + // Then the folder will be: |
| 127 | + // "/path/to/my-repo/common/autoinstallers/my-autoinstaller/node_modules/rush-mcp-example-plugin" |
| 128 | + const installedPluginPackageFolder: string = await Import.resolvePackageAsync({ |
| 129 | + baseFolderPath: autoinstaller.folderFullPath, |
| 130 | + packageName: jsonMcpPlugin.packageName |
| 131 | + }); |
| 132 | + |
| 133 | + const manifestFilePath: string = path.join(installedPluginPackageFolder, 'rush-mcp-plugin.json'); |
| 134 | + if (!(await FileSystem.existsAsync(manifestFilePath))) { |
| 135 | + throw new Error( |
| 136 | + 'The "rush-mcp-plugin.json" manifest file was not found under ' + installedPluginPackageFolder |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + const jsonManifest: IJsonRushMcpPluginManifest = await JsonFile.loadAndValidateAsync( |
| 141 | + manifestFilePath, |
| 142 | + RushMcpPluginLoader._rushMcpPluginSchemaObject |
| 143 | + ); |
| 144 | + |
| 145 | + let rushMcpPluginOptions: JsonObject = {}; |
| 146 | + if (jsonManifest.configFileSchema) { |
| 147 | + const mcpPluginSchemaFilePath: string = path.resolve( |
| 148 | + installedPluginPackageFolder, |
| 149 | + jsonManifest.configFileSchema |
| 150 | + ); |
| 151 | + const mcpPluginSchema: JsonSchema = await JsonSchema.fromFile(mcpPluginSchemaFilePath); |
| 152 | + const rushMcpPluginOptionsFilePath: string = path.resolve( |
| 153 | + this._rushWorkspacePath, |
| 154 | + `common/config/rush-mcp/${jsonMcpPlugin.pluginName}.json` |
| 155 | + ); |
| 156 | + // Example: /path/to/my-repo/common/config/rush-mcp/rush-mcp-example-plugin.json |
| 157 | + rushMcpPluginOptions = await JsonFile.loadAndValidateAsync( |
| 158 | + rushMcpPluginOptionsFilePath, |
| 159 | + mcpPluginSchema |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + const fullEntryPointPath: string = path.join(installedPluginPackageFolder, jsonManifest.entryPoint); |
| 164 | + let pluginFactory: RushMcpPluginFactory; |
| 165 | + try { |
| 166 | + const entryPointModule: { default?: RushMcpPluginFactory } = require(fullEntryPointPath); |
| 167 | + if (entryPointModule.default === undefined) { |
| 168 | + throw new Error('The commonJS "default" export is missing'); |
| 169 | + } |
| 170 | + pluginFactory = entryPointModule.default; |
| 171 | + } catch (e) { |
| 172 | + throw new Error(`Unable to load plugin entry point at ${fullEntryPointPath}: ` + e.toString()); |
| 173 | + } |
| 174 | + |
| 175 | + const session: RushMcpPluginSessionInternal = new RushMcpPluginSessionInternal(this._mcpServer); |
| 176 | + |
| 177 | + let plugin: IRushMcpPlugin; |
| 178 | + try { |
| 179 | + plugin = pluginFactory(session, rushMcpPluginOptions); |
| 180 | + } catch (e) { |
| 181 | + throw new Error(`Error invoking entry point for plugin ${jsonManifest.pluginName}:` + e.toString()); |
| 182 | + } |
| 183 | + |
| 184 | + try { |
| 185 | + await plugin.onInitializeAsync(); |
| 186 | + } catch (e) { |
| 187 | + throw new Error( |
| 188 | + `Error occurred in onInitializeAsync() for plugin ${jsonManifest.pluginName}:` + e.toString() |
| 189 | + ); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments