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


2. What Is MCP

MCP stands for Model Context Protocol, defining a standard protocol for AI Agent communication with external tools.

100%
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


📝 Exercises

  1. Basic (⭐): Configure an MCP server (e.g., filesystem), have Codex use it to operate on files.
  2. Intermediate (⭐⭐): Configure a database MCP server, have Codex query data and generate code.
  3. Advanced (⭐⭐⭐): Create a custom MCP server that connects to your company's internal API.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