
ローカルLLMによるセキュリティ分析、Ghidraバイナリ分析、C/C++脆弱性スキャン、そしてMCPツール統合による自動リバースエンジニアリングとレポート生成のためのPythonライブラリ。
TrustedSec LLM ライブラリは、ツールの使用をサポートするローカルLLMと対話するためのPythonライブラリです。ローカルLLMエンドポイントとMCP(Model Context Protocol)統合を活用することで、従来はフロンティアモデルでのみ可能だった大規模なワークフローを実行できます。
# Clone or copy the repository
git clone https://github.com/trustedsec/ts_llmlib.git
cd ts_llmlib
# Install with pip
pip install -e .
ts_llmlib/ディレクトリをプロジェクトにコピーします。
cp -rf ts_llmlib /path/to/your/project/
from ts_llmlib import ChatSession
# Initialize with defaults (connects to http://localhost:1234/v1/chat/completions)
chat = ChatSession()
# Run a prompt
response = chat.run_prompt("What files are in the current directory?")
print(response['content'])
from ts_llmlib import ChatSession
# Configure with custom settings
chat = ChatSession(
system_prompt="You are a helpful assistant that uses file tools.",
tool_list=[], # Empty = use default file tools
mcp_servers={
"default": "http://localhost:3000/mcp"
},
llm_endpoint_url="http://localhost:1234/v1/chat/completions",
model_name="qwen3-coder-next",
timeout=60,
max_runtime=300
)
response = chat.run_prompt("Write 'hello' to /tmp/greeting.txt")
print(response['content'])
from ts_llmlib import ChatSession
chat = ChatSession()
history = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": "The answer is 4."}
]
response = chat.run_prompt("Can you write that to a file?", history=history)
| ツール | パラメータ | 説明 |
|---|---|---|
read_local_file | path: str | ローカルファイルの内容を読み取ります |
write_local_file | path: str, content: str | ローカルファイルに内容を書き込みます |
list_directory | path: str | パス内のファイルとディレクトリを一覧表示します |
MCP(Model Context Protocol)は、外部ツールやサービスとの統合を可能にします。MCPサーバーが設定されている場合、ts_llmlibは次のことを行います:
from ts_llmlib import ChatSession
chat = ChatSession(
mcp_servers={
"ghidraSvr": "http://localhost:8081/sse"
},
llm_endpoint_url="http://localhost:1234/v1/chat/completions",
model_name="qwen3-coder-next"
)
# The chat session will automatically fetch and integrate Ghidra tools
# such as list_methods, decompile_function, get_xrefs_to, etc.
ts_llmlibはRPCスタイルとSSE(Server-Sent Events)エンドポイントの両方をサポートします:
http://localhost:3000/mcphttp://localhost:3000/sse(RPCコール時に自動的に/mcpに変換)ChatSession(
system_prompt: str | None = None,
tool_list: list | None = None,
mcp_servers: dict[str, str] | None = None,
llm_endpoint_url: str = "http://localhost:1234/v1/chat/completions",
model_name: str = "default",
timeout: int = 60,
max_runtime: int = 300
)
パラメータ:
system_prompt (str | None): カスタムシステムプロンプト。デフォルトは最小限のアシスタントプロンプトです。tool_list (list | None): カスタムツール定義のリスト。空リストは組み込みツールを使用します。mcp_servers (dict[str, str] | None): サーバー名をURLにマッピングする辞書。llm_endpoint_url (str): LLM APIエンドポイントのURL。model_name (str): LLMエンドポイントのモデル識別子。timeout (int): HTTPリクエストのタイムアウト(秒)。max_runtime (int): プロンプトの最大実行時間(秒)。response = chat.run_prompt(
user_prompt: str,
conversation_history: list[dict] | None = None,
disable_tools: list[str] | None = None,
max_runtime: int | None = None
) -> dict
パラメータ:
user_prompt (str): ユーザーのメッセージまたは質問。conversation_history (list[dict] | None): オプションの会話履歴(role/contentペアのリスト)。disable_tools (list[str] | None): この呼び出しで無効にするツール名のリスト。max_runtime (int | None): この特定の呼び出しのデフォルトの最大実行時間を上書きします。戻り値:
{
"content": str, # LLM response text
"tool_calls": list, # List of tool calls made (if any)
"usage": dict | None, # Token usage if available from the LLM
"error": str | None # Error message if failed
}
ToolRegistryはチャットセッションで利用可能なすべてのツールを管理します:
tool_listパラメータでユーザー定義されたツールデフォルトのパスを上書きするには、次の変数を設定できます。これらはChatSessionでチェックされます。
TS_LLM_MODEL=qwen3-coder-next TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions
# Example usage
export TS_LLM_MODEL=qwen3-coder-next
export TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions
ts_llmlib-redclippy
# OR
TS_LLM_MODEL=qwen3-coder-next TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions ts_llmlib-redclippy
セキュリティ脆弱性についてC/C++ソースファイルを解析します:
ts_llmlib-cpp-analyze <source_folder> <output_folder>
Ghidra統合によるリバースエンジニアリングバイナリ解析:
# Basic analysis
ts_llmlib-ghidra-analyze <output_folder>
# Rename-only mode (first pass)
ts_llmlib-ghidra-analyze --rename_only <output_folder>
# Process only previously unnamed functions
ts_llmlib-ghidra-analyze --process_unnamed_only <output_folder>
# Grouped analysis for call relationship grouping
ts_llmlib-ghidra-analyze --grouped <output_folder>
--rename_onlyを使用しない場合、実行後は以下を実行して構造をクリーンアップできます:
ts_llmlib-ghidra-cleanup <input_folder> <output_folder>
JSONレビューファイルからフォーマットされた脆弱性レポートを生成します:
ts_llmlib-ghidra-report <review_folder>
RedClippy Qtベースのチャットインターフェースを実行します(この例ではpyside6が必要です):
ts_llmlib-redclippy
ts_llmlibはOpenAI互換のAPIエンドポイントに接続します。一般的なローカルLLMサーバー:
| サーバー | デフォルトURL |
|---|---|
| Ollama | http://localhost:11434/v1/chat/completions |
| LM Studio | http://localhost:1234/v1/chat/completions |
| vLLM | http://localhost:8000/v1/chat/completions |
2つのタイムアウト設定が実行を制御します:
timeout): 単一のAPIリクエストの最大時間max_runtime): プロンプト処理に許可される総経過時間(ツール呼び出しを含む)どちらかの制限を超えた場合、レスポンスにエラーメッセージが含まれます。
すべてのエラーはレスポンス辞書で返されます:
response = chat.run_prompt("Some prompt")
if response.get('error'):
print(f"Error: {response['error']}")
else:
print(response['content'])
max_runtime制限を超えたts_llmlib/
├── __init__.py # パッケージの初期化、ChatSessionをエクスポート
├── client.py # LLMエンドポイントへのHTTPリクエスト用LLMClient
├── chat.py # ChatSessionクラス(メインAPI)
├── mcp.py # Model Context Protocol統合用MCPClient
├── tools.py # ツール管理用ToolRegistry
├── HOW_TO_TS_LLMLIB.md # 元のドキュメント
└── examples/ # サンプルスクリプト
├── c_cpp_analyze.py # C/C++脆弱性解析スクリプト
├── redclippy.py # QtベースのGUIチャットアプリケーション
├── ghidra_analyze.py # Ghidraバイナリ解析(MCP統合用)
├── ghidra_vuln_report.py # 脆弱性レポート生成器
├── ghidra_cleanup.py # 出力ファイル再整理ユーティリティ
└── example_ts_llmlib.py # ライブラリ使用例のサンプルスクリプト
pyproject.toml # モダンなPythonパッケージ設定(スクリプトはここで定義)
LICENSE.txt # BSD-3-Clause License
README.md # このファイル
BSD-3-Clauseライセンス - 詳細はLICENSE.txtファイルを参照してください。
貢献を歓迎します!遠慮なくプルリクエストを送信してください。
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)