Skip to content

Commit 3a1a862

Browse files
committed
Initial commit: MCP Python Interpreter
0 parents  commit 3a1a862

File tree

9 files changed

+1390
-0
lines changed

9 files changed

+1390
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Python-generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
test_dir/
8+
*.egg-info
9+
.pyirc
10+
11+
# Virtual environments
12+
.venv

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10

LICENSE

Whitespace-only changes.

README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

mcp_python_interpreter/__init__.py

Whitespace-only changes.

mcp_python_interpreter/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Main module for mcp-python-interpreter."""
2+
3+
import sys
4+
import os
5+
import argparse
6+
from pathlib import Path
7+
8+
from mcp_python_interpreter.server import mcp
9+
10+
11+
def main():
12+
"""Run the MCP Python Interpreter server."""
13+
# The actual argument parsing is done in the server module
14+
# to ensure the working directory is set early
15+
mcp.run()
16+
17+
18+
if __name__ == "__main__":
19+
main()

0 commit comments

Comments
 (0)