用codex定制MCP服务
By:Roy.LiuLast updated:2026-06-04
这一节主要记录用codex定制MCP服务,只能是个入门的小测试。
一个测试的提示语如下:
请编写一个MCP服务,实现的功能如下: 1. 随机生成一个六位数的随机数,随机数包含数字以及大写英文字母 2. 功能实现之后,并配置到codex的配置文件中。

我会先检查当前工作区里是否已有 MCP 服务样例和 Codex 配置文件位置,然后直接实现一个最小可用的服务并把它接入现有配置。
• Running Get-ChildItem -Force
• Running rg --files -g "*mcp*" -g "*.json" -g "*.yaml" -g "*.yml" -g "*.toml" .codex
• Ran rg --files -g "*mcp*" -g "*.json" -g "*.yaml" -g "*.yml" -g "*.toml" .codex
└ .codex\version.json
.codex\chrome-native-hosts-v2.json
… +19 lines (ctrl + t to view transcript)
.codex\plugins\cache\openai-bundled\browser\26.601.21317\scripts\node_modules\classic-level\package.json
.codex\plugins\cache\openai-bundled\browser\26.601.21317\scripts\node_modules\module-error\package.json
• Ran rg -n "mcp|server" .codex
└ .codex\config.toml:60:[mcp_servers.node_repl]
.codex\config.toml:65:[mcp_servers.node_repl.env]
… +5658 lines (ctrl + t to view transcript)
m3MZnmXyRRSgGgtQY2V131opLErFtQuo7V0QHNE1dd9b8Alav+7WxoCceOgQ2ZYP7nqBW2tuDiBcqXfy3t8z8TP+oRXHZ4BOMINpfZMI6wfwCrNdQCEkj8m9hgSMwV4jIJxIvIkEBrME0wjWBzmc1/Oxu3hm2u+p/PLXFrYPOvkgYA8cJJ1J0d3Brt4ID/
FDa0hM1NyAQbMC9Mji
所在位置 行:2 字符: 1
… +3 lines (ctrl + t to view transcript)
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
• 已经定位到 Codex 的 MCP 配置入口在 .codex/config.toml。下一步我会读取这个配置格式,然后在用户目录下创建一个本地 MCP 服务并把它注册进去。
└ rg: C:\Users\LENOVO: 拒绝访问。 (os error 5)
• Ran Get-Content .codex\config.toml
└ model_provider = "OpenAI"
model = "gpt-5.4"
… +73 lines (ctrl + t to view transcript)
BROWSER_USE_CODEX_APP_BUILD_FLAVOR = "prod"
CODEX_CLI_PATH = 'C:\Users\LENOVO\AppData\Local\OpenAI\Codex\bin\716dda49c14d31a0\codex.exe'
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
• 我已经拿到配置结构了。为了避免依赖安装,我会做一个零依赖的 stdio MCP 服务,用 Python 直接实现协议,这样接入最稳。
• Running python --version
• Running Get-ChildItem .codex -Force
• Ran python --version
└ Python 3.14.4
• Ran Get-Command python | Format-List -Property Source,Name,Version
└
… +4 lines (ctrl + t to view transcript)
• Ran Get-ChildItem .codex -Force
└
… +48 lines (ctrl + t to view transcript)
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
• 现在开始落地实现。我会新增一个本地 Python MCP 服务目录,提供 generate_random_code 工具,然后把它注册到 .codex/config.toml 里。
• Ran New-Item -ItemType Directory -Force mcp-random-code
└ New-Item : 对路径“mcp-random-code”的访问被拒绝。
所在位置 行:2 字符: 1
… +4 lines (ctrl + t to view transcript)
+ FullyQualifiedErrorId : CreateDirectoryUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
• 目录创建被当前命令沙箱拦住了,但写入用户目录本身是允许的。我改用补丁方式直接创建文件和目录,并同步更新 Codex 配置。
• Edited 3 files (+116 -0)
└ .codex\config.toml (+5 -0)
64
65 +[mcp_servers.random_code]
66 +args = ['C:\Users\LENOVO\mcp-random-code\server.py']
67 +command = 'D:\software\python3.14\python.exe'
68 +startup_timeout_sec = 30
69 +
70 [mcp_servers.node_repl.env]
└ mcp-random-code\README.md (+5 -0)
1 +# random-code-mcp
2 +
3 +一个本地 `stdio` MCP 服务,提供 `generate_random_code` 工具。
4 +
5 +返回值是一个 6 位随机字符串,字符集为 `0-9` 和 `A-Z`。
└ mcp-random-code\server.py (+106 -0)
1 +import json
2 +import random
3 +import string
4 +import sys
5 +
6 +
7 +ALPHABET = string.digits + string.ascii_uppercase
8 +TOOL_NAME = "generate_random_code"
9 +
10 +
11 +def make_response(request_id, result):
12 + return {"jsonrpc": "2.0", "id": request_id, "result": result}
13 +
14 +
15 +def make_error(request_id, code, message):
16 + return {
17 + "jsonrpc": "2.0",
18 + "id": request_id,
19 + "error": {"code": code, "message": message},
20 + }
21 +
22 +
23 +def write_message(message):
24 + sys.stdout.write(json.dumps(message, ensure_ascii=True) + "\n")
25 + sys.stdout.flush()
26 +
27 +
28 +def generate_code():
29 + return "".join(random.choice(ALPHABET) for _ in range(6))
30 +
31 +
32 +def handle_initialize(request):
33 + return {
34 + "protocolVersion": "2024-11-05",
35 + "capabilities": {"tools": {}},
36 + "serverInfo": {"name": "random-code-mcp", "version": "1.0.0"},
37 + }
38 +
39 +
40 +def handle_tools_list():
41 + return {
42 + "tools": [
43 + {
44 + "name": TOOL_NAME,
45 + "description": "Generate a random 6-character code using digits and uppercase letters.",
46 + "inputSchema": {
47 + "type": "object",
48 + "properties": {},
49 + "additionalProperties": False,
50 + },
51 + }
52 + ]
53 + }
54 +
55 +
56 +def handle_tools_call(request):
57 + params = request.get("params", {})
58 + name = params.get("name")
59 + if name != TOOL_NAME:
65 + {
66 + "content": [
67 + {"type": "text", "text": code},
68 + ],
69 + "structuredContent": {
70 + "code": code,
71 + "length": 6,
72 + "charset": "0-9A-Z",
73 + },
74 + },
75 + )
76 +
77 +
78 +def main():
79 + for raw_line in sys.stdin:
80 + line = raw_line.strip()
81 + if not line:
82 + continue
83 +
84 + try:
85 + request = json.loads(line)
86 + except json.JSONDecodeError:
87 + write_message(make_error(None, -32700, "Parse error"))
88 + continue
89 +
90 + method = request.get("method")
91 + request_id = request.get("id")
92 +
93 + if method == "initialize":
94 + write_message(make_response(request_id, handle_initialize(request)))
95 + elif method == "notifications/initialized":
96 + continue
97 + elif method == "tools/list":
98 + write_message(make_response(request_id, handle_tools_list()))
99 + elif method == "tools/call":
100 + write_message(handle_tools_call(request))
101 + else:
102 + write_message(make_error(request_id, -32601, f"Method not found: {method}"))
103 +
104 +
105 +if __name__ == "__main__":
106 + main()
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
• 代码和配置已经写好。现在我做一次本地协议验证,确认这个 MCP 服务能正确响应 initialize、tools/list 和 tools/call。
• Running Get-Content C:\Users\LENOVO\mcp-random-code\server.py
• Ran Get-Content C:\Users\LENOVO\mcp-random-code\server.py
└ import json
import random
… +102 lines (ctrl + t to view transcript)
if __name__ == "__main__":
main()
• Ran @'
│ {"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}
│ {"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}
│ … +2 lines
└ {"jsonrpc": "2.0", "id": 1, "result": {"protocolVersion": "2024-11-05", "capabilities": {"tools": {}}, "serverInfo": {"name": "random-code-mcp", "version": "1.0.0"}}}
{"jsonrpc": "2.0", "id": 2, "result": {"tools": [{"name": "generate_random_code", "description": "Generate a random 6-character code using digits and uppercase letters.", "inputSchema": {"type": "object",
"properties": {}, "additionalProperties": false}}]}}
{"jsonrpc": "2.0", "id": 3, "result": {"content": [{"type": "text", "text": "SOU8R2"}], "structuredContent": {"code": "SOU8R2", "length": 6, "charset": "0-9A-Z"}}}
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
• 已实现并配置完成。
新增了本地 MCP 服务 mcp-random-code/server.py:1,它提供一个工具 generate_random_code,每次调用都会返回一个 6 位随机字符串,字符集是 0-9 和 A-Z。说明文件在 mcp-random-code/README.md:1。
我也已经把它注册到 Codex 配置里了,配置项在 .codex/config.toml:64。当前配置会用本机 Python 启动这个 MCP 服务。
本地验证已通过:
- initialize 正常
- tools/list 能列出 generate_random_code
- tools/call 能返回类似 SOU8R2 这样的 6 位随机码
如果你要立即在 Codex 里使用,重启一下 Codex 或刷新 MCP 连接即可。整个codex运行并部署完成,现在检查一下MCP是否已经生效了,执行命令 /mcp 出现如下图所示:

说明已经生效了,现在测试如何调用这个MCP服务,直接在命令行输入你想调用哪个mcp服务,如果有参数,直接说出你的参数就可以,比如:

我们再次打开 codex的 config.toml文件,可以看到MCP服务配置:
[mcp_servers.random_code] args = ['C:\Users\LENOVO\mcp-random-code\server.py'] command = 'D:\software\python3.14\python.exe' startup_timeout_sec = 30
可以看到用了python实现的,并且代码在相应的目录都能找到。这是一个非常简单的实例,几乎没什么价值,但入门就是这样,先记录。
From:一号门
Previous:codex 命令行模式下的一些基础命令
Next: None

COMMENTS