This commit is contained in:
2025-06-03 05:09:56 +09:00
parent 5a441e847d
commit df86fb827e
3 changed files with 1 additions and 1 deletions

172
docs/AI_CARD_INTEGRATION.md Normal file
View File

@ -0,0 +1,172 @@
# ai.card と ai.gpt の統合ガイド
## 概要
ai.gptのMCPサーバーにai.cardのツールを統合し、AIがカードゲームシステムとやり取りできるようになりました。
## セットアップ
### 1. 必要な環境
- Python 3.13
- ai.gpt プロジェクト
- ai.card プロジェクト(`./card` ディレクトリ)
### 2. 起動手順
**ステップ1: ai.cardサーバーを起動**ターミナル1
```bash
cd card
./start_server.sh
```
**ステップ2: ai.gpt MCPサーバーを起動**ターミナル2
```bash
aigpt server
```
起動時に以下が表示されることを確認:
- 🎴 Card Game System: 6 tools
- 🎴 ai.card: ./card directory detected
**ステップ3: AIと対話**ターミナル3
```bash
aigpt conv syui --provider openai
```
## 使用可能なコマンド
### カード関連の質問例
```
# カードコレクションを表示
「カードコレクションを見せて」
「私のカードを見せて」
「カード一覧を表示して」
# ガチャを実行
「ガチャを引いて」
「カードを引きたい」
# コレクション分析
「私のコレクションを分析して」
# ガチャ統計
「ガチャの統計を見せて」
```
## 技術仕様
### MCP ツール一覧
| ツール名 | 説明 | パラメータ |
|---------|------|-----------|
| `card_get_user_cards` | ユーザーのカード一覧取得 | did, limit |
| `card_draw_card` | ガチャでカード取得 | did, is_paid |
| `card_get_card_details` | カード詳細情報取得 | card_id |
| `card_analyze_collection` | コレクション分析 | did |
| `card_get_gacha_stats` | ガチャ統計取得 | なし |
| `card_system_status` | システム状態確認 | なし |
### 動作の流れ
1. **ユーザーがカード関連の質問をする**
- AIがキーワードカード、コレクション、ガチャなどを検出
2. **AIが適切なMCPツールを呼び出す**
- OpenAIのFunction Callingを使用
- didパラメータには会話相手のユーザーID'syui')を使用
3. **ai.gpt MCPサーバーがai.cardサーバーに転送**
- http://localhost:8001 → http://localhost:8000
- 適切なエンドポイントにリクエストを転送
4. **結果をAIが解釈して返答**
- カード情報を分かりやすく説明
- エラー時は適切なガイダンスを提供
## 設定
### config.json
```json
{
"providers": {
"openai": {
"api_key": "your-api-key",
"default_model": "gpt-4o-mini",
"system_prompt": "カード関連の質問では、必ずcard_get_user_cardsなどのツールを使用してください。"
}
},
"mcp": {
"servers": {
"ai_gpt": {
"endpoints": {
"card_get_user_cards": "/card_get_user_cards",
"card_draw_card": "/card_draw_card",
"card_get_card_details": "/card_get_card_details",
"card_analyze_collection": "/card_analyze_collection",
"card_get_gacha_stats": "/card_get_gacha_stats",
"card_system_status": "/card_system_status"
}
}
}
}
}
```
## トラブルシューティング
### エラー: "ai.card server is not running"
ai.cardサーバーが起動していません。以下を実行
```bash
cd card
./start_server.sh
```
### エラー: "カード一覧の取得に失敗しました"
1. ai.cardサーバーが正常に起動しているか確認
2. aigpt serverを再起動
3. ポート8000と8001が使用可能か確認
### プロセスの終了方法
```bash
# ポート8001のプロセスを終了
lsof -ti:8001 | xargs kill -9
# ポート8000のプロセスを終了
lsof -ti:8000 | xargs kill -9
```
## 実装の詳細
### 主な変更点
1. **ai.gpt MCPサーバーの拡張** (`src/aigpt/mcp_server.py`)
- `./card`ディレクトリの存在を検出
- ai.card用のMCPツールを自動登録
2. **AIプロバイダーの更新** (`src/aigpt/ai_provider.py`)
- card_*ツールの定義追加
- ツール実行時のパラメータ処理
3. **MCPクライアントの拡張** (`src/aigpt/cli.py`)
- `has_card_tools`プロパティ追加
- ai.card MCPメソッドの実装
## 今後の拡張案
- [ ] カードバトル機能の追加
- [ ] カードトレード機能
- [ ] レアリティ別の表示
- [ ] カード画像の表示対応
- [ ] atproto連携の実装
## 関連ドキュメント
- [ai.card 開発ガイド](./card/claude.md)
- [エコシステム統合設計書](./CLAUDE.md)
- [ai.gpt README](./README.md)

