The Debug MCP Server provides support for Python debugging through the debugpy library. This document explains how to use the Python debugging capabilities.
Before using the Python debugging features, ensure you have:
- Python 3.7 or higher installed
- The debugpy package installed:
pip install debugpy
First, create a Python debug session:
use_mcp_tool(
tool_name="create_debug_session",
arguments={
"language": "python",
"name": "My Python Debug Session"
}
)
This returns a session ID that you'll use for all subsequent debugging commands.
Set breakpoints in your code before starting execution:
use_mcp_tool(
tool_name="set_breakpoint",
arguments={
"sessionId": "your-session-id",
"file": "/path/to/your/script.py",
"line": 10
}
)
You can also set conditional breakpoints:
use_mcp_tool(
tool_name="set_breakpoint",
arguments={
"sessionId": "your-session-id",
"file": "/path/to/your/script.py",
"line": 15,
"condition": "x > 5"
}
)
Start debugging your Python script:
use_mcp_tool(
tool_name="start_debugging",
arguments={
"sessionId": "your-session-id",
"scriptPath": "/path/to/your/script.py",
"args": ["--optional", "arguments"]
}
)
When execution pauses at a breakpoint, you can:
use_mcp_tool(
tool_name="step_over",
arguments={
"sessionId": "your-session-id"
}
)
use_mcp_tool(
tool_name="step_into",
arguments={
"sessionId": "your-session-id"
}
)
use_mcp_tool(
tool_name="step_out",
arguments={
"sessionId": "your-session-id"
}
)
use_mcp_tool(
tool_name="continue_execution",
arguments={
"sessionId": "your-session-id"
}
)
use_mcp_tool(
tool_name="pause_execution",
arguments={
"sessionId": "your-session-id"
}
)
When paused, you can examine the program's state using the get_stack_trace -> get_scopes -> get_variables sequence. Each step returns numeric handles that feed into the next:
use_mcp_tool(
tool_name="get_stack_trace",
arguments={
"sessionId": "your-session-id"
}
)
This returns stack frames, each with a numeric id (the frame ID).
Use the id from the top stack frame:
use_mcp_tool(
tool_name="get_scopes",
arguments={
"sessionId": "your-session-id",
"frameId": 3
}
)
This returns scopes (e.g., "Locals", "Globals"), each with a numeric variablesReference.
Use the variablesReference from a scope (not the frame ID):
use_mcp_tool(
tool_name="get_variables",
arguments={
"sessionId": "your-session-id",
"scope": 5
}
)
The scope parameter is the numeric variablesReference from get_scopes.
For convenience, get_local_variables performs the full stack->scopes->variables traversal in a single call:
use_mcp_tool(
tool_name="get_local_variables",
arguments={
"sessionId": "your-session-id"
}
)
use_mcp_tool(
tool_name="evaluate_expression",
arguments={
"sessionId": "your-session-id",
"expression": "x + y * 2"
}
)
use_mcp_tool(
tool_name="get_stack_trace",
arguments={
"sessionId": "your-session-id"
}
)
use_mcp_tool(
tool_name="get_source_context",
arguments={
"sessionId": "your-session-id",
"file": "/path/to/your/script.py",
"line": 15,
"linesContext": 5
}
)
When finished debugging, close the session:
use_mcp_tool(
tool_name="close_debug_session",
arguments={
"sessionId": "your-session-id"
}
)
- Always check if breakpoints are verified (some lines cannot have breakpoints, like blank lines)
- If the debugger doesn't stop at a breakpoint, ensure the file path is correct and absolute
- Use source context to see code around your current position
- The stack trace shows the call hierarchy that led to the current position
- Expressions are evaluated against the current paused debug context (current frame when available), with the evaluation context defaulting to
'variables'