This commit is contained in:
2025-07-05 13:48:50 +09:00
parent 00c9af330b
commit 9758126225
26 changed files with 3134 additions and 0 deletions

343
mcp/setup-mcp.sh Executable file
View File

@ -0,0 +1,343 @@
#!/bin/bash
# MCP (Model Context Protocol) セットアップスクリプト
set -e
# カラー定義
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
# 設定ディレクトリ
MCP_DIR="$HOME/.mcp"
CONFIG_FILE="$HOME/.claude/claude_desktop_config.json"
echo -e "${BLUE}MCP (Model Context Protocol) セットアップを開始します${NC}"
# MCPディレクトリの作成
mkdir -p "$MCP_DIR"
# 必要なパッケージのインストール
install_mcp_servers() {
echo -e "${GREEN}MCPサーバーをインストール中...${NC}"
# 基本的なMCPサーバー
npm install -g @modelcontextprotocol/server-filesystem
npm install -g @modelcontextprotocol/server-github
npm install -g @modelcontextprotocol/server-postgres
npm install -g @modelcontextprotocol/server-slack
# 開発系
npm install -g @modelcontextprotocol/server-git
npm install -g @modelcontextprotocol/server-docker
echo -e "${GREEN}✓ MCPサーバーのインストール完了${NC}"
}
# 設定ファイルの作成
create_config() {
echo -e "${GREEN}設定ファイルを作成中...${NC}"
# 既存の設定をバックアップ
if [[ -f "$CONFIG_FILE" ]]; then
cp "$CONFIG_FILE" "$CONFIG_FILE.backup.$(date +%Y%m%d-%H%M%S)"
echo -e "${YELLOW}既存の設定をバックアップしました${NC}"
fi
# 新しい設定を作成
cat > "$CONFIG_FILE" << EOF
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem"],
"env": {
"FILESYSTEM_ROOT": "$HOME/projects"
}
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "your-github-token-here"
}
},
"git": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-git"],
"env": {
"GIT_ROOT": "$HOME/projects"
}
},
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://localhost/postgres"
}
},
"docker": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-docker"]
}
}
}
EOF
echo -e "${GREEN}✓ 設定ファイルを作成しました: $CONFIG_FILE${NC}"
}
# カスタムMCPサーバーの作成
create_custom_server() {
local server_name="$1"
local server_dir="$MCP_DIR/servers/$server_name"
mkdir -p "$server_dir"
# Python版のサンプルMCPサーバー
cat > "$server_dir/server.py" << 'EOF'
#!/usr/bin/env python3
import asyncio
import json
import sys
from typing import Dict, Any, List
from mcp.server import Server
from mcp.types import Tool, TextContent, ImageContent
app = Server("custom-tools")
@app.tool()
async def search_codebase(query: str, file_pattern: str = "*.py") -> TextContent:
"""コードベース全体を検索"""
import subprocess
try:
result = subprocess.run(
["rg", query, "--type", "py", "--json"],
capture_output=True,
text=True
)
return TextContent(text=result.stdout)
except Exception as e:
return TextContent(text=f"エラー: {str(e)}")
@app.tool()
async def get_system_info() -> TextContent:
"""システム情報を取得"""
import platform
import psutil
info = {
"platform": platform.system(),
"machine": platform.machine(),
"processor": platform.processor(),
"memory": f"{psutil.virtual_memory().total // (1024**3)} GB",
"disk": f"{psutil.disk_usage('/').total // (1024**3)} GB"
}
return TextContent(text=json.dumps(info, indent=2))
@app.tool()
async def run_shell_command(command: str) -> TextContent:
"""シェルコマンドを実行(注意: セキュリティリスクあり)"""
import subprocess
# セキュリティ: 危険なコマンドをブロック
dangerous_commands = ["rm -rf", "dd if=", "mkfs", "format"]
if any(cmd in command for cmd in dangerous_commands):
return TextContent(text="エラー: 危険なコマンドです")
try:
result = subprocess.run(
command,
shell=True,
capture_output=True,
text=True,
timeout=30
)
return TextContent(text=f"stdout: {result.stdout}\nstderr: {result.stderr}")
except subprocess.TimeoutExpired:
return TextContent(text="エラー: コマンドがタイムアウトしました")
except Exception as e:
return TextContent(text=f"エラー: {str(e)}")
if __name__ == "__main__":
asyncio.run(app.run())
EOF
# 実行権限を付与
chmod +x "$server_dir/server.py"
# 設定ファイルに追加
echo -e "${GREEN}カスタムMCPサーバーを作成しました: $server_dir${NC}"
echo -e "${YELLOW}設定ファイルに以下を追加してください:${NC}"
cat << EOF
"$server_name": {
"command": "python3",
"args": ["$server_dir/server.py"]
}
EOF
}
# Arch Linux固有の設定
setup_arch_specific() {
echo -e "${GREEN}Arch Linux固有の設定を適用中...${NC}"
# pacmanツール用のMCPサーバー
cat > "$MCP_DIR/servers/pacman/server.py" << 'EOF'
#!/usr/bin/env python3
import asyncio
import subprocess
from mcp.server import Server
from mcp.types import Tool, TextContent
app = Server("pacman-tools")
@app.tool()
async def search_packages(query: str) -> TextContent:
"""パッケージを検索"""
result = subprocess.run(
["pacman", "-Ss", query],
capture_output=True,
text=True
)
return TextContent(text=result.stdout)
@app.tool()
async def get_package_info(package: str) -> TextContent:
"""パッケージ情報を取得"""
result = subprocess.run(
["pacman", "-Si", package],
capture_output=True,
text=True
)
return TextContent(text=result.stdout)
@app.tool()
async def list_installed_packages() -> TextContent:
"""インストール済みパッケージのリスト"""
result = subprocess.run(
["pacman", "-Q"],
capture_output=True,
text=True
)
return TextContent(text=result.stdout)
if __name__ == "__main__":
asyncio.run(app.run())
EOF
chmod +x "$MCP_DIR/servers/pacman/server.py"
mkdir -p "$MCP_DIR/servers/pacman"
}
# macOS固有の設定
setup_macos_specific() {
echo -e "${GREEN}macOS固有の設定を適用中...${NC}"
# Homebrew用のMCPサーバー
cat > "$MCP_DIR/servers/homebrew/server.py" << 'EOF'
#!/usr/bin/env python3
import asyncio
import subprocess
from mcp.server import Server
from mcp.types import Tool, TextContent
app = Server("homebrew-tools")
@app.tool()
async def search_formulae(query: str) -> TextContent:
"""Homebrewのformulaを検索"""
result = subprocess.run(
["brew", "search", query],
capture_output=True,
text=True
)
return TextContent(text=result.stdout)
@app.tool()
async def get_formula_info(formula: str) -> TextContent:
"""formula情報を取得"""
result = subprocess.run(
["brew", "info", formula],
capture_output=True,
text=True
)
return TextContent(text=result.stdout)
@app.tool()
async def list_installed_formulae() -> TextContent:
"""インストール済みformulaのリスト"""
result = subprocess.run(
["brew", "list"],
capture_output=True,
text=True
)
return TextContent(text=result.stdout)
if __name__ == "__main__":
asyncio.run(app.run())
EOF
chmod +x "$MCP_DIR/servers/homebrew/server.py"
mkdir -p "$MCP_DIR/servers/homebrew"
}
# テスト実行
test_mcp() {
echo -e "${GREEN}MCP設定をテスト中...${NC}"
# Claude Codeでテスト
claude "MCPが正常に動作しているかテストしてください。利用可能なツールを確認してください。"
}
# メイン処理
case "$1" in
install)
install_mcp_servers
;;
config)
create_config
;;
custom)
create_custom_server "${2:-custom-tools}"
;;
arch)
setup_arch_specific
;;
macos)
setup_macos_specific
;;
test)
test_mcp
;;
all)
install_mcp_servers
create_config
create_custom_server "custom-tools"
# OS固有の設定
if [[ "$(uname)" == "Linux" ]]; then
setup_arch_specific
elif [[ "$(uname)" == "Darwin" ]]; then
setup_macos_specific
fi
echo -e "${GREEN}✓ MCP設定完了${NC}"
echo -e "${YELLOW}Claude Codeを再起動してください${NC}"
;;
*)
echo "使用方法: $0 {install|config|custom|arch|macos|test|all}"
echo ""
echo " install - MCPサーバーをインストール"
echo " config - 設定ファイルを作成"
echo " custom - カスタムMCPサーバーを作成"
echo " arch - Arch Linux固有の設定"
echo " macos - macOS固有の設定"
echo " test - 設定をテスト"
echo " all - 全ての設定を実行"
exit 1
;;
esac