1
0
shell/docs/claude-code-inception-methods.md
2025-05-31 01:47:48 +09:00

246 lines
6.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Claude CodeでClaude Code的環境を作る方法
Claude Code**で**Claude Code**のような**ことを実現する様々なアプローチをご紹介!
## 🎯 方法1: MCP Server経由でローカルLLMに委譲
### claude-code-mcp を使用
```bash
# Claude Code MCPサーバーのセットアップ
git clone https://github.com/steipete/claude-code-mcp
cd claude-code-mcp
# Claude Codeをローカルで呼び出すMCPサーバーとして動作
npm install
npm start
```
**仕組み:**
- Claude Code → MCP Server → ローカルLLM → 結果を返す
- Claude Codeを全権限バイパス--dangerously-skip-permissionsで実行
- Agent in Agent 構造の実現
## 🎯 方法2: Claude Desktop + Custom MCP Server
### カスタムMCPサーバーでローカルLLM統合
```python
# custom_llm_mcp_server.py
import asyncio
import json
from mcp.server import Server
from mcp.types import Tool, TextContent
import requests
app = Server("local-llm-mcp")
@app.tool("run_local_llm")
async def run_local_llm(prompt: str, model: str = "qwen2.5-coder:14b") -> str:
"""ローカルLLMでコード生成・分析を実行"""
response = requests.post("http://localhost:11434/api/generate", json={
"model": model,
"prompt": prompt,
"stream": False
})
return response.json()["response"]
@app.tool("execute_code")
async def execute_code(code: str, language: str = "python") -> str:
"""生成されたコードを実行"""
# セキュアな実行環境でコード実行
# Docker containerやsandbox環境推奨
pass
if __name__ == "__main__":
asyncio.run(app.run())
```
### Claude Desktop設定
```json
{
"mcpServers": {
"local-llm": {
"command": "python",
"args": ["custom_llm_mcp_server.py"]
}
}
}
```
## 🎯 方法3: VS Code拡張 + MCP統合
### VS Code設定でClaude Code風環境
```json
// settings.json
{
"mcp.servers": {
"claude-code-local": {
"command": ["python", "claude_code_local.py"],
"args": ["--model", "qwen2.5-coder:14b"]
}
}
}
```
VS Codeは両方の構成ローカル/リモート)をサポートしているから、柔軟に設定できるよ〜
## 🎯 方法4: API Gateway パターン
### Claude Code → API Gateway → ローカルLLM
```python
# api_gateway.py
from fastapi import FastAPI
import requests
app = FastAPI()
@app.post("/v1/chat/completions")
async def proxy_to_local_llm(request: dict):
"""OpenAI API互換エンドポイント"""
# Claude Code → この API → Ollama
ollama_response = requests.post(
"http://localhost:11434/api/chat",
json={
"model": "qwen2.5-coder:14b",
"messages": request["messages"]
}
)
# OpenAI API形式で返却
return {
"choices": [{
"message": {"content": ollama_response.json()["message"]["content"]}
}]
}
```
### Claude Code設定
```bash
# 環境変数でローカルAPIを指定
export ANTHROPIC_API_KEY="dummy"
export ANTHROPIC_BASE_URL="http://localhost:8000/v1"
claude code --api-base http://localhost:8000
```
## 🎯 方法5: Docker Compose 統合環境
### docker-compose.yml
```yaml
version: '3.8'
services:
ollama:
image: ollama/ollama:latest
ports:
- "11434:11434"
volumes:
- ollama_data:/root/.ollama
mcp-server:
build: ./mcp-server
ports:
- "3000:3000"
depends_on:
- ollama
environment:
- OLLAMA_URL=http://ollama:11434
claude-desktop:
image: claude-desktop:latest
volumes:
- ./config:/app/config
environment:
- MCP_SERVER_URL=http://mcp-server:3000
volumes:
ollama_data:
```
DockerはMCPサーバーの展開と管理を簡素化し、分離とポータビリティを提供
## 🎯 方法6: 簡易プロキシスクリプト
### claude_to_local.py
```python
#!/usr/bin/env python3
import subprocess
import sys
import json
def claude_code_wrapper():
"""Claude CodeコマンドをインターセプトしてローカルLLMに転送"""
# Claude Codeの引数を取得
args = sys.argv[1:]
prompt = " ".join(args)
# ローカルLLMで処理
result = subprocess.run([
"ollama", "run", "qwen2.5-coder:14b", prompt
], capture_output=True, text=True)
# 結果を整形してClaude Code風に出力
print("🤖 Local Claude Code (Powered by Qwen2.5-Coder)")
print("=" * 50)
print(result.stdout)
# 必要に応じてファイル操作も実行
if "--write" in args:
# ファイル書き込み処理
pass
if __name__ == "__main__":
claude_code_wrapper()
```
### エイリアス設定
```bash
# .bashrc または .zshrc
alias claude-code="python claude_to_local.py"
```
## 🎯 方法7: Aider + Claude Code 統合
### 設定方法
```bash
# Aiderでローカルモデル使用
aider --model ollama/qwen2.5-coder:14b
# Claude Codeから呼び出し
claude code "Run aider with local model to implement feature X"
```
## 💡 どの方法がおすすめ?
### 用途別推奨:
1. **🔧 開発効率重視**: MCP Server方式方法1,2
2. **🏠 統合環境**: Docker Compose方法5
3. **⚡ 簡単設置**: プロキシスクリプト方法6
4. **🎨 カスタマイズ**: API Gateway方法4
## 🚀 実装のコツ
### セキュリティ考慮
- サンドボックス環境でコード実行
- ファイルアクセス権限の制限
- API キーの適切な管理
### パフォーマンス最適化
- ローカルLLMのGPU使用確認
- MCP サーバーのキャッシュ機能
- 並列処理の活用
### デバッグ方法
```bash
# MCP サーバーのログ確認
tail -f ~/.config/claude-desktop/logs/mcp.log
# Ollama の動作確認
ollama ps
curl http://localhost:11434/api/tags
```
## 🎉 まとめ
Claude CodeでClaude Code的な環境を作るには、MCPプロトコルを活用するのが最も効果的ローカルLLMの性能も向上しているので、実用的な環境が構築できるよ〜✨
どの方法から試してみる?アイが一緒に設定をお手伝いするからね!