109
docs/FIXED_MCP_TOOLS.md Normal file
View File

@ -0,0 +1,109 @@
# Fixed MCP Tools Issue
## Summary
The issue where AI wasn't calling card tools has been fixed. The problem was:
1. The `chat` command wasn't creating an MCP client when using OpenAI
2. The system prompt in `build_context_prompt` didn't mention available tools
## Changes Made
### 1. Updated `/Users/syui/ai/gpt/src/aigpt/cli.py` (chat command)
Added MCP client creation for OpenAI provider:
```python
# Get config instance
config_instance = Config()
# Get defaults from config if not provided
if not provider:
provider = config_instance.get("default_provider", "ollama")
if not model:
if provider == "ollama":
model = config_instance.get("providers.ollama.default_model", "qwen2.5")
else:
model = config_instance.get("providers.openai.default_model", "gpt-4o-mini")
# Create AI provider with MCP client if needed
ai_provider = None
mcp_client = None
try:
# Create MCP client for OpenAI provider
if provider == "openai":
mcp_client = MCPClient(config_instance)
if mcp_client.available:
console.print(f"[dim]MCP client connected to {mcp_client.active_server}[/dim]")
ai_provider = create_ai_provider(provider=provider, model=model, mcp_client=mcp_client)
console.print(f"[dim]Using {provider} with model {model}[/dim]\n")
except Exception as e:
console.print(f"[yellow]Warning: Could not create AI provider: {e}[/yellow]")
console.print("[yellow]Falling back to simple responses[/yellow]\n")
```
### 2. Updated `/Users/syui/ai/gpt/src/aigpt/persona.py` (build_context_prompt method)
Added tool instructions to the system prompt:
```python
context_prompt += f"""IMPORTANT: You have access to the following tools:
- Memory tools: get_memories, search_memories, get_contextual_memories
- Relationship tools: get_relationship
- Card game tools: card_get_user_cards, card_draw_card, card_analyze_collection
When asked about cards, collections, or anything card-related, YOU MUST use the card tools.
For "カードコレクションを見せて" or similar requests, use card_get_user_cards with did='{user_id}'.
Respond to this message while staying true to your personality and the established relationship context:
User: {current_message}
AI:"""
```
## Test Results
After the fix:
```bash
$ aigpt chat syui "カードコレクションを見せて"
🔍 [MCP Client] Checking availability...
[MCP Client] ai_gpt server connected successfully
[MCP Client] ai.card tools detected and available
MCP client connected to ai_gpt
Using openai with model gpt-4o-mini
🔧 [OpenAI] 1 tools called:
- card_get_user_cards({"did":"syui"})
🌐 [MCP] Executing card_get_user_cards...
[MCP] Result: {'error': 'カード一覧の取得に失敗しました'}...
```
The AI is now correctly calling the `card_get_user_cards` tool! The error is expected because the ai.card server needs to be running on port 8000.
## How to Use
1. Start the MCP server:
```bash
aigpt server --port 8001
```
2. (Optional) Start the ai.card server:
```bash
cd card && ./start_server.sh
```
3. Use the chat command with OpenAI:
```bash
aigpt chat syui "カードコレクションを見せて"
```
The AI will now automatically use the card tools when asked about cards!
## Test Script
A test script `/Users/syui/ai/gpt/test_openai_tools.py` is available to test OpenAI API tool calls directly.