mcpField
P
Perfonext Profiler MCP

Perfonext Profiler MCP

本地服务
github.com/souvikdu开发工具03 周前更新

关于 Perfonext Profiler MCP

Perfonext Profiler MCP 面向排查 Next.js 服务端和脚本性能的开发者,分析 V8 与 Chrome CPU 配置文件。它将数据整理为热点排名、包的 CPU 开销、带注释的源码行、配置比较和优化建议。

主要功能

  1. 01加载 V8 和 Chrome CPU 配置文件
  2. 02按自身耗时排列热点函数
  3. 03用 V8 采样标注源码行
  4. 04按 npm 包汇总 CPU 开销
  5. 05比较配置文件以发现回退
  6. 06为热点生成优化建议

适用场景

  1. 01从 CPU 配置文件中找出高耗时函数
  2. 02识别 CPU 开销较大的依赖
  3. 03检查热点源码行及调用关系
  4. 04比较配置文件以定位性能回退

MCP 相关

这是一个 stdio MCP 服务,向 MCP 客户端提供加载和分析 CPU 配置文件、检查源码上下文、比较配置文件及生成优化建议的工具。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

mcp.json · 通用配置
{
"mcpServers": {
"souvikdu-perfonext-profiler": {
"command": "npx",
"args": [
"-y",
"@perfonext/profiler-mcp"
]
}
}
}
启动命令npx -y @perfonext/profiler-mcp

本地服务需要在你的电脑上安装运行环境(如 Node.js 18+)。各客户端的配置文件位置不同,请参考客户端文档。

安装包
npm@perfonext/profiler-mcp0.7.3

工具

9

收录的工具列表 · 更新于 3 周前

参数类型必填说明
filePathstring是.cpuprofile 或 Chrome trace .json 文件的绝对路径或相对路径

详细介绍

perfonext-profiler-mcp

分析 V8 和 Chrome CPU 性能分析文件,找出 Next.js 服务和脚本中的性能热点。

npm npm downloads license website

perfonext-profiler-mcp 是一个 Model Context Protocol(MCP)服务,可为 GitHub Copilot、Claude Desktop、 Claude Code 和其他 MCP 客户端提供结构化的 CPU 性能分析数据,用于 Next.js 性能优化。它会加载 V8 和 Chrome CPU 性能分析文件,并将其转换为热点排名、逐包开销和带源码注释的热点行——这些证据可供 Agent 推理, 而不必摄取数 MB 的性能分析文件转储。

快速开始

perfonext-profiler-mcp 是一个标准的 MCP stdio 服务,因此可与任何兼容 MCP 的客户端搭配使用 (VS Code 中的 GitHub Copilot、Claude Desktop、Claude Code、Cursor 等)。使用 npx 直接运行:

npx -y @perfonext/profiler-mcp

或全局安装:

npm install -g @perfonext/profiler-mcp

安装后,可执行命令仍为 perfonext-profiler-mcp。

VS Code

将服务添加到 .vscode/mcp.json(工作区 MCP 配置文件):

{
  "servers": {
    "perfonext-profiler": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "@perfonext/profiler-mcp"]
    }
  }
}

重新加载 VS Code 窗口,然后运行 MCP: List Servers 启动服务,或在出现信任提示时接受提示。

Claude Desktop

将服务添加到 claude_desktop_config.json:

{
  "mcpServers": {
    "perfonext-profiler": {
      "command": "npx",
      "args": ["-y", "@perfonext/profiler-mcp"]
    }
  }
}

重启 Claude Desktop 以加载新服务。

Claude Code

使用命令行添加服务:

claude mcp add perfonext-profiler -- npx -y @perfonext/profiler-mcp

或者将相同的 mcpServers 条目添加到 .mcp.json。

其他 MCP 客户端

任何支持 stdio MCP 服务的客户端都可以启动 npx -y @perfonext/profiler-mcp。有关 MCP 服务配置格式, 请参阅客户端文档。

对于本地构建的检出版本,请将 command/args 指向 node 和仓库的 dist/index.js。

故障排除

在使用 nvm 的 macOS 上出现 spawn npx ENOENT / spawn node ENOENT

如果服务因该错误而无法启动,图形界面的 MCP 客户端可能无法访问 nvm。macOS 上的图形应用不会加载 shell 配置文件(.zshrc/.bashrc),因此通过 nvm 安装的 npx/node 不在 PATH 中。 请使用 npx 的绝对路径,并在 PATH 中加入相同的 Node 目录:

{
  "servers": {
    "perfonext-profiler": {
      "type": "stdio",
      "command": "/Users/YOU/.nvm/versions/node/v<version>/bin/npx",
      "args": ["-y", "@perfonext/profiler-mcp"],
      "env": {
        "PATH": "/Users/YOU/.nvm/versions/node/v<version>/bin:/usr/bin:/bin"
      }
    }
  }
}

将这些字段合并到客户端的服务条目中:VS Code 中放在 servers 下,Claude Desktop/Code 中放在 mcpServers 下。然后询问助手:"如何为我的 Next.js 服务采集 CPU 性能分析文件?"

功能

  • 加载 .cpuprofile 文件和包含 CPU 性能分析数据的 Chrome trace 导出文件
  • 根据自身耗时识别最热点的函数,并标注其所属的 npm 包
  • 说明所选函数的调用方和被调用方关系
  • 读取热点函数的实际源码,并为每一行标注 V8 采样数(v0.2.0)
  • 汇总每个 npm 包的 CPU 自身耗时,以找出开销较大的第三方依赖(v0.3.0)
  • 比较两份性能分析文件,找出回归和改进
  • 针对常见热点提供确定性的优化建议
  • 汇总已加载的性能分析文件,帮助 MCP 客户端精简上下文

工具

工具描述
how_to_collect返回可直接运行的命令和分步操作指南,用于捕获 .cpuprofile,然后加载它。如果你还没有性能分析文件,请使用此工具
load_profile从磁盘解析并加载 .cpuprofile 文件或 Chrome 跟踪导出文件
get_hotspots按自身耗时查找排名靠前的函数。每个条目都包含一个 package 字段,用于标识 npm 包或 (user code)
explain_function说明函数的耗时、调用方和被调用方。传入 includeSource: true 可附上带注释的源代码行
read_source_context读取热点函数的实际源文件,并使用 positionTicks 为每一行标注刻度计数
get_package_costs按 npm 包汇总 CPU 自身耗时,显示哪些依赖项开销最大
compare_profiles比较两个性能分析文件并突出显示性能回退
suggest_optimizations为热点函数生成结构化的多模式优化建议。检测高扇入、递归、主导调用方和 V8 特有模式。对分散在多个调用点的同一函数进行去重
get_profile_summary汇总一个性能分析文件,或列出所有已加载的性能分析文件

每个工具结果都会附带一个指向自然后续调用的 nextStep 指引,让 MCP 客户端无需猜测即可完成收集 → 分析 → 修复循环。

示例提示词

  • “如何捕获我的 Next.js 服务的 CPU 性能分析文件?”
  • “加载 ./profile.cpuprofile 处的 CPU 性能分析文件,并显示排名靠前的热点。”
  • “此性能分析文件中,哪些 npm 包消耗的 CPU 最多?”
  • “解释为什么已加载的性能分析文件中 processData 开销很大。”
  • “显示 processData 的实际源代码行,并标出最耗时的行。”
  • “解释 transformResult,并附上带注释的源代码。”
  • “比较我的基准和当前 CPU 性能分析文件,并告诉我哪些地方变慢了。”
  • “为排名前三的热点提供优化建议。”

工具详细参考

各工具的输入/输出架构和手动捕获性能分析文件 ### `how_to_collect` 详情
// Input
{ "scenario": "next-server" } // or "script"; defaults to "next-server"

// Output
{
  "scenario": "next-server",
  "summary": "Profile a production Next.js server while it handles a single request. ...",
  "command": "node --cpu-prof --cpu-prof-dir=./.perf-profiles ./node_modules/next/dist/bin/next start",
  "steps": [ "...", "load_profile({ filePath: \"./.perf-profiles/<file>.cpuprofile\" })" ],
  "outputDir": "./.perf-profiles",
  "nextStep": "After stopping the server, call load_profile with the .cpuprofile ..."
}

next-server 会在生产版 Next.js 服务处理单个请求时对其进行性能分析。如果 next start 提示不支持 standalone 输出,请使用 script 场景,并指定 .next/standalone/server.js。script 会对该 standalone 服务(或其他 Node 入口)进行性能分析。场景仅限一个路由和一次请求。Node 会在输出目录中为每个进程/工作线程写入一个 .cpuprofile 文件。Next 服务命令使用 Node CLI 标志(而非 NODE_OPTIONS),因此在 Unix 和 Windows 上都相同。

