A Model Context Protocol (MCP) server for Python debugging using pdb
Project description
PDB MCP Server
A Model Context Protocol (MCP) server for Python debugging using pdb. This allows you to debug Python scripts through MCP tools.
Features
- Start Debug Session: Launch a Python script in debug mode using pdb
- Start Pytest Debug Session: Run pytest with automatic pdb on test failures
- Execute PDB Commands: Send commands to control the debugging session
- Auto-detect Python Interpreter: Automatically uses virtual environment Python
- Modular Architecture: Clean separation of concerns with session and server modules
Installation
Install from PyPI
pip install pdb-mcp
Install with uv (Recommended) ⚡️
uvx will automatically install the package and run the command.
uvx pdb-mcp
Development Installation
pip install -e .
Usage
As MCP Server
Run the server:
pdb-mcp
The server exposes three tools:
1. start_debug
Start a debugging session for a Python script.
Parameters:
script_path(required): Path to the Python script to debugpython_path(required): Path to Python interpreter for the project being debuggedworking_directory(required): Directory to run the command in (should be the project directory)args(optional): Command-line arguments to pass to the scripttimeout(optional): Time to wait without new output before returning in seconds (default: 5.0). As long as there's new output, will keep waiting.env(optional): Environment variables to set for the debugging session (e.g., {"DEBUG": "1"})
Example:
{
"script_path": "test_script.py",
"python_path": "/path/to/python",
"working_directory": "/path/to/project",
"env": {"DEBUG": "1"}
}
2. start_pytest_debug
Start a pytest debugging session that automatically drops into pdb on test failures.
Parameters:
args(required): Arguments to pass after 'pytest --pdb'. Can include test paths, filters, and pytest options (e.g., "tests/test_example.py -v", "-k test_login", "-x test_auth.py")python_path(required): Path to Python interpreter for the project being debuggedworking_directory(required): Directory to run the command in (should be the project directory)timeout(optional): Time to wait without new output before returning in seconds (default: 10.0). As long as there's new output, will keep waiting.env(optional): Environment variables to set for the debugging session (e.g., {"DEBUG": "1"})
Example:
{
"args": "tests/test_example.py -v",
"python_path": "/path/to/python",
"working_directory": "/path/to/project",
"env": {"DEBUG": "1"}
}
3. execute_pdb_command
Execute a pdb command in the active debugging session.
Parameters:
command(required): The pdb command to executetimeout(optional): Time to wait without new output before returning in seconds (default: 5.0)
Available Commands:
n(next): Execute next line (don't step into functions)s(step): Execute next line (step into functions)b <line>: Set breakpoint at line (e.g.,b 10)b <file>:<line>: Set breakpoint in another file (e.g.,b utils.py:5)l(list): Show code around current linep <var>: Print variable value (e.g.,p user_name)c(continue): Continue execution until next breakpointq(quit): Quit debugging sessionh(help): Show help for all commandsr(return): Execute until current function returnsw(where): Show call stackcl <breakpoint>: Clear breakpointa(args): Print arguments of current function
Example:
{
"command": "n"
}
Testing
Basic Script Debugging
A sample test script is provided: test_script.py
Run the automated test:
uv run python test_mcp_client.py
Example debugging workflow:
- Start debug:
start_debugwithscript_path = "test_script.py" - Step through code:
execute_pdb_commandwithcommand = "n" - View code:
execute_pdb_commandwithcommand = "l" - Print variable:
execute_pdb_commandwithcommand = "p num" - Continue:
execute_pdb_commandwithcommand = "c" - Quit:
execute_pdb_commandwithcommand = "q"
Pytest Debugging
A sample test file is provided: test_example.py
Run the pytest debugging test:
uv run python test_pytest_client.py
The test will:
- Run pytest with
-x --pdbflags - Stop at the first failing test
- Drop into pdb at the assertion failure
- Allow you to inspect variables and debug
Configuration
To use with Claude Desktop or other MCP clients, add to your configuration file (e.g., claude_desktop_config.json):
Using uvx (Recommended)
{
"mcpServers": {
"pdb-debugger": {
"command": "uvx",
"args": ["--from", "pdb-mcp", "pdb-mcp"]
}
}
}
Using Local Development Version
{
"mcpServers": {
"pdb-debugger": {
"command": "uv",
"args": [
"--directory",
"/path/to/pdb-mcp",
"run",
"pdb-mcp"
]
}
}
}
Replace /path/to/pdb-mcp with the actual path to this project.
Architecture
Project Structure
src/pdb_mcp/
├── __init__.py # Entry point and main function
├── session.py # PdbSession class for managing pdb processes
└── server.py # MCP server and tool definitions
Session Management
The PdbSession class maintains a persistent pdb process:
- Uses
subprocess.Popento launch pdb with the target script - Communicates via stdin/stdout pipes
- Background thread reads output asynchronously
- Maintains debugging session state between command executions
- Auto-detects Python interpreter from virtual environment
Python Interpreter Configuration
The python_path parameter specifies the Python interpreter for the client's project (not the server's environment):
- Required parameter: Agents should provide the project's Python interpreter path
- Typical values:
.venv/bin/pythonorvenv/bin/pythonfor virtual environments/usr/bin/python3for system Python- Custom paths for conda or other environments
- Fallback: If not provided or empty, falls back to
"python"command (uses system PATH)
Note: The server does NOT use its own Python (sys.executable) because the MCP server may run in a different environment than the client's project being debugged.
Working Directory Configuration
Both start_debug and start_pytest_debug require a working_directory parameter:
- Required parameter: Should be the project's root directory or the directory containing the script
- Purpose: Ensures the script runs in the correct directory context
- Use cases:
- Scripts that use relative file paths
- Projects with specific directory structure requirements
- Loading configuration files from project root
- Agent integration: Agents (like Claude) typically know the project directory and should provide it
Example:
{
"script_path": "src/main.py",
"working_directory": "/home/user/myproject"
}
This is equivalent to running:
cd /home/user/myproject && python -m pdb src/main.py
Smart Output Waiting
The server uses an intelligent output waiting mechanism:
- Single timeout parameter: Time to wait without new output before returning
- No total time limit: As long as there's new output, the server keeps waiting
- Adaptive: Fast commands return quickly, slow commands get as much time as they need
- User configurable: All tools accept a
timeoutparameter
How it works:
- Server continuously checks for output every 0.1 seconds
- Each time new output arrives, the idle timer resets
- When there's no new output for
timeoutseconds, it returns - No matter how long the script runs, if it keeps producing output, the server waits
Default timeout values:
start_debug: 5.0 seconds (pdb usually starts quickly)start_pytest_debug: 10.0 seconds (tests may produce output slowly)execute_pdb_command: 5.0 seconds (most commands are quick)
Example: A script that runs for 10 seconds but prints every 0.5 seconds will work fine with timeout=3.0, because the idle time never exceeds 3 seconds.
License
See LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pdb_mcp-0.0.2.tar.gz.
File metadata
- Download URL: pdb_mcp-0.0.2.tar.gz
- Upload date:
- Size: 54.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
053abf93cfc38b616f510782f5350eaf235b5b236278b5d353aecf90dccf3fb0
|
|
| MD5 |
1c479608d2f927972b7f3376840cca2d
|
|
| BLAKE2b-256 |
a47f533fe4101d2ecb119a8894f86b6fed8aee19d7cc0f64df95537c4b6c2b3c
|
Provenance
The following attestation bundles were made for pdb_mcp-0.0.2.tar.gz:
Publisher:
release.yml on zc277584121/pdb-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pdb_mcp-0.0.2.tar.gz -
Subject digest:
053abf93cfc38b616f510782f5350eaf235b5b236278b5d353aecf90dccf3fb0 - Sigstore transparency entry: 930132460
- Sigstore integration time:
-
Permalink:
zc277584121/pdb-mcp@ab87f0981faaf69ff24c41b77852dac52da08853 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/zc277584121
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ab87f0981faaf69ff24c41b77852dac52da08853 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pdb_mcp-0.0.2-py3-none-any.whl.
File metadata
- Download URL: pdb_mcp-0.0.2-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a232c6dac9408eef622bdad7aaadece56d4343c05cdfdfc0a3c6b15f3713327
|
|
| MD5 |
207b611a3e8b3a17644de2d149dd671e
|
|
| BLAKE2b-256 |
861fe0c81aad4d876a98bb60a549682294dbeefd8c42c08f6f6b927dd22aada5
|
Provenance
The following attestation bundles were made for pdb_mcp-0.0.2-py3-none-any.whl:
Publisher:
release.yml on zc277584121/pdb-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pdb_mcp-0.0.2-py3-none-any.whl -
Subject digest:
0a232c6dac9408eef622bdad7aaadece56d4343c05cdfdfc0a3c6b15f3713327 - Sigstore transparency entry: 930132464
- Sigstore integration time:
-
Permalink:
zc277584121/pdb-mcp@ab87f0981faaf69ff24c41b77852dac52da08853 -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/zc277584121
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ab87f0981faaf69ff24c41b77852dac52da08853 -
Trigger Event:
push
-
Statement type: