natprof-mcp

natprof-mcp

macOS native profiling and debugging MCP server that wraps sample, atos, vmmap, leaks, and lldb to enable an agent to profile, symbolicate, and debug running or freshly launched processes without manual path or port handling.

Category
访问服务器

README

natprof-mcp

macOS native profiling and debugging over MCP. One session, keyed to a pid — wraps sample, atos, vmmap, leaks, and lldb so an agent can profile, symbolicate, and debug a running (or freshly launched) process without touching paths, slides, ports, or protocol-server directly.

Works against any Mach-O + DWARF binary: C, C++, Rust, Swift, Go, Zig.

How profiling works

record/hotspots/light_backtrace sample each thread's stack by briefly suspending it one at a time, rather than freezing the whole process — any latency-sensitive threads (audio callbacks, a render loop, network I/O) keep running between samples instead of stalling. This is different from backtrace_all, which uses LLDB's process interrupt and does freeze everything for as long as the dump takes; reach for that one only when you need real LLDB commands (breakpoints, expr, frame variable) alongside the stacks.

record() doesn't block for its seconds duration — MCP tool calls are otherwise synchronous, so a caller with no way to background one would sit fully frozen for the whole recording with no visibility and no way to bail out. It returns immediately once sample is kicked off; poll record_status() until it reports "done", then call hotspots().

Correctness model

Symbols are either right or the call fails, never silently wrong:

  • Images are snapshotted when a session opens. Symbolication reads the frozen copy, so rebuilding mid-session can't corrupt a trace.
  • Every image's LC_UUID is captured at session-open and re-verified before every atos call. UUID changes iff the binary changes — unlike mtime, which both false-positives (touch, git checkout, ccache) and false-negatives (copied trace file).
  • dSYM UUID is checked against the binary UUID separately. A stale dSYM is a different bug than a rebuilt binary, and the snapshot alone doesn't catch it.
  • atos failure yields the hex address, never a guessed symbol.

Requirements

  • macOS with the Xcode Command Line Tools (xcode-select --install).
  • Python 3.10+.
  • Terminal (or whichever app launches this server) needs Developer Tools access under System Settings > Privacy & Security > Developer Tools — vmmap, sample, and any lldb attach or launch all go through the same permission check.
  • Attaching to a running process (open_session, or the lazy attach behind debug()/backtrace_all()) requires the target to carry the get-task-allow entitlement — true for local Debug builds, not for a codesigned Release build. launch_session isn't affected by this, since it starts the process itself.

Install

cd natprof-mcp
python3 -m venv .venv
.venv/bin/pip install -e .

Register with Claude Code

Run from inside the natprof-mcp directory (claude mcp add needs an absolute path to the interpreter/entry point):

claude mcp add natprof -- "$(pwd)/.venv/bin/natprof-mcp"

(Or point it at .venv/bin/python -m natprof_mcp.server if you'd rather not rely on the console-script entry point.)

Sessions

Every tool operates on a session_id returned by one of two entry points:

  • open_session(pid, ...) attaches to a process that's already running.
  • launch_session(path, ...) starts a fresh process under LLDB — the only way to set environment variables (like MallocStackLogging) before the target's first instruction runs.

Neither attaches LLDB eagerly by default: an attach is a hard freeze of the whole process, so a profiling-only session shouldn't pay for it unless asked to. debug() and backtrace_all() attach lazily on first use instead; pass open_session(..., attach_debugger=True) if you want it up front.

Tools

Tool Purpose
open_session(pid, dsym_paths=[], attach_debugger=False) Attach to a running process: snapshot images, capture UUIDs.
launch_session(path, args=[], env={}, cwd=None, dsym_paths=[], mem_debug=False, stop_at_entry=False) Launch a fresh binary under LLDB. mem_debug=True sets MallocStackLogging for real leak backtraces.
debug(session_id, command) Run any LLDB command (bt, expr, breakpoint set, ...).
backtrace_all(session_id) Interrupt, dump every thread's backtrace, resume — halts the process for the duration.
light_backtrace(session_id, seconds=1) Low-impact "what's every thread doing" snapshot via sample, without halting the process.
record(session_id, seconds=10, interval_ms=1) Start a stack-sampling profile via sample, in the background. Returns immediately with status: "recording".
record_status(session_id) Poll a record() in progress — "running" or "done".
hotspots(session_id, top_n=20, include_system=False) Symbolicated self-time leaders from the last completed record(). Errors if a recording is still in progress.
set_baseline(session_id) / compare(session_id, threshold_pct=1.0) Snapshot hotspots, then diff a later run against it.
leaks(session_id) Run leaks --list against the pid.
verify(session_id) Re-check every snapshot's UUID and dSYM match.
close_session(session_id, kill=False) Tear down the session. Doesn't kill the process by default — a hard kill skips the target's own graceful-shutdown path (flushing state, closing devices/connections cleanly), so the process is just left running (detached) unless you pass kill=True.

Leak backtraces

leaks() only returns allocation backtraces — not just a count — if MallocStackLogging was set before the target launched, and that can't be turned on retroactively on an already-running process. Use launch_session(path, ..., mem_debug=True) instead of open_session to get them.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选