Codex: Codex MCP
最終更新:2026-08-31
MCP(Model Context Protocol)は Codex の外部世界への架け橋です — データベース、API、クラウドサービス、ドキュメントシステムなどに接続します。
📋 前提条件: Codex の基本操作と設定を理解していること
1. 学ぶ内容
- MCP の概念とアーキテクチャ
- 内蔵 MCP サーバー
- カスタム MCP 統合
- 一般的な MCP シナリオ
2. MCP とは
MCP は Model Context Protocol の略で、AI エージェントと外部ツールの通信のための標準プロトコルを定義しています。
graph LR
A[Codex エージェント] --> B[MCP クライアント]
B --> C[MCP サーバー:データベース]
B --> D[MCP サーバー:GitHub]
B --> E[MCP サーバー:ドキュメント]
B --> F[MCP サーバー:クラウドサービス]
| 概念 | 説明 |
|---|---|
| MCP サーバー | ツールとデータを提供するサービス |
| MCP クライアント | Codex の内蔵クライアント |
| ツール | サーバーが公開する操作 |
| リソース | サーバーが提供するデータ |
3. 内蔵 MCP サーバー
Codex には一般的な MCP サーバーが含まれています:
| サーバー | 機能 | 例のツール |
|---|---|---|
| filesystem | ファイル操作 | read_file, write_file, list_dir |
| github | GitHub 操作 | create_pr, list_issues, review_pr |
| fetch | ネットワークリクエスト | get_url, post_url |
| database | データベース操作 | query, insert, update |
4. MCP サーバーの設定
(1) グローバル設定
JSON
{
"mcp_servers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/project"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "ghp_xxx"
}
}
}
}
(2) プロジェクトレベルの設定
JSON
// .codex/mcp.json
{
"mcp_servers": {
"database": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://localhost/mydb"
}
}
}
}
▶ 例1:Alice がデータベースに接続
JSON
// Alice のプロジェクト MCP 設定
{
"mcp_servers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://localhost/ecommerce"
}
}
}
}
TEXT
📖 参照専用
# Alice が Codex にデータベースをクエリさせる
> Query the order count and total amount for the last 7 days
# Codex が MCP を通じてデータベースにアクセス
→ Connected to postgres MCP server
→ Executing: SELECT COUNT(*), SUM(amount) FROM orders WHERE created_at > NOW() - INTERVAL '7 days'
→ Result: 1,234 orders, $89,567.89 total
5. 一般的な MCP シナリオ
(1) データベース操作
TEXT
📖 参照専用
> View the structure of the users table
> Query the number of active users
> Create a database migration for the new feature
(2) GitHub 統合
TEXT
📖 参照専用
> List pending Issues
> Create a PR and link it to Issue #15
> View review comments on PR #42
(3) ドキュメントクエリ
TEXT
📖 参照専用
> Look up the API design doc in Notion
> Read the technical spec from Confluence
> Search the project Wiki for deployment guide
(4) クラウドサービス操作
TEXT
📖 参照専用
> View AWS S3 bucket list
> Check CloudWatch alerts
> List EC2 instance status
▶ 例2:Bob の MCP ワークフロー
TEXT
📖 参照専用
# Bob が MCP を使ったデータ駆動開発
> 1. Query the structure of the products table in the database
> 2. Create TypeScript type definitions based on the Schema
> 3. Generate CRUD API endpoints
> 4. Write integration tests
# Codex が MCP 経由で:
→ Connected to postgres MCP server
→ Reading products table schema...
→ Creating TypeScript types...
→ Generating API endpoints...
→ Writing integration tests...
→ All done ✓
6. カスタム MCP サーバー
(1) シンプルな MCP サーバーの作成
TYPESCRIPT
import { Server } from "@modelcontextprotocol/sdk/server";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio";
const server = new Server({
name: "my-custom-server",
version: "1.0.0",
});
server.tool("search_docs", { query: { type: "string" } }, async ({ query }) => {
const results = await searchDocumentation(query);
return { content: [{ type: "text", text: JSON.stringify(results) }] };
});
const transport = new StdioServerTransport();
await server.connect(transport);
(2) Codex への登録
JSON
{
"mcp_servers": {
"my-docs": {
"command": "node",
"args": ["/path/to/my-mcp-server.js"]
}
}
}
7. MCP セキュリティ
| セキュリティ対策 | 説明 |
|---|---|
| パーミッション制御 | 各 MCP サーバーは必要なツールのみ公開 |
| 環境分離 | MCP サーバーは独立したプロセスで実行 |
| 監査ログ | すべての MCP ツール呼び出しを記録 |
| 読み取り専用優先 | データベースなどの機密サービスは読み取り専用アクセスを推奨 |
❓ よくある質問
Q MCP と通常の API 呼び出しの違いは何ですか?
A MCP は標準化されたプロトコルです。Codex は自動的に MCP ツールを発見して使用でき、API 呼び出しコードを手動で書く必要がありません。通常の API は手動の統合が必要です。
Q MCP はどのデータベースをサポートしていますか?
A PostgreSQL、MySQL、SQLite、MongoDB などの主流データベースです。コミュニティによりさらに多くのデータベースの MCP サーバーが提供されています。
Q カスタム MCP サーバーの作成は複雑ですか?
A それほどではありません。MCP SDK を使用すれば、数ダース行のコードでサーバーを作成できます。主な作業は具体的なツールロジックの実装です。
Q MCP は機密データを公開する可能性がありますか?
A 設定によります。データベース MCP には読み取り専用アカウントの使用を推奨し、API MCP にはアクセス可能なエンドポイントの制限を推奨します。
Q MCP はパフォーマンスに影響しますか?
A 各 MCP サーバーは独立したプロセスで起動オーバーヘッドがあります。最初の呼び出しはわずかに遅くなりますが、その後の呼び出しは正常です。
📖 まとめ
- MCP は AI エージェントと外部ツールの接続のための標準プロトコル
- 内蔵サーバー:filesystem / github / fetch / database
- 設定:グローバルまたはプロジェクトレベルの mcp.json
- シナリオ:データベース操作、GitHub 統合、ドキュメントクエリ、クラウドサービス
- セキュリティ:パーミッション制御、読み取り専用優先、監査ログ
📝 練習問題
- 基本 (⭐):MCP サーバー(例:filesystem)を設定し、Codex にファイル操作を行わせる。
- 中級 (⭐⭐):データベース MCP サーバーを設定し、Codex にデータをクエリしてコードを生成させる。
- 上級 (⭐⭐⭐):会社の内部 API に接続するカスタム MCP サーバーを作成する。