|
| 1 | +# MCP Python Interpreter |
| 2 | + |
| 3 | +A Model Context Protocol (MCP) server that allows LLMs to interact with Python environments, read and write files, execute Python code, and manage development workflows. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Environment Management**: List and use different Python environments (system and conda) |
| 8 | +- **Code Execution**: Run Python code or scripts in any available environment |
| 9 | +- **Package Management**: List installed packages and install new ones |
| 10 | +- **File Operations**: |
| 11 | + - Read files of any type (text, source code, binary) |
| 12 | + - Write text and binary files |
| 13 | +- **Python Prompts**: Templates for common Python tasks like function creation and debugging |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +You can install the MCP Python Interpreter using pip: |
| 18 | + |
| 19 | +```bash |
| 20 | +pip install mcp-python-interpreter |
| 21 | +``` |
| 22 | + |
| 23 | +Or with uv: |
| 24 | + |
| 25 | +```bash |
| 26 | +uv install mcp-python-interpreter |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage with Claude Desktop |
| 30 | + |
| 31 | +1. Install [Claude Desktop](https://claude.ai/download) |
| 32 | +2. Open Claude Desktop, click on menu, then Settings |
| 33 | +3. Go to Developer tab and click "Edit Config" |
| 34 | +4. Add the following to your `claude_desktop_config.json`: |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "mcpServers": { |
| 39 | + "mcp-python-interpreter": { |
| 40 | + "command": "uvx", |
| 41 | + "args": [ |
| 42 | + "mcp-python-interpreter", |
| 43 | + "--dir", |
| 44 | + "/path/to/your/work/dir", |
| 45 | + "--python-path", |
| 46 | + "/path/to/your/python" |
| 47 | + ] |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +For Windows: |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "mcpServers": { |
| 58 | + "python-interpreter": { |
| 59 | + "command": "uvx", |
| 60 | + "args": [ |
| 61 | + "mcp-python-interpreter", |
| 62 | + "--dir", |
| 63 | + "C:\\path\\to\\your\\working\\directory", |
| 64 | + "--python-path", |
| 65 | + "/path/to/your/python" |
| 66 | + ] |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +5. Restart Claude Desktop |
| 73 | +6. You should now see the MCP tools icon in the chat interface |
| 74 | + |
| 75 | +The `--dir` parameter is **required** and specifies where all files will be saved and executed. This helps maintain security by isolating the MCP server to a specific directory. |
| 76 | + |
| 77 | +### Prerequisites |
| 78 | + |
| 79 | +- Make sure you have `uv` installed. If not, install it using: |
| 80 | + ```bash |
| 81 | + curl -LsSf https://astral.sh/uv/install.sh | sh |
| 82 | + ``` |
| 83 | +- For Windows: |
| 84 | + ```powershell |
| 85 | + powershell -ExecutionPolicy Bypass -Command "iwr -useb https://astral.sh/uv/install.ps1 | iex" |
| 86 | + ``` |
| 87 | + |
| 88 | +## Available Tools |
| 89 | + |
| 90 | +The Python Interpreter provides the following tools: |
| 91 | + |
| 92 | +### Environment and Package Management |
| 93 | +- **list_python_environments**: List all available Python environments (system and conda) |
| 94 | +- **list_installed_packages**: List packages installed in a specific environment |
| 95 | +- **install_package**: Install a Python package in a specific environment |
| 96 | + |
| 97 | +### Code Execution |
| 98 | +- **run_python_code**: Execute Python code in a specific environment |
| 99 | +- **run_python_file**: Execute a Python file in a specific environment |
| 100 | + |
| 101 | +### File Operations |
| 102 | +- **read_file**: Read contents of any file type, with size and safety limits |
| 103 | + - Supports text files with syntax highlighting |
| 104 | + - Displays hex representation for binary files |
| 105 | +- **write_file**: Create or overwrite files with text or binary content |
| 106 | +- **write_python_file**: Create or overwrite a Python file specifically |
| 107 | +- **list_directory**: List Python files in a directory |
| 108 | + |
| 109 | +## Available Resources |
| 110 | + |
| 111 | +- **python://environments**: List all available Python environments |
| 112 | +- **python://packages/{env_name}**: List installed packages for a specific environment |
| 113 | +- **python://file/{file_path}**: Get the content of a Python file |
| 114 | +- **python://directory/{directory_path}**: List all Python files in a directory |
| 115 | + |
| 116 | +## Prompts |
| 117 | + |
| 118 | +- **python_function_template**: Generate a template for a Python function |
| 119 | +- **refactor_python_code**: Help refactor Python code |
| 120 | +- **debug_python_error**: Help debug a Python error |
| 121 | + |
| 122 | +## Example Usage |
| 123 | + |
| 124 | +Here are some examples of what you can ask Claude to do with this MCP server: |
| 125 | + |
| 126 | +- "Show me all available Python environments on my system" |
| 127 | +- "Run this Python code in my conda-base environment: print('Hello, world!')" |
| 128 | +- "Create a new Python file called 'hello.py' with a function that says hello" |
| 129 | +- "Read the contents of my 'data.json' file" |
| 130 | +- "Write a new configuration file with these settings..." |
| 131 | +- "List all packages installed in my system Python environment" |
| 132 | +- "Install the requests package in my system Python environment" |
| 133 | +- "Run data_analysis.py with these arguments: --input=data.csv --output=results.csv" |
| 134 | + |
| 135 | +## File Handling Capabilities |
| 136 | + |
| 137 | +The MCP Python Interpreter now supports comprehensive file operations: |
| 138 | +- Read text and binary files up to 1MB |
| 139 | +- Write text and binary files |
| 140 | +- Syntax highlighting for source code files |
| 141 | +- Hex representation for binary files |
| 142 | +- Strict file path security (only within the working directory) |
| 143 | + |
| 144 | +## Security Considerations |
| 145 | + |
| 146 | +This MCP server has access to your Python environments and file system. Key security features include: |
| 147 | +- Isolated working directory |
| 148 | +- File size limits |
| 149 | +- Prevented writes outside the working directory |
| 150 | +- Explicit overwrite protection |
| 151 | + |
| 152 | +Always be cautious about running code or file operations that you don't fully understand. |
| 153 | + |
| 154 | +## License |
| 155 | + |
| 156 | +MIT |
0 commit comments