MCP协议插件

一个统一的MCP协议插件,用于创建和管理MCP服务器,提供MCP工具,使AI能够了解代码库、构建工具甚至控制构建过程。

  • by situ2001
  • TypeScript
  • 本地部署
  • 31 星标
  • 更新于
在 GitHub 查看 situ2001/unplugin-mcp

使用说明

拔下mcp插头

[!重要] 这是一项正在进行的工作。尚未准备好投入生产使用。如果您有兴趣并想提供帮助,请随时打开问题或PR。

一个统一的MCP(模型上下文协议)插件,用于创建和管理MCP服务器,并提供MCP工具,人工智能可以通过这些工具更多地了解你的代码库、构建工具,甚至控制构建过程。它与unplugin支持的多种JavaScript构建工具一起工作,包括Rollup、Vite、Webpack等。

这是这个插件的愿景,为MCP客户端提供统一的MCP服务器和MCP工具。

flowchart LR
    subgraph "Build tools"
        Rollup["Rollup"]
        Vite["Vite"]
        ESBuild["ESBuild"]
        Webpack["Webpack"]
        Rspack["Rspack"]
        Rolldown["Rolldown"]
    end

    subgraph "unplugin-mcp (This plugin)"
        unplugin["unplugin"]
        McpTool["UnpluginMcpTool"]
        McpServer["MCP Server"]
        HTTPServer["HTTP Server"]
    end

    subgraph "MCP Clients"
        Cursor["Cursor"]
        VSCode["VSCode"]
        More["More"]
    end

    Rollup & Webpack & Vite & ESBuild & Rspack & Rolldown --> unplugin
    unplugin --> McpTool
    McpTool --> McpServer
    McpServer --> HTTPServer
    HTTPServer --> Cursor & VSCode & More

特性

  • 🚀 跨平台MCP集成:跨多个构建工具无缝创建和管理MCP服务器。
  • 🧩 双向人工智能集成:不仅为AI助手提供有关代码库的上下文,还使AI能够主动修改和控制您的构建过程。
  • 🧰 丰富的内置工具:用于分析模块依赖关系、检查构建配置、调试错误消息等的内置工具集合。
  • 🛠️ 可扩展工具框架:使用简单的工具创建自定义MCP工具 UnpluginMcpTool 接口,以公开项目特定的信息或功能。
  • 🔍 构建流程集成:在插件链和Rollup等构建工具的挂钩中的任何点无缝集成。
  • 🔄 持久服务器:即使在监视模式下构建完成后也会继续运行,从而实现持续的AI交互。
  • 🌐 标准传输层:使用HTTP和服务器发送事件(SSE)与实现MCP协议的AI助手广泛兼容。

安装

# Install the plugin
pnpm add -D unplugin-mcp

# or install bundler-specific one, it shares the same codebase but only exports the plugin for the specific bundler
pnpm add -D rollup-plugin-mcp

用法

构建工具集成

下面是一个如何将插件与Rollup一起使用的示例。运行时,服务器将自动启动 rollup 处于监视模式(rollup -w).

// rollup.config.js
import { defineConfig } from 'rollup';
import { rollupPlugin as mcp } from 'unplugin-mcp';

// import some built-in tools
import { ModuleTool, BuildConfigTool, BuildErrorTool } from 'unplugin-mcp/tools';

export default defineConfig({
  plugins: [
    // other plugins...
    mcp({
      provideUnpluginMcpTools: () => [
        new ModuleTool(),
        new BuildConfigTool(),
        new BuildErrorTool()
      ]
    }),
    // other plugins...
  ]
});

🚧 其他捆绑器正在使用中。

游标中的用法

只需将MCP服务器添加到光标设置中即可。例如,在 ~/.config/cursor/mcp.json:

{
  "mcpServers": {
    "rollup": {
      "url": "http://localhost:14514/mcp/sse"
    }
  }
}

选项

检查 McpPluginOptions 在……里面 类型文件 所有可用选项。

内置工具兼容性

注:目前,内置工具的实现相对简单,可能无法覆盖所有边缘情况。

工具 描述 汇总 Webpack
ModuleTool 分析模块依赖关系和导入 ✅ ❌
BuildConfigTool 检查构建配置 ✅ ✅
BuildErrorTool 调试生成错误 ✅ ✅
BundleSizeTool 检查捆包及其模块的尺寸 ✅ ❌
  • ✅ = 支持
  • ❌ = 尚未实施

自定义工具

您可以使用自定义工具扩展插件,实现 UnpluginMcpTool 接口:

import { InputOptions } from "rollup";
import { UnpluginMcpTool, UnpluginMcpToolSetupOptions } from "unplugin-mcp";
import DeferredCtor, { Deferred } from 'promise-deferred';
import { UnpluginOptions } from "unplugin";

export class BuildConfigTool implements UnpluginMcpTool {
  private buildConfig: Deferred<InputOptions>;

  affectsBuildProcess: boolean = false;

  constructor() {
    this.buildConfig = new DeferredCtor<InputOptions>();
  }

  setupMcpServer(mcpServer: any, options?: any) {
    mcpServer.tool(
      `get-build-config`,
      "Get build configuration",
      {},
      async () => {
        const cfg = await this.buildConfig.promise;

        return {
          content: [
            {
              type: 'text',
              text: `Build configuration: ${JSON.stringify(cfg)}`
            }
          ]
        };
      }
    );

    return mcpServer;
  }

  registerPlugins(options?: UnpluginMcpToolSetupOptions): UnpluginOptions {
    let self = this;

    return {
      name: 'build-config-tool',

      rollup: {
        options(config) {
          self.buildConfig.resolve(config);
        }
      }
    }
  }
}

然后在插件选项中注册它,例如在Rollup配置中:

// rollup.config.js
// ... 
plugins: [
  mcp({
    provideUnpluginMcpTools: () => [
      new BuildConfigTool()
    ]
  })
]
// ...

例子

查看示例目录中的工作示例,包括:

运作原理

它初始化并设置这些组件:

  1. 创建并设置单例MCP服务器实例。
  2. 注册一些 UnpluginMcpTool 将实例发送到MCP服务器。
  3. 创建HTTP服务器并为MCP服务器设置HTTP路由。
  4. 启动HTTP服务器,在指定的端口和主机上侦听。
  5. 注册由创建的钩子 UnpluginMcpTool 实例来构建工具。

完成这些步骤后,插件可以:

  1. 处理来自MCP客户端的传入请求并对其作出响应。
  2. 在构建过程中对构建工具触发的钩子做出反应。
  3. 向MCP客户端提供构建上下文信息。

许可证

MIT许可证。版权所有(c)2025 situ2001。