Skip to main content

MCP server for Python performance testing with py-spy

Project description

py-spy MCP Server

A Model Context Protocol (MCP) server that exposes Python performance testing tools powered by py-spy.

License: MIT Python 3.10+ MCP Compatible tests passing

🌐 English | 简体中文

Profile Python in context. Sample live processes, generate flamegraphs, dump stacks, and compare runs — all through MCP.

Features

  • Profile by PID or command — sample a running Python process or launch a new one directly.
  • record_profile — generate profiles in multiple formats:
    • speedscope (interactive JSON)
    • flamegraph (self-contained SVG)
    • raw (stack-count text)
    • chrometrace (Chrome DevTools timeline JSON)
  • dump_stacks — capture the current Python call stacks of a process as JSON or human-readable text.
  • list_python_processes — list running Python processes on the machine to pick a target.
  • analyze_profile — parse an existing profile and return the hottest frames.
  • compare_profiles — compare two speedscope profiles and show percentage changes.
  • top_profile — run a short py-spy top session and return a summary.
    • On Windows, py-spy top cannot be captured through a pipe, so this tool falls back to a short raw recording and returns the hottest frames.
  • Low-overhead sampling — powered by py-spy; reads process memory without modifying or running inside the target process.
  • Cross-platform — works on Linux, macOS, and Windows (subject to OS permissions).
  • Local-source friendly — during development the server automatically prefers a py-spy binary built from the sibling Rust source (src/pyspy/).
  • Optional native/C extension profiling — enable --native where the platform supports it.
  • GIL and idle filtering — focus on active threads or GIL-holding threads.

Installation (from PyPI)

Using pip:

pip install pyspy-mcp

Using uv:

uv pip install pyspy-mcp
# or install as a global tool
uv tool install pyspy-mcp

This will automatically install the compatible py-spy binary wheel for your platform.

Running with Claude Desktop / Claude Code

Add the server as a Local command connector:

{
  "mcpServers": {
    "pyspy": {
      "command": "pyspy-mcp"
    }
  }
}

Or run directly:

pyspy-mcp

The server speaks MCP over stdio.

Development (from source)

If you want to use the local py-spy Rust source instead of the PyPI package:

# Build py-spy from the local Rust source
cargo build --release

# The binary will be at:
#   target/release/py-spy        (Linux / macOS)
#   target/release/py-spy.exe    (Windows)

# Install the Python MCP package in editable mode
pip install -e ".[dev]"

# Or using uv
uv pip install -e ".[dev]"

# Run tests
python -m pytest tests/pyspy_mcp -v

The server will automatically prefer a locally built binary at target/release/py-spy[.exe] over the py-spy installed from PyPI. You can also force a specific binary by setting the environment variable:

export PYSPY_MCP_BINARY=/path/to/py-spy

Publishing to PyPI

python -m build
python -m twine upload dist/*

The published wheel is a pure-Python py3-none-any package and depends on the upstream py-spy PyPI package. If you modify the Rust source and want to ship those changes, you will need to build platform-specific wheels (or bundle the rebuilt py-spy binary as package data).

Permissions

  • On Linux, profiling an existing PID usually requires ptrace permissions (sudo or cap_sys_ptrace).
  • On macOS, profiling often requires root due to System Integrity Protection (SIP).
  • On Windows, running as Administrator may be needed for some processes.

Configuration

Set PYSPY_MCP_BINARY to override the bundled/development py-spy binary location.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyspy_mcp-0.2.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyspy_mcp-0.2.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file pyspy_mcp-0.2.0.tar.gz.

File metadata

  • Download URL: pyspy_mcp-0.2.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for pyspy_mcp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6c0b28dce6e5a35896da517057fbe6eb448c445c5e6742d54b50fcaee93f5368
MD5 db86e01ed17f327a817b23bf68d82a59
BLAKE2b-256 1303533b4bc06cfe1948045f926aa8f9f186ae744b9ac0138751f40629aeaf11

See more details on using hashes here.

File details

Details for the file pyspy_mcp-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyspy_mcp-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for pyspy_mcp-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b61680b933067d3c388de03c27fefb55a484f9d23ec1da53849f9c0cb33a3ce5
MD5 7e2154b86f3e2687a0cae8674cb7f36a
BLAKE2b-256 bc87d1e17e7d449a19be729a9eb0939600089e8bf0c0fbe7a2d8f4869bed65cb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page