Skip to main content
Glama
LBurny

py-spy MCP Server

by LBurny

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.

Related MCP server: javaperf

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.

Available Tools

6 tools
analyze_profileA
Read-onlyIdempotent

Analyze an existing py-spy profile and return the hottest frames.

ParametersJSON Schema
NameRequiredDescriptionDefault
top_nNoNumber of top frames to return (default 10).
profile_pathYesPath to a speedscope JSON (.json) or raw (.txt) profile.

Output Schema

ParametersJSON Schema
NameRequiredDescription
resultYes

TDQS

A3.6/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already indicate read-only and idempotent. Description adds 'return the hottest frames' but not what 'hottest' means (CPU? wall time?) or other behavioral traits like sorting or limits. Annotations cover safety, so a 3 is appropriate.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Single sentence that is clear and efficient, no unnecessary words.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

With output schema and annotations present, the description is mostly complete. However, lacks explanation of 'hottest' meaning and does not mention file access requirements.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so baseline is 3. Description does not add meaning beyond the schema; parameters are already described in the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clearly states the verb 'analyze', resource 'existing py-spy profile', and outcome 'return the hottest frames'. Distinguishes from siblings like record_profile, compare_profiles, and dump_stacks.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No explicit guidance on when to use vs alternatives, no prerequisites or exclusions stated. Implies usage but lacks contrast with siblings like compare_profiles or top_profile.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

compare_profilesA
Read-onlyIdempotent

Compare two speedscope profiles and show percentage changes.

ParametersJSON Schema
NameRequiredDescriptionDefault
top_nNoNumber of frames to show (default 10).
profile_aYesPath to the first profile.
profile_bYesPath to the second profile.

Output Schema

ParametersJSON Schema
NameRequiredDescription
resultYes

TDQS

A3.6/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint=true and idempotentHint=true, indicating safe read operations. The description adds no additional behavioral context (e.g., no mention of file permissions, error conditions, or output size). It does not contradict annotations, so a baseline score of 3 is appropriate.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Single sentence that is front-loaded with the action and output. No redundant words or extraneous details. Every word serves a purpose.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the presence of an output schema explaining return values and the clarity of the description for a straightforward comparison tool, the description is mostly complete. It lacks details on prerequisite file existence or error scenarios, but these are less critical with annotations indicating safe operation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Input schema has 100% coverage with descriptions for all parameters. The description does not add extra semantics beyond 'compare' and 'show percentage changes', which are already implied. The parameter 'top_n' is described in schema, so no added value from description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description uses specific verb+resource: 'compare two speedscope profiles' and specifies output 'show percentage changes'. It clearly distinguishes from sibling tools like analyze_profile (single profile analysis) and top_profile (single profile top frame).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. No mention of context such as needing two distinct profiles or that it is for comparative analysis. The presence of sibling tools like analyze_profile implies a distinction, but no explicit instructions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

dump_stacksA
Read-onlyIdempotent

Dump the current Python call stacks of a process.

ParametersJSON Schema
NameRequiredDescriptionDefault
pidYesProcess ID to inspect.
nativeNoInclude native frames if supported.
json_outputNoReturn structured JSON instead of human-readable text.
locals_levelNoNumber of times to pass --locals (0-2). Each level shows more locals.
subprocessesNoInclude child Python processes.

Output Schema

ParametersJSON Schema
NameRequiredDescription
resultYes

TDQS

A3.6/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already provide readOnlyHint=true and idempotentHint=true, indicating safe, read-only behavior. The description adds no further behavioral traits beyond stating the action, so it does not surpass the annotation baseline.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, short sentence that conveys the core functionality immediately. No wasted words.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the presence of an output schema and full parameter descriptions, the description is adequate for a straightforward debugging tool. However, it could benefit from a brief usage hint (e.g., debugging deadlocks).

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100% with detailed parameter descriptions. The tool description adds no additional meaning beyond what the schema provides, so baseline score applies.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action 'Dump' and the resource 'the current Python call stacks of a process.' It distinguishes from sibling profiling tools like analyze_profile or top_profile by specifying call stacks.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives like list_python_processes or analyze_profile. Lacks usage context, prerequisites, or scenarios.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

list_python_processesA
Read-onlyIdempotent

List running Python processes on this machine.

Returns: A table of PID, memory usage, and command line.

ParametersJSON Schema
NameRequiredDescriptionDefault

No parameters

Output Schema

ParametersJSON Schema
NameRequiredDescription
resultYes

TDQS

A4.1/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint and idempotentHint, so the description adds value by specifying the return format (table with PID, memory, command line). No contradictions.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two concise, front-loaded sentences: first states action, second lists outputs. No wasted words.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple, no-parameter tool with annotations covering safety and an output schema, the description fully covers purpose and return format. No gaps.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

No parameters exist, so baseline is 4. Schema coverage is 100% trivially; description does not need to add parameter info.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states 'List running Python processes on this machine' with a specific verb and resource. It distinguishes from sibling profiling and stack dump tools by focusing on listing processes.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like dump_stacks or analyze_profile. Missing when-not and context for selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

record_profileA
Destructive

Record a sampling profile of a Python process or command.

