Python REPL MCP server

Provides an interactive Python REPL environment for executing code within conversations, maintaining separate state for each session and supporting both expressions and statements.
Back to servers
Setup instructions
Provider
Alex Velikanov
Release date
Dec 26, 2024
Language
Python
Package
Stats
1.1K downloads
52 stars

This MCP server provides an interactive Python REPL environment, allowing you to execute Python code in persistent sessions directly from Claude. The server maintains separate state for each session and captures outputs from your code execution.

Installation

Prerequisites

The server requires Python and the UV package manager to be installed on your system.

Setup for Claude Desktop

To use the Python MCP server with Claude Desktop, you need to add configuration to your Claude desktop settings file. The location of this file depends on your operating system:

  • MacOS: ~/Library/Application\ Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%/Claude/claude_desktop_config.json

Configuration Options

For Published Server

If you're using the published version of the server:

"mcpServers": {
  "python_local": {
    "command": "uvx",
    "args": [
      "python_local"
    ]
  }
}

For Development/Unpublished Server

If you're using a development or unpublished version:

"mcpServers": {
  "python_local": {
    "command": "uv",
    "args": [
      "--directory",
      "/path/to/python_local",
      "run",
      "python_local"
    ]
  }
}

Make sure to replace /path/to/python_local with the actual path to your python_local directory.

Usage

Running Python Code

You can execute Python code by using the python_repl tool. This tool requires two parameters:

  • code: The Python code you want to execute
  • session_id: An identifier for your session

The server maintains separate state for each session, allowing you to run multiple independent Python environments.

Accessing Session History

The server provides access to your REPL session history through a custom URI scheme:

  • You can view your session history with the repl:// URI
  • History is provided as plain text
  • The history includes both your input code and the corresponding output

Example Usage

To execute a simple Python expression:

# Calculate a simple expression
result = 10 + 20
print(result)

To maintain state across multiple executions (in the same session):

# First execution
x = 5
y = 10
# Second execution (same session_id)
print(x + y)  # Will output 15

To use Python libraries:

import math
import random

# Generate a random number and apply a math function
num = random.randint(1, 100)
print(f"Random number: {num}")
print(f"Square root: {math.sqrt(num)}")

Working with Multiple Sessions

You can maintain multiple independent Python environments by using different session_id values:

  • Each session maintains its own variables and state
  • Variables defined in one session are not available in another
  • Output history is tracked separately for each session

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "python_local" '{"command":"uvx","args":["python_local"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "python_local": {
            "command": "uvx",
            "args": [
                "python_local"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "python_local": {
            "command": "uvx",
            "args": [
                "python_local"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later