Codex: Codex MCP
Last updated: 2026-08-31
MCP (Model Context Protocol) is Codex's bridge to the external world — databases, APIs, cloud services, documentation systems, and more.
📋 Prerequisites: Understanding Codex basic operations and configuration
1. What You Will Learn
- MCP concepts and architecture
- Built-in MCP servers
- Custom MCP integration
- Common MCP scenarios
2. What Is MCP
MCP stands for Model Context Protocol, defining a standard protocol for AI Agent communication with external tools.
graph LR
A[Codex Agent] --> B[MCP Client]
B --> C[MCP Server: Database]
B --> D[MCP Server: GitHub]
B --> E[MCP Server: Documentation]
B --> F[MCP Server: Cloud Services]
| Concept | Description |
|---|---|
| MCP Server | Service providing tools and data |
| MCP Client | Codex's built-in client |
| Tool | Operation exposed by the Server |
| Resource | Data provided by the Server |
3. Built-in MCP Servers
Codex includes commonly used MCP servers:
| Server | Function | Example Tools |
|---|---|---|
| filesystem | File operations | read_file, write_file, list_dir |
| github | GitHub operations | create_pr, list_issues, review_pr |
| fetch | Network requests | get_url, post_url |
| database | Database operations | query, insert, update |
4. Configuring MCP Servers
(1) Global Configuration
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) Project-Level Configuration
JSON
// .codex/mcp.json
{
"mcp_servers": {
"database": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://localhost/mydb"
}
}
}
}
▶ Example 1: Alice Connects to a Database
JSON
// Alice's project MCP config
{
"mcp_servers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://localhost/ecommerce"
}
}
}
}
TEXT
📖 Display only
# Alice has Codex query the database
> Query the order count and total amount for the last 7 days
# Codex calls the database via 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. Common MCP Scenarios
(1) Database Operations
TEXT
📖 Display only
> View the structure of the users table
> Query the number of active users
> Create a database migration for the new feature
(2) GitHub Integration
TEXT
📖 Display only
> List pending Issues
> Create a PR and link it to Issue #15
> View review comments on PR #42
(3) Documentation Query
TEXT
📖 Display only
> Look up the API design doc in Notion
> Read the technical spec from Confluence
> Search the project Wiki for deployment guide
(4) Cloud Service Operations
TEXT
📖 Display only
> View AWS S3 bucket list
> Check CloudWatch alerts
> List EC2 instance status
▶ Example 2: Bob's MCP Workflow
TEXT
📖 Display only
# Bob uses MCP for data-driven development
> 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 via MCP:
→ Connected to postgres MCP server
→ Reading products table schema...
→ Creating TypeScript types...
→ Generating API endpoints...
→ Writing integration tests...
→ All done ✓
6. Custom MCP Server
(1) Create a Simple MCP Server
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) Register with Codex
JSON
{
"mcp_servers": {
"my-docs": {
"command": "node",
"args": ["/path/to/my-mcp-server.js"]
}
}
}
7. MCP Security
| Security Measure | Description |
|---|---|
| Permission Control | Each MCP Server only exposes necessary tools |
| Environment Isolation | MCP Servers run in independent processes |
| Audit Logging | Record all MCP tool calls |
| Read-only First | Database and other sensitive services prefer read-only access |
❓ FAQ
Q What's the difference between MCP and regular API calls?
A MCP is a standardized protocol. Codex can automatically discover and use MCP tools without manually writing API call code. Regular APIs require manual integration.
Q Which databases does MCP support?
A PostgreSQL, MySQL, SQLite, MongoDB, and other mainstream databases. The community has MCP servers for even more databases.
Q Is creating a custom MCP Server complex?
A Not really. Using the MCP SDK, creating a Server takes only a few dozen lines of code. The main work is implementing the specific tool logic.
Q Can MCP expose sensitive data?
A Depends on configuration. Recommend using read-only accounts for database MCP, and limiting accessible endpoints for API MCP.
Q Does MCP affect performance?
A Each MCP Server is an independent process with startup overhead. First call is slightly slower; subsequent calls are normal.
📖 Summary
- MCP is the standard protocol for AI Agent connections to external tools
- Built-in servers: filesystem / github / fetch / database
- Configuration: global or project-level mcp.json
- Scenarios: database operations, GitHub integration, documentation queries, cloud services
- Security: permission control, read-only first, audit logging
📝 Exercises
- Basic (⭐): Configure an MCP server (e.g., filesystem), have Codex use it to operate on files.
- Intermediate (⭐⭐): Configure a database MCP server, have Codex query data and generate code.
- Advanced (⭐⭐⭐): Create a custom MCP server that connects to your company's internal API.