Skip to main content
Glama
maxim-saplin

Safe Local Python Executor/Interpreter

by maxim-saplin

安全なローカル Python エグゼキュータ

Hugging FaceのLocalPythonExecutor ( smolagentsフレームワークより)をラップするMCPサーバー(stdioトランスポート)です。これは、LLMによって生成されたPythonコードをローカルで実行する際に基本的な分離性とセキュリティを提供するカスタムPythonランタイムです。DockerやVMは必要ありません。このパッケージは、Claude Desktop、Cursor、その他のMCP対応クライアントなどのLLMアプリ用のツールとして、MCP(Model Context Protocol)経由でPythonエグゼキューターを公開することを可能にします。Claude Desktopの場合、このツールは不足しているコードインタープリター(ChatGPTのプラグインとして既にかなり以前から利用可能)を簡単に追加する方法です。

特徴

  • run_pythonツールを公開する

  • Python eva()lを直接使用するよりも安全に Python コードを実行できます

  • Python venvでuv経由で実行

  • ファイルI/O操作は許可されません

  • 輸入制限リスト

    • コレクション

    • 日時

    • イターツール

    • 数学

    • 列

    • ランダム

    • 再

    • 統計

    • 統計

    • 時間

    • ユニコードデータ

Related MCP server: MCP Code Mode

安全

LLM によって生成されたコードを自分のマシンで実行する際は注意してください。コマンドラインやeval()を使って Python を実行する MCP サーバーは使用しないでください。最も安全な選択肢は VM または Docker コンテナを使用することですが、セットアップに多少の手間がかかり、リソースを消費し、速度も低下します。Python ランタイムを提供するサードパーティのサービスもありますが、登録や API キーなどが必要になります。

LocalPythonExecutor 、ローカルPython環境の直接利用(セットアップが容易)と、DokcerコンテナまたはVM/サードパーティサービスでのリモート実行(安全)のバランスに優れています。Hugginng Faceチームは、コードエージェントで使用されるLLM生成コードを迅速かつ安全に実行するためのオプションの開発に時間を費やしました。このMCPサーバーは、このソリューションを基盤としています。

セキュリティの第一層として、smolagents でのコード実行は標準の Python インタープリターでは実行されません。より安全な LocalPythonExecutor をゼロから再構築しました。

詳細はこちらをご覧ください。

インストールと実行

  1. uvをインストールします (macOS ではbrew install uv実行するか、公式ドキュメントを使用してください)

  2. リポジトリをクローンし、ディレクトリを変更しますcd mcp_safe_local_python_executor

  3. サーバーはコマンドラインuv run mcp_server.pyで起動できます。venv が自動的に作成され、依存関係 (smollagents、mcp) がインストールされます。

Claudeデスクトップの設定

  1. Claude for Desktop がインストールされていることを確認してください ( claude.aiからダウンロード)

  2. Claude for Desktop 構成ファイルを編集します。

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

    • Windows: %APPDATA%\Claude\claude_desktop_config.json

    • または、Claude Desktop -> 設定 -> 開発者 -> 「設定の編集」ボタンをクリックします。

  3. 次の構成を追加します。

{
    "mcpServers": {
        "safe-local-python-executor": {
            "command": "uv",
            "args": [
                "--directory", 
                "/path/to/mcp_local_python_executor/",
                "run",
                "mcp_server.py"
            ]
        }
    }
}
  1. デスクトップ版のClaudeを再起動する

  2. Python 実行ツールが Claude で利用できるようになります (メッセージ入力フィールドにハンマーアイコンが表示されます)

プロンプトの例

設定が完了すると、次のようなプロンプトが使用できるようになります。

  • 「Python を使って 5 の階乗を計算する」

  • 「100までの素数のリストを作成してください」

  • 「この方程式を解いてください(Python を使用):x^2 + 5x + 6 = 0」

発達

リポジトリをクローンします。uv uv使用して venv を作成し、開発依存関係をインストールし、テストを実行します。

uv venv .venv
uv sync --group dev
python -m pytest tests/

Available Tools

1 tool
run_pythonA

Execute Python code in a secure sandbox environment.

This tool allows running simple Python code for calculations and data manipulations.
The execution environment is restricted for security purposes. Make sure you create a single file
that can be executed in one go and it returns a result.

Default allowed imports:
- math
- random 
- datetime
- time
- json
- re
- string
- collections
- itertools
- functools
- operator

Args:
    code: The Python code to execute. Must be valid Python 3 code. The result must be stored in a variable called `result`. E.g.:
    ```python
    import math
    result = math.sqrt(16)
    ```
    
Returns:
    A dictionary with execution results containing:
    - result: The final value or None if no value is returned
    - logs: Any output from print statements
ParametersJSON Schema
NameRequiredDescriptionDefault
codeYes

Output Schema

ParametersJSON Schema
NameRequiredDescription
resultYes

TDQS

A4.3/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes key traits: the secure sandbox environment, restrictions for security, requirement to create a single executable file, and default allowed imports. However, it doesn't cover aspects like execution time limits, memory constraints, or error handling, leaving some behavioral gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured and front-loaded with the core purpose, followed by usage notes, allowed imports, parameter details, and return information. It's appropriately sized for the tool's complexity, but the list of allowed imports and code example add some length that could be streamlined without losing clarity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (code execution with security constraints), no annotations, and an output schema that documents return values, the description is highly complete. It covers purpose, behavioral traits, parameter semantics with examples, and usage context, leaving minimal gaps for an AI agent to understand and invoke the tool correctly.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 0% description coverage, so the description must fully compensate. It adds substantial meaning beyond the schema by detailing the 'code' parameter: it must be valid Python 3 code, require storing results in a variable called 'result', and includes a clear example with syntax and usage. This provides comprehensive semantic context for the single parameter.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Execute Python code in a secure sandbox environment' with specific verbs ('execute', 'run') and resource ('Python code'). It distinguishes the tool's scope by mentioning it's for 'simple Python code for calculations and data manipulations' and operates in a restricted environment, making its function unambiguous even without siblings.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context by specifying it's for 'simple Python code for calculations and data manipulations' and mentions security restrictions, but it lacks explicit guidance on when to use this tool versus alternatives (e.g., other code execution tools or manual methods). No siblings are listed, so differentiation isn't needed, but general usage context is only partially addressed.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

  1. 1 tool update
    • First observedrun_python

TDQS

A4.1/5.0

Scored across 1 tool

Disambiguation5/5

With only one tool, there is no possibility of confusion or overlap between tools. The single tool 'run_python' has a clearly defined and distinct purpose.

Naming Consistency5/5

The naming follows a consistent verb_noun pattern with 'run_python'. Since there's only one tool, consistency is inherently perfect with no deviations to assess.

Tool Count2/5

A single tool is too few for the server's purpose as a 'Safe Local Python Executor/Interpreter'. This suggests a thin surface that may lack essential operations like code validation, environment inspection, or error handling, limiting agent functionality.

Completeness2/5

The tool set is severely incomplete for the domain. While 'run_python' covers execution, there are obvious gaps such as tools for listing allowed imports, checking code safety, managing execution timeouts, or handling errors, which are critical for a secure interpreter.

Maintenance

ActivityInactive
ResponsivenessUnresponsive

Related MCP Connectors

Related MCP Servers