goose + Repomix MCP 扩展实战:将整个代码库压缩为 AI 友好格式,让 LLM 在上下文窗口内完成架构分析与测试补全
goose Repomix MCP 扩展实战将整个代码库压缩为 AI 友好格式让 LLM 在上下文窗口内完成架构分析与测试补全【免费下载链接】goosean open source, extensible AI agent that goes beyond code suggestions - install, execute, edit, and test with any LLM项目地址: https://gitcode.com/GitHub_Trending/goose3/goose导读在真实代码评审与测试补全任务中LLM 最大的瓶颈往往不是理解能力而是上下文窗口的容量——完整仓库动辄数万行代码远超一次对话可承载的 token 上限。本教程讲解如何将 Repomix MCP Server 注册为 goose 的扩展利用它把代码仓库打包压缩为适合 AI 读取的紧凑格式从而让 goose 自动化完成仓库打包、代码库分析、架构总结、测试生成与代码探索等任务。阅读完成后你将掌握通过 UI、CLI 或配置文件三种方式安装 Repomix 扩展理解其底层配置ExtensionConfig::Stdio如何驱动 go 的 MCP 调用链并学会用一个完整的实操样例驱动 goose 对指定源码文件做测试覆盖度分析与重构建议。Repomix 是什么为 AI Agent 准备的仓库打包工具在默认情况下goose 通过 Developer 这类内建扩展读写文件、执行命令但它无法一次性读懂整个大型仓库。Repomix 解决的正是这个问题它把整个仓库内容压缩、重组为单份 AI 友好输出如repomix-output.txt或可交互的 Markdown并对文件做摘要、精简和按重要度排序从源头压缩 token 用量让 agent 始终停留在 LLM 的上下文限制之内。goose 官方文档将 Repomix 定位为一款代码库分析与代码组织类扩展在仓库的扩展目录元数据 servers.json 中有对应登记字段值idrepomix-mcpnameRepomixdescriptionRepository analysis and code organizationcommandnpx -y repomix --mcpinstallation_notesInstall using npx package manageris_builtinfalse即 Repomix 并非 goose 内建扩展is_builtin: false需要你按下文手动添加同时它不要求任何环境变量environmentVariables: []安装后即可直接使用。快速安装两种入口goose 把一切可通过 MCPModel Context Protocol连接的能力统称为扩展详见 使用扩展。安装 Repomix 有两种快捷方式二者等价。方式一goose Desktop深链一键安装在桌面端点击如下安装器深链即可完成扩展注册goose://extension?cmdnpxarg-yargrepomixarg--mcpidrepomix-mcpnameRepomixdescriptionPack%20repositories%20into%20AI-friendly%20formats%20for%20goose深链遵循 goose 的扩展协议格式goose://extension?cmdcommandargargumentididnamenamedescriptiondescription。其中cmd的取值只能是jbang、npx、uvx、goosed或docker每条传给npx的参数都必须用独立的arg传递例如上面的-y、repomix、--mcp各占一个argdescription中空格必须 URL 编码为%20。注意深链中的所有参数都必须进行 URL 编码。方式二goose CLI在终端中直接运行npx -y repomix --mcp无论哪种方式前提都是你的系统已安装 Node.js因为该命令依赖npx来拉取并启动 Repomix。手动配置扩展如果希望精细控制扩展名称、超时时间或环境变量可以通过goose configure的交互菜单或直接编辑配置文件来完成添加。通过 goose configure 添加在终端运行goose configure依次选择Add Extension→Command-Line Extension然后按提示填写扩展名称Repomix要执行的命令npx -y repomix --mcp工具超时秒可按需设置例如300是否需要环境变量NoRepomix 无需环境变量直接编辑配置文件goose 使用 YAML 管理扩展设置主配置文件位置为macOS/Linux 位于~/.config/goose/config.yamlWindows 位于%APPDATA%\Block\goose\config\config.yaml。手动添加 Repomix 的配置段形如extensions: repomix: name: Repomix cmd: npx args: [-y, repomix, --mcp] enabled: true type: stdio timeout: 300从 goose 源码结构看这段配置最终会被解析为ExtensionConfig::Stdio并由 acp/provider.rs 中的extension_configs_to_mcp_servers转换成本地 stdio MCP 服务器goose 进程启动cmd即npx把args逐项作为命令行参数传入并通过标准输入/输出与该 MCP 服务器通信。这也解释了为什么配置中的args必须把-y、repomix、--mcp拆成独立数组元素——它们会原样拼接到启动命令之后。启用/停用该扩展可通过goose configure→Toggle Extensions或用会话内斜杠命令/extension npx -y repomix --mcp动态加载动态启用的扩展仅对当前会话生效。实战示例用 goose Repomix 分析 Axios 源码下面演示如何让 goose 借助 Repomix 扩展分析被压缩后的 Axios 仓库goose 将聚焦lib/core/Axios.js文件解释其在架构中的角色、找出缺失测试、编写新的单元测试并提出测试策略与可维护性改进建议。压缩整个代码库为 AI 友好格式后Repomix 帮助 goose 在保持 token 限制的同时产出针对具体文件的高质量洞察。goose Prompt提示词原文Review the file lib/core/Axios.js from the Axios repository and identify any missing unit tests needed to thoroughly cover its core functions and classes. Explain the role this file plays in the overall Axios architecture, how it integrates with other modules, and how well it is currently tested. Write new unit tests to improve coverage, and recommend improvements to the testing strategy or refactoring opportunities to make the code more maintainable.审查 Axios 仓库中的 lib/core/Axios.js识别为了完整覆盖其核心函数与类而缺失的单元测试解释该文件在整体 Axios 架构中的角色、与其他模块的集成方式及其现有测试状况编写新单元测试以提高覆盖率并针对测试策略与代码可维护性提出改进或重构建议。goose 输出完整实录goose 会分三步推进先用 repomix 拉取并打包远程仓库再读取Axios.js及其相关测试文件最后给出结构化分析与测试代码。Axios.js 在架构中的角色Axios.js是 Axios 库最关键的底层组件之一职责包括请求处理内含处理并分发 HTTP 请求的主Axios类配置管理负责合并默认配置与请求级配置拦截器管理管理请求/响应拦截器HTTP 方法便捷封装为各 HTTP 动词get、post 等提供便捷方法它通过以下模块与其他部分集成InterceptorManager处理请求/响应拦截器dispatchRequest发起实际 HTTP 请求mergeConfig处理配置buildFullPath解析 URLAxiosHeaders管理请求头现有测试覆盖分析当前针对 Axios.js 的测试覆盖整体尚可但存在一些缺口。查看相关测试文件test/unit/core/Axios.js—— 主要覆盖错误处理场景特别是不可写un-writable的错误堆栈test/specs/api.spec.js—— 测试公开 API 表面test/specs/interceptors.spec.js—— 测试拦截器功能test/specs/instance.spec.js—— 测试实例创建与配置缺失的测试覆盖点错误处理请求链中的错误传播缺少测试_request(configOrUrl, config) { // Error handling here needs more coverage }配置处理配置校验的边界场景覆盖不足request(configOrUrl, config) { // Config validation edge cases not fully tested }方法别名所有 HTTP 方法别名与各种参数组合缺少测试utils.forEach([post, put, patch], function forEachMethodWithData(method) { // Missing tests for these methods with different data types });拦截器链复杂拦截器链与错误场景缺少测试const chain [dispatchRequest.bind(this), undefined]; chain.unshift.apply(chain, requestInterceptorChain); chain.push.apply(chain, responseInterceptorChain);新增单元测试示例goose 依据上述缺口生成的补全测试节选自其完整输出describe(core::Axios, function() { describe(request method, function() { it(should handle null/undefined config values, function(done) { const instance new Axios({}); instance.request({ url: /foo, params: null, data: undefined }).then(function(response) { expect(response.config.params).toBeNull(); expect(response.config.data).toBeUndefined(); done(); }).catch(done); }); it(should properly merge nested config objects, function(done) { const instance new Axios({ headers: { common: { X-Common: common }, get: { X-Method: get } } }); instance.request({ method: get, headers: { X-Custom: custom } }).then(function(response) { expect(response.config.headers[X-Common]).toBe(common); expect(response.config.headers[X-Method]).toBe(get); expect(response.config.headers[X-Custom]).toBe(custom); done(); }).catch(done); }); }); describe(interceptor chain, function() { it(should handle async interceptors correctly, async function() { const instance new Axios({}); const order []; instance.interceptors.request.use(async function(config) { await new Promise(resolve setTimeout(resolve, 100)); order.push(1); return config; }); instance.interceptors.request.use(async function(config) { await new Promise(resolve setTimeout(resolve, 50)); order.push(2); return config; }); instance.interceptors.response.use(async function(response) { await new Promise(resolve setTimeout(resolve, 100)); order.push(3); return response; }); await instance.request(/foo); expect(order).toEqual([2, 1, 3]); // Interceptors should execute in correct order }); it(should handle interceptor errors properly, function(done) { const instance new Axios({}); const error new Error(Interceptor Error); instance.interceptors.request.use(function() { throw error; }); instance.request(/foo).catch(function(e) { expect(e).toBe(error); done(); }); }); }); describe(HTTP method aliases, function() { it(should handle various data types in post/put/patch, function(done) { const instance new Axios({}); const testCases [ {method: post, data: {foo: bar}}, {method: put, data: string data}, {method: patch, data: new URLSearchParams(foobar)}, {method: post, data: new FormData()}, {method: put, data: new Blob([test])}, {method: patch, data: null} ]; Promise.all(testCases.map(({method, data}) { return instancemethod.then(response { expect(response.config.data).toEqual(data); return response; }); })).then(() done()).catch(done); }); }); });测试策略改进建议模块化测试将测试拆分为更小、更聚焦的单元分别测试每个 HTTP 方法别名为配置处理、拦截器、错误处理建立独立测试套件模拟集成点使用依赖注入以获得更好的隔离性一致地模拟外部依赖创建可复用的 mock 工厂错误处理增加错误场景测试测试错误在拦截器链中的传播校验错误对象属性与继承关系配置处理测试复杂配置合并场景校验默认值是否正确应用测试配置校验边界场景拦截器测试测试复杂拦截器链验证异步拦截器行为测试拦截器错误处理重构机会错误处理——将同步包装改为显式async/await并规范错误类型转换// Current _request(configOrUrl, config) { try { return this.request(configOrUrl, config); } catch (err) { // Error handling } } // Suggested async _request(configOrUrl, config) { try { return await this.request(configOrUrl, config); } catch (err) { if (err instanceof AxiosError) { throw err; } throw AxiosError.from(err); } }配置处理——将请求处理拆分为规范化、校验、执行三个阶段// Current request(configOrUrl, config) { // Config processing } // Suggested request(configOrUrl, config) { const normalizedConfig this._normalizeConfig(configOrUrl, config); this._validateConfig(normalizedConfig); return this._processRequest(normalizedConfig); }拦截器链——将链式构建逻辑从请求逻辑中抽离为独立类// Current // Interceptor chain building mixed with request logic // Suggested class InterceptorChain { constructor(request, config) { this.request request; this.config config; this.chain []; } addRequestInterceptors(interceptors) { // Add request interceptors } addResponseInterceptors(interceptors) { // Add response interceptors } execute() { // Execute chain } }这些改进将提升代码的可维护性与可测试性同时改善错误处理与关注点分离。现有测试覆盖不错但在上述特定区域仍有提升空间新增测试与重构机会将有助于捕获边界场景使代码更加健壮。使用建议与注意事项扩展管理的通用能力Repomix 与 goose 生态中其他npx类扩展一样可通过goose configure新增/切换/移除、桌面端侧边栏 Extensions 面板或直接编辑 config.yaml 中的extensions段进行管理需要为扩展配置密钥或令牌时可在goose configure的扩展菜单中录入实际值保存在 goose 的密钥存储中而非明文配置。一次性会话用法若只想临时体验而无需持久安装可以直接在启动时附加扩展例如goose session --with-extension npx -y repomix --mcp该扩展只会在当前会话生效。环境依赖Repomix 通过npx运行需要本机具备 Node.js 环境在离线或受限网络air-gapped环境下npx 首次拉取包可能失败需参照仓库文档中的离线方案处理。小结通过把 Repomix 作为 goose 的 MCP 扩展接入Agent 得以在有限上下文内完成以往难以企及的整库级任务压缩打包仓库 → 针对具体文件做架构定位与依赖梳理 → 找出测试盲区 → 直接生成可落地的单元测试与重构建议。整个过程本质上是在上下文预算与分析深度之间架起一座桥——既用到了 repomix 的打包压缩能力又依赖 goose 的扩展/工具编排机制stdio 配置解析、会话内动态加载等。如需复现本文流程参考 repomix-mcp.md 与 使用扩展 即可按步骤操作。【免费下载链接】goosean open source, extensible AI agent that goes beyond code suggestions - install, execute, edit, and test with any LLM项目地址: https://gitcode.com/GitHub_Trending/goose3/goose创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考