
Pystub
io.github.KoyoYeagerPublished by GitHub account KoyoYeagerv0.1.1Updated Oct 24, 2026
Auto-detect stubbable packages for Python exe builds and generate minimal stub code
Overview
Analyzes a Python project's import graph to find packages safe to replace with minimal stubs, shrinking PyInstaller, Nuitka or cx_Freeze executables.
- What you can do
- Pystub exposes tools that statically analyze a Python entry point and its import graph. The analyze tool classifies packages as stubbable, nofollow or required and reports submodule hints for indirectly eliminating C-extension packages; graph visualizes the import graph; check inspects one package's usage; generate and generate_submodule produce minimal stub code plus build, backup, restore and verification instructions.
- When to use it
- Use it when packaging a Python application into an executable and the bundle is bloated by heavy dependencies that are imported but never actually called on the runtime path. It is aimed at build-time size reduction, not at runtime dependency management.
- Requirements
- A local Python environment with the mcp-pystub package installed via pip (mcp>=1.0.0 is the only declared dependency; the analysis engine uses the standard library). It runs as a local stdio process, so it needs access to the project's entry point and site-packages path. No accounts, API keys or environment variables are declared.
Installation
In SourceWeft
- Open Pystub in the dashboard and add it to a workspace.
- Enable the server for the chats that should use its tools.
Desktop only via STDIO. STDIO servers start a local process, so they need the SourceWeft desktop host.
Other MCP clients
Follow the launch instructions in the repository.
README
mcp-pystub
Python exe ビルド(PyInstaller / Nuitka / cx_Freeze)時にスタブ置換可能なパッケージを自動検出し、最小スタブコードを生成する MCP サーバー。
An MCP server that auto-detects stubbable packages for Python exe builds (PyInstaller / Nuitka / cx_Freeze) and generates minimal stub code to reduce executable size.
背景 / Background
Python アプリを exe 化すると、依存ライブラリがモジュールレベルで import する重量パッケージが丸ごと同梱され、exe サイズが膨張する。実際のコードパスで使わないパッケージは最小ダミー(スタブ)で置換すれば大幅なサイズ削減が可能。
When building Python apps into executables, heavy packages imported at module level by dependencies get bundled entirely, bloating the exe size. Replacing unused packages with minimal stubs can significantly reduce size.
実証データ / Verified Results
asammdf プロジェクト — PyInstaller exe ビルド(E2E 動作確認済み):
asammdf 変換ツール(analyze 結果):
手動でスタブ化していた 3 パッケージ(pandas, canmatrix, asammdf.gui)を全て自動検出
機能 / Features
判定結果 / Verdicts
できること / What It Can Do
- ライブラリ非固定の汎用検出: AST 構造のみで判定。ハードコードなし
- 関数レベルの使用追跡:
mdf.get()は使うがmdf.to_dataframe()は使わない → pandas は stubbable - Call と参照の区別:
isinstance(x, pd.DataFrame)は stub-safe、pd.DataFrame(data)は実使用 - クラス継承の検出:
class User(BaseModel)→ pydantic は required - module-level 呼び出し検出: import 時に実行されるコードを追跡
- try/except 保護の自動検出: 保護された import は
nofollowと判定 - スタブコード自動生成: 参照シンボルのみの最小スタブ + ビルド手順(バックアップ・復元・検証)
- C 拡張の自動検出: .pyd / .so を含むパッケージは直接スタブ化不可と判定
- C 拡張の間接排除 (v0.2 new): C拡張パッケージを import しているサブモジュールが未使用なら、そのサブモジュールをスタブ化して C拡張を排除可能。PySide6 (523 MB) → asammdf.gui スタブで exe 40% 削減を実証
- 復元安全設計 (v0.2 new): バックアップ + バージョン固定 pip + 検証コマンドの 3 重安全策
- PyInstaller フック情報: 無効化が必要なフックファイルを通知
できないこと・制限事項 / Limitations
- C 拡張パッケージ(.pyd / .so)は直接スタブ化すると C 拡張が欠落するため required 判定。ただし
generate_submoduleで間接排除が可能(v0.2 で対応) - 動的 import(
importlib.import_module(変数))は静的解析で追跡不可(warnings で通知) - PyInstaller カスタムフック: スタブと衝突する場合がありフックの手動無効化が必要
- 遅延初期化パターン:
__init__で直接呼ばず後のメソッドで使うパッケージは検出精度が下がる(安全側で required に判定) - pip 名と import 名の不一致:
python-dateutil→dateutil等のマッピングは未対応 - ランタイムの条件分岐:
if sys.version < (3,11)内の import は静的解析で判定不可
安全性の設計方針 / Safety Policy
「stubbable と判定したが実は必要だった」は絶対に起こさない設計。判定に迷う場合は required(安全側)に倒す。逆方向の誤判定(本当は stubbable なのに required)は許容する。
解析アルゴリズム / Analysis Algorithm
- Import 抽出:
ast.parse()で各ファイルの import 文を解析(module-level / function-level / try-except 保護を区別) - Import グラフ構築: エントリーポイントから BFS で依存関係を再帰解決(stdlib / third_party / local を自動分類)
- 使用分析: gateway 関数(依存ライブラリ内でパッケージを呼び出す関数)を特定し、プロジェクトコードがそれを呼んでいるか追跡。名前参照のみ(isinstance, 型アノテーション)は stub-safe として除外
- Module-level 検出: import 時にパッケージの関数が呼ばれる場合は required に格上げ
- サブモジュール間接排除 (v0.2): C拡張パッケージを import しているサブモジュールを特定し、プロジェクトが直接 import していない + re-export シンボルを呼んでいない場合にスタブ化ヒントを出力
インストール / Installation
依存パッケージ / Dependencies
mcp>=1.0.0- Model Context Protocol SDK- 解析エンジンは Python 標準ライブラリのみ使用(ast, importlib, pathlib)
使い方 / Usage
MCP サーバーとして起動 / Run as MCP server
Claude Desktop / Claude Code 設定
ツール使用例 / Tool Examples
analyze
generate
generate_submodule (v0.2 new)
パフォーマンス / Performance
テスト / Testing
テスト実績
ライセンス / License
MIT
Source: README.md at commit e5b0e4e
Tools
0Version history
1- v0.1.1LatestSep 16, 2026
Score
50% of the applicable checks met
How complete this listing is, computed from the registry entry, the endpoint and public sources. Not a rating of the server and not a statement that it is safe or working today: reachability is a dated check, and no installs or reviews go into the score.
- Reached in the last 30 days25 pts
Keep the endpoint answering the MCP handshake; SourceWeft probes it every 30 days.
- Auth declared10 pts
Declare the credential headers in the registry entry, or keep the endpoint answering the probe so its auth can be classified.
- Tools listed15 pts
Answer tools/list without a credential, or ship a package that starts in a sandbox without one.
- DescriptionRequired15 pts
- Logo10 pts
- Documentation15 pts
- License10 pts