OPENAI_CHATGPT.md•8.24 kB
# OpenAI ChatGPT 統合ガイド
このガイドでは、Universal MCP ServerをOpenAI ChatGPTのGPTsやAssistants APIから利用する方法を説明します。
## 🎯 概要
OpenAI ChatGPTは**MCPプロトコル**ではなく**OpenAPI (Actions)**形式でAPIを統合します。このサーバーはOpenAI互換のRESTful APIエンドポイントとOpenAPIスキーマを提供します。
## 🚀 クイックスタート
### 1. サーバー起動
```bash
# インタラクティブモード
./start-openai.sh
# または直接起動
npm run build
PORT=3000 node build/openai-server.js
```
### 2. ngrokで公開(必要な場合)
```bash
# ngrok起動
ngrok http 3000
# 公開URLを控える
# 例: https://abc123.ngrok.io
```
### 3. OpenAPI仕様の確認
ブラウザで以下にアクセス:
```
http://localhost:3000/.well-known/openapi.json
```
ngrok使用時:
```
https://your-ngrok-url.ngrok.io/.well-known/openapi.json
```
## 📋 OpenAI GPTs への統合
### 手順
1. **ChatGPT を開く**
- https://chat.openai.com にアクセス
- 左下のユーザー名 → "My GPTs" をクリック
2. **新しいGPTを作成**
- "Create a GPT" をクリック
- GPTの名前と説明を設定
3. **Actions を追加**
- "Configure" タブを選択
- 下にスクロールして "Actions" セクションへ
- "Create new action" をクリック
4. **OpenAPI スキーマをインポート**
**方法1: URLでインポート(推奨)**
```
https://your-ngrok-url.ngrok.io/.well-known/openapi.json
```
**方法2: 直接貼り付け**
- `/.well-known/openapi.json` の内容をコピー
- Schema欄に貼り付け
5. **認証設定(オプション)**
API認証を有効にした場合:
- Authentication → "API Key"
- Auth Type: "Bearer"
- API Keyに設定したキーを入力
6. **テスト**
GPTとの会話で以下を試してください:
```
ファイルに "test.txt" というキーで "Hello World" と書き込んでください
```
```
ファイル一覧を表示してください
```
```
"test.txt" を読み込んでください
```
## 🔌 利用可能なエンドポイント
### 1. ファイル書き込み
```http
POST /storage/write
Content-Type: application/json
{
"key": "example.txt",
"content": "Hello, World!"
}
```
**レスポンス:**
```json
{
"success": true,
"key": "example.txt",
"message": "File written successfully"
}
```
### 2. ファイル読み込み
```http
GET /storage/read/{key}
```
**レスポンス:**
```json
{
"key": "example.txt",
"content": "Hello, World!"
}
```
### 3. ファイル削除
```http
DELETE /storage/delete/{key}
```
**レスポンス:**
```json
{
"success": true,
"key": "example.txt",
"message": "File deleted successfully"
}
```
### 4. ファイル一覧
```http
GET /storage/list?pattern=*.txt
```
**レスポンス:**
```json
{
"files": ["example.txt", "test.txt"],
"count": 2
}
```
### 5. ファイル検索
```http
GET /storage/search?query=Hello
```
**レスポンス:**
```json
{
"results": [
{
"key": "example.txt",
"content": "Hello, World!"
}
],
"count": 1
}
```
## 🔒 セキュリティ設定
### API認証を有効化
```bash
# 起動時に設定
OPENAI_API_KEY="your-secret-key" PORT=3000 node build/openai-server.js
```
または `start-openai.sh` でインタラクティブに設定。
### リクエスト例(認証あり)
```bash
curl -X POST https://your-ngrok-url.ngrok.io/storage/write \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-secret-key" \
-d '{"key": "test.txt", "content": "Hello"}'
```
## 🌐 公開URL設定
ngrokや他のトンネリングサービス使用時:
```bash
PUBLIC_URL="https://your-ngrok-url.ngrok.io" PORT=3000 node build/openai-server.js
```
これにより、OpenAPIスキーマ内のサーバーURLが自動的に更新されます。
## 🧪 テスト方法
### 1. ヘルスチェック
```bash
curl http://localhost:3000/health
```
### 2. OpenAPIスキーマ確認
```bash
curl http://localhost:3000/.well-known/openapi.json | jq
```
### 3. ファイル操作テスト
```bash
# 書き込み
curl -X POST http://localhost:3000/storage/write \
-H "Content-Type: application/json" \
-d '{"key": "test.txt", "content": "Hello, World!"}'
# 読み込み
curl http://localhost:3000/storage/read/test.txt
# 一覧
curl http://localhost:3000/storage/list
# 検索
curl "http://localhost:3000/storage/search?query=Hello"
# 削除
curl -X DELETE http://localhost:3000/storage/delete/test.txt
```
## 📚 OpenAI Assistants API との統合
Assistants APIでFunction Callingとして使う場合:
```python
from openai import OpenAI
client = OpenAI()
assistant = client.beta.assistants.create(
name="Storage Assistant",
instructions="You are a helpful assistant that can store and retrieve files.",
model="gpt-4-turbo-preview",
tools=[
{
"type": "function",
"function": {
"name": "write_file",
"description": "Write content to a file",
"parameters": {
"type": "object",
"properties": {
"key": {"type": "string", "description": "File key/name"},
"content": {"type": "string", "description": "File content"}
},
"required": ["key", "content"]
}
}
},
{
"type": "function",
"function": {
"name": "read_file",
"description": "Read a file by key",
"parameters": {
"type": "object",
"properties": {
"key": {"type": "string", "description": "File key/name"}
},
"required": ["key"]
}
}
}
]
)
```
Function Calling時に実際のAPI呼び出しを行います。
## 🔧 トラブルシューティング
### "Failed to build actions from MCP endpoint"
- **原因**: OpenAI GPTsはMCPプロトコルに対応していません
- **解決**: `openai-server.js` を使用してください(このガイドの方法)
### スキーマが読み込めない
- OpenAPI仕様が正しいJSON形式か確認
- `/.well-known/openapi.json` が正常にアクセスできるか確認
- ngrok使用時、URLが正しいか確認
### 認証エラー
- `OPENAI_API_KEY` が正しく設定されているか確認
- GPTs側で同じAPIキーを設定しているか確認
- Authorization ヘッダーが `Bearer YOUR_KEY` 形式か確認
### CORSエラー
- サーバーはすでにCORSを許可しています
- ブラウザコンソールでエラー詳細を確認
## 📖 関連ドキュメント
- [OpenAI GPTs Actions ドキュメント](https://platform.openai.com/docs/actions)
- [OpenAPI 3.1 仕様](https://spec.openapis.org/oas/v3.1.0)
- [ngrok ドキュメント](https://ngrok.com/docs)
## 💡 Tips
1. **開発時の注意**
- ngrokの無料プランでは接続が定期的に切れます
- 固定URLが必要な場合は有料プランを検討
2. **プロダクション利用**
- 必ずAPI認証を有効化してください
- HTTPSを使用してください(ngrokは自動的にHTTPS)
- レート制限の実装を検討してください
3. **パフォーマンス**
- ファイル数が多い場合は `list` にページネーションの追加を検討
- 大きなファイルの場合はストリーミングを検討
## 🆚 MCPサーバーとの違い
| 機能 | MCPサーバー | OpenAIサーバー |
|------|------------|---------------|
| プロトコル | MCP (SSE) | REST API |
| 対応クライアント | Claude Desktop | OpenAI GPTs, Assistants |
| 認証 | Optional API Key | Optional Bearer Token |
| スキーマ | MCP Tools/Resources | OpenAPI 3.1 |
| トランスポート | STDIO/HTTP+SSE | HTTP |
両方のサーバーは**同じストレージバックエンド**を使用するため、データは共有されます。