rollup-plugin-mcp

rollup-plugin-mcp

用于Rollup的MCP Server集成。

简介

rollup-plugin-mcp 是一个将 MCP 服务器集成到 Rollup 构建过程中的插件,支持双向 AI 集成和丰富的模块信息暴露。

注意:这是一个正在开发中的项目,API 和功能可能会有变化。

安装

pnpm add -D rollup-plugin-mcp

基本使用

rollup.config.js 中添加插件:

import { defineConfig } from 'rollup';
import mcp from 'rollup-plugin-mcp';
import { ModuleTool, BuildConfigTool, BuildErrorTool } from 'rollup-plugin-mcp/tools';

export default defineConfig({
  input: 'src/index.ts',
  output: {
    file: 'dist/bundle.js',
    format: 'esm'
  },
  plugins: [
    mcp({
      provideRollupMcpTools: () => [
        new ModuleTool(),
        new BuildConfigTool(),
        new BuildErrorTool()
      ]
    }),
  ]
});

主要特性

  • 🚀 MCP 服务器集成:在构建过程中管理 MCP 服务器
  • 🧩 双向 AI 集成:AI 可获取代码库信息并控制构建过程
  • 📊 模块信息暴露:提供依赖、配置和错误诊断
  • 🛠️ 可扩展工具框架:创建自定义 MCP 工具
  • 🔄 持久化服务器:构建完成后保持运行

自定义工具

实现 RollupMcpTool 接口扩展功能:

import { InputOptions, PluginHooks } from "rollup";
import { RollupMcpTool } from "rollup-plugin-mcp";
import DeferredCtor, { Deferred } from 'promise-deferred';

export class BuildConfigTool implements RollupMcpTool {
  private buildConfig: Deferred<InputOptions> = new DeferredCtor<InputOptions>();
  affectsBuildProcess: boolean = false;

  setupMcpServer(mcpServer: any, options?: any) {
    mcpServer.tool(
      `get-build-config`,
      async () => {
        const cfg = await this.buildConfig.promise;
        return {
          content: [{ type: 'text', text: `构建配置: ${JSON.stringify(cfg)}` }]
        };
      }
    );
    return mcpServer;
  }

  registerRollupHooks(): Partial<PluginHooks> {
    let self = this;
    return {
      options(config) {
        self.buildConfig.resolve(config);
        return config;
      }
    }
  }
}

更多信息

  • 完整配置选项详见 McpPluginOptions
  • 示例请参阅 examples/simple-hello 目录

许可证

MIT © 2025 situ2001