read_source_context 详情
// Input
{ "profileId": "<id>", "functionName": "myFn", "contextLines": 10 }

// Output (per line)
{
  "lineNumber": 42,
  "content": "  for (let i = 0; i < items.length; i++) {",
  "ticks": 18,      // V8 samples that landed on this line
  "isHot": true     // true when ticks >= 50% of peak ticks for this function
}

返回的代码窗口会覆盖函数实际的热点代码行,而不只是围绕函数声明的固定范围——函数的真正瓶颈往往远在 function 行之后。contextLines(默认值为 10)用于设置函数声明和热点代码行两侧的最小填充行数;如果仍有采样点落在返回窗口之外,顶层结果会包含 hiddenTicks(计数)和 warning,提示你使用更大的 contextLines 重试。使用 includeSource: true 调用时,explain_function 也接受 contextLines。

只能读取当前工作目录中的文件。file:// URL 和绝对路径均受支持;http://、node: 内置模块以及项目根目录之外的路径则会被拒绝。

suggest_optimizations 详情
// Input
{ "profileId": "<id>", "limit": 5 }

// Output (per function)
{
  "function": "processData",
  "file": "file:///app/src/processor.js",
  "line": 10,
  "selfPercent": "18.2%",
  "patterns": [
    {
      "pattern": "high-fan-in",
      "detail": "Called from 6 distinct call sites (e.g. renderRow, buildTree, …)",
      "suggestion": "This function is a shared hot path. Ensure it is well-optimised and monomorphic …"
    },
    {
      "pattern": "hot-caller",
      "detail": "84% of calls come from \"renderRow\"",
      "suggestion": "Focus optimisation effort on \"renderRow\" rather than this function …"
    }
  ],
  "topSuggestion": "This function is a shared hot path …"
}

检测到的模式(同一个函数可能触发多个模式):

模式触发条件
gc-pressure函数名匹配 GC/Scavenge/MarkCompact
json-serializationJSON.parse / JSON.stringify
regex-costRegExp / exec / test 调用
v8-deoptCompile / Recompile / Optimize / Deoptimize
high-fan-in≥ 3 个不同的父调用点
recursion函数出现在自身的后代子树中
hot-caller单个调用方占调用点出现次数的 ≥ 80%
cpu-bound未匹配其他模式时的回退模式

在排序之前,出现在多个调用点的函数会自动合并,因此同一个逻辑函数只会报告一次。

get_package_costs 详情
// Input
{ "profileId": "<id>", "limit": 10 }

// Output (per package)
{
  "rank": 1,
  "package": "lodash",
  "selfTime": "42.3ms",
  "selfPercent": "14.1%",
  "totalTimeIncludingCallbacks": "58.0ms",
  "totalPercentIncludingCallbacks": "19.3%",
  "topFunctions": [
    { "function": "chunk", "file": "lodash/chunk.js", "line": 41, "selfTime": "28.0ms", "selfPercent": "9.3%" }
  ]
}

selfTime 是在包自身代码中耗费的时间。totalTimeIncludingCallbacks 还会统计该包调用的所有内容——包括传回给它的自定义回调——因此可能会高于移除该包实际能节省的时间。

Scoped 包(如 @babel/core、@next/env 等)会被正确处理。用户代码和原生内置模块(路径中没有 node_modules)会被排除。

生成 CPU 配置文件

让 Copilot 调用 how_to_collect 获取可直接运行的方案,或手动生成:

Next.js 生产服务(对单个请求进行性能分析):

node --cpu-prof --cpu-prof-dir=./.perf-profiles ./node_modules/next/dist/bin/next start
# hit the route once, then stop the process so it can exit and write the profile

如果 next start 提示不支持 standalone 输出:

node --cpu-prof --cpu-prof-dir=./.perf-profiles .next/standalone/server.js

Chrome DevTools:

  1. 打开 DevTools 并进入 Performance 选项卡。
  2. 录制你想检查的场景。
  3. 停止录制并将结果保存为 .cpuprofile 导出文件。

相关 Perfonext 工具

开发

npm install
npm run build
npm test

仓库已在 tests/fixtures/ 下包含用于本地验证的示例夹具。

许可证

MIT

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。