py-spy MCP Server
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@py-spy MCP ServerGenerate a flamegraph for the Python process running my Flask app"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
py-spy MCP Server
A Model Context Protocol (MCP) server that exposes Python performance testing tools powered by py-spy.
đ 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 shortpy-spy topsession and return a summary.On Windows,
py-spy topcannot 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-spybinary built from the sibling Rust source (src/pyspy/).Optional native/C extension profiling â enable
--nativewhere 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-mcpUsing uv:
uv pip install pyspy-mcp
# or install as a global tool
uv tool install pyspy-mcpThis 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-mcpThe 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 -vThe 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-spyPublishing 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
ptracepermissions (sudoorcap_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 toolsanalyze_profileARead-onlyIdempotent
Analyze an existing py-spy profile and return the hottest frames.
| Name | Required | Description | Default |
|---|---|---|---|
| top_n | No | Number of top frames to return (default 10). | |
| profile_path | Yes | Path to a speedscope JSON (.json) or raw (.txt) profile. |
Output Schema
| Name | Required | Description |
|---|---|---|
| result | Yes |
TDQS
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.
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.
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.
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.
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.
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_profilesARead-onlyIdempotent
Compare two speedscope profiles and show percentage changes.
| Name | Required | Description | Default |
|---|---|---|---|
| top_n | No | Number of frames to show (default 10). | |
| profile_a | Yes | Path to the first profile. | |
| profile_b | Yes | Path to the second profile. |
Output Schema
| Name | Required | Description |
|---|---|---|
| result | Yes |
TDQS
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.
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.
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.
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.
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.
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_stacksARead-onlyIdempotent
Dump the current Python call stacks of a process.
| Name | Required | Description | Default |
|---|---|---|---|
| pid | Yes | Process ID to inspect. | |
| native | No | Include native frames if supported. | |
| json_output | No | Return structured JSON instead of human-readable text. | |
| locals_level | No | Number of times to pass --locals (0-2). Each level shows more locals. | |
| subprocesses | No | Include child Python processes. |
Output Schema
| Name | Required | Description |
|---|---|---|
| result | Yes |
TDQS
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.
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.
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.
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.
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.
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_processesARead-onlyIdempotent
List running Python processes on this machine.
Returns: A table of PID, memory usage, and command line.
| Name | Required | Description | Default |
|---|---|---|---|
No parameters | |||
Output Schema
| Name | Required | Description |
|---|---|---|
| result | Yes |
TDQS
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.
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.
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.
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.
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.
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_profileADestructive
Record a sampling profile of a Python process or command.
| Name | Required | Description | Default |
|---|---|---|---|
| gil | No | Only include traces holding the GIL. | |
| pid | No | Process ID to sample. Use this OR command, not both. | |
| idle | No | Include idle threads. | |
| rate | No | Samples per second (default 100). | |
| native | No | Include native/C extension frames if supported on this platform. | |
| command | No | Command to run and sample, e.g. ["python", "script.py"]. Use this OR pid. | |
| duration | No | How many seconds to sample (default 5, recommended 5-60). | |
| subprocesses | No | Include child Python processes. | |
| output_format | No | One of "speedscope" (JSON), "flamegraph" (SVG), "raw" (text), "chrometrace". | speedscope |
Output Schema
| Name | Required | Description |
|---|---|---|
| result | Yes |
TDQS
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.
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.
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.
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.
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.
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_profileADestructive
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.
| Name | Required | Description | Default |
|---|---|---|---|
| gil | No | Only show traces holding the GIL. | |
| pid | No | Process ID. Use this OR command. | |
| idle | No | Include idle threads. | |
| rate | No | Samples per second (default 100). | |
| native | No | Include native frames if supported. | |
| command | No | Command to run and monitor. Use this OR pid. | |
| duration | No | How many seconds to run top (default 5, recommended 5-60). | |
| subprocesses | No | Include child processes. |
Output Schema
| Name | Required | Description |
|---|---|---|
| result | Yes |
TDQS
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.
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.
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.
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.
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.
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.
6 tool updates
v0.2.1- First observed
analyze_profile - First observed
compare_profiles - First observed
dump_stacks - First observed
list_python_processes - First observed
record_profile - First observed
top_profile
TDQS
Scored across 6 tools
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.
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.
With 6 tools, the set covers all essential py-spy operations without being excessive. Each tool earns its place for a focused profiling server.
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
Related MCP Connectors
MCP server for progressive tool usage at any scale (see https://klavis.ai)
Repository knowledge graph MCP server for codebase understanding and debugging.
- AgentCatOAuthcom.agentcat
Analytics and debugging for your MCP server â explore usage and sessions, then root-cause errors.
Related MCP Servers
- AlicenseNot gradedqualityCmaintenanceThis is a Model Context Protocol (MCP) server implemented in Go, providing a tool to analyze Go pprof performance profiles.50MIT
- AlicenseAqualityDmaintenanceMCP 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.2646 npm10MIT
- FlicenseBqualityDmaintenanceA 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.61-
- AlicenseAqualityCmaintenanceMCP server that reads Spark profiler files to give accurate Minecraft server tuning advice, parsing binary protobuf directly and diagnosing TPS, MSPT, GC, heap, and call-tree issues.1418 npm4MIT