ParametersJSON Schema
NameRequiredDescriptionDefault
gilNoOnly include traces holding the GIL.
pidNoProcess ID to sample. Use this OR command, not both.
idleNoInclude idle threads.
rateNoSamples per second (default 100).
nativeNoInclude native/C extension frames if supported on this platform.
commandNoCommand to run and sample, e.g. ["python", "script.py"]. Use this OR pid.
durationNoHow many seconds to sample (default 5, recommended 5-60).
subprocessesNoInclude child Python processes.
output_formatNoOne of "speedscope" (JSON), "flamegraph" (SVG), "raw" (text), "chrometrace".speedscope

Output Schema

ParametersJSON Schema
NameRequiredDescription
resultYes

TDQS

A3.8/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The description adds no behavioral context beyond the annotation 'destructiveHint=true'. While the annotation covers destructive nature, the description could elaborate on performance impact or system effects during sampling. It does not contradict annotations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, concise sentence with no fluff. It front-loads the core purpose and uses precise language, earning its place without unnecessary details.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity (9 parameters), complete schema descriptions, and existence of an output schema, the description is adequate. It succinctly defines the tool's purpose, though it could briefly note default behavior (e.g., duration) to tie parameters to usage.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema provides 100% coverage with detailed descriptions for all 9 parameters. The tool description adds no extra meaning beyond the schema, meeting the baseline expected for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'record' and the resource 'sampling profile' with a specific target 'Python process or command', distinguishing it from sibling tools like 'analyze_profile' and 'compare_profiles' which focus on analysis rather than data capture.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description does not explicitly state when to use this tool over alternatives. It implies usage via the action of recording, but lacks guidance on scenarios where this tool is preferred over, say, 'dump_stacks' or 'top_profile'. The schema includes mutual exclusivity notes for parameters, but no tool-level usage guidance.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

top_profileA
Destructive

Run a live py-spy top view for a few seconds and return the summary.

On Windows, py-spy top cannot be captured through a pipe, so this tool falls back to a short record --format raw sample and returns the hottest frames instead of raw top output.

ParametersJSON Schema
NameRequiredDescriptionDefault
gilNoOnly show traces holding the GIL.
pidNoProcess ID. Use this OR command.
idleNoInclude idle threads.
rateNoSamples per second (default 100).
nativeNoInclude native frames if supported.
commandNoCommand to run and monitor. Use this OR pid.
durationNoHow many seconds to run top (default 5, recommended 5-60).
subprocessesNoInclude child processes.

Output Schema

ParametersJSON Schema
NameRequiredDescription
resultYes

TDQS

A3.8/5.0
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

The description adds value by explaining the Windows fallback behavior, but beyond that, it does not elaborate on the implications of the destructiveHint annotation (e.g., resource usage). The annotations already indicate destructiveness, so the added context is moderate.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is two sentences, front-loaded with the core purpose, and contains no superfluous information. Every sentence earns its place.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given that the tool has a complete schema, an output schema, and annotations, the description covers the essential purpose and a behavioral nuance (Windows fallback). It could be slightly more explicit about the non-Windows behavior, but it is largely complete.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so the input schema already describes all parameters. The description does not add any parameter-level details beyond what's in the schema, meeting the baseline expectation.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool runs a live py-spy top view and returns a summary. It distinguishes itself from sibling tools like record_profile by specifying it's a live view, and also notes a platform-specific fallback on Windows.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for quick live monitoring but does not explicitly compare with sibling tools like record_profile. It provides a Windows-specific guideline but lacks general when-to-use or when-not-to-use advice.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

  1. 6 tool updatesv0.2.1
    • First observedanalyze_profile
    • First observedcompare_profiles
    • First observeddump_stacks
    • First observedlist_python_processes
    • First observedrecord_profile
    • First observedtop_profile

TDQS

A4/5.0

Scored across 6 tools

Disambiguation5/5

Each tool has a clearly distinct purpose: listing processes, recording, analyzing, comparing, dumping stacks, and live top view. No two tools overlap, making selection unambiguous for an agent.

Naming Consistency4/5

Most tools follow a verb_noun pattern (e.g., list_python_processes, record_profile). The tool 'top_profile' deviates slightly by using 'top' as a verb, but it is still interpretable. Overall consistent.

Tool Count5/5

With 6 tools, the set covers all essential py-spy operations without being excessive. Each tool earns its place for a focused profiling server.

Completeness5/5

The tools cover the full lifecycle of profiling: process discovery, recording, analysis, comparison, stack inspection, and live monitoring. No major gaps are apparent for the intended use case.

Maintenance

ActivityStale
ResponsivenessNo issues

Related MCP Connectors

Related MCP Servers

  • A
    license
    A
    quality
    D
    maintenance
    MCP server for profiling Java applications via JDK utilities (jcmd, jfr, jps). Enables AI assistants to diagnose performance, analyze threads, and inspect JFR recordings without manual CLI usage.
    26
    46 npm
    10
    MIT
  • F
    license
    B
    quality
    D
    maintenance
    A read-only MCP server for Grafana Pyroscope that lets AI assistants query continuous profiling data, including flamegraphs, hotspot functions, memory allocations, and label discovery, directly from any MCP-compatible client.
    6
    1
    -