"""Check MCP settings and exercise the local stdio server shipped beside this script."""
import json
import os
from hashlib import sha256
from pathlib import Path
import shutil
import subprocess
import sys
from tempfile import TemporaryDirectory
import tomllib
def main():
codex = shutil.which("codex.exe") or shutil.which("codex")
if not codex:
raise SystemExit("Codex CLI must be available on PATH.")
cases = []
def record(name, inputs, operation, expected, actual):
assert actual == expected, (name, expected, actual)
cases.append(dict(name="mcp_" + name, input=inputs, operation=operation,
expected=expected, actual=actual))
with TemporaryDirectory(prefix="mbsky-mcp-config-") as temp:
root = Path(temp)
environment = dict(os.environ, CODEX_HOME=temp)
config = (
'mcp_oauth_callback_port = 8192\n'
'mcp_oauth_credentials_store = "file"\n'
'[mcp_servers.catalog]\ncommand = "python"\nargs = ["catalog_tool.py"]\n'
'env = { CATALOG_MODE = "readonly" }\n'
'enabled_tools = ["read_record", "erase_record"]\n'
'disabled_tools = ["erase_record"]\n'
'startup_timeout_sec = 8\ntool_timeout_sec = 4\n'
'[mcp_servers.catalog.tools.read_record]\napproval_mode = "prompt"\n'
'[mcp_servers.web_sample]\nurl = "https://catalog.example.invalid/mcp"\n'
'enabled = false\n'
'env_http_headers = { X_Access = "MBSKY_CATALOG_TOKEN" }\n'
'http_headers = { X_Site = "catalog" }\n'
)
(root / "config.toml").write_bytes(config.encode())
def cli(*args):
return subprocess.run([codex,*args],cwd=root,env=environment,encoding="utf8",
capture_output=True,check=True,timeout=15).stdout
local = json.loads(cli("mcp","get","catalog","--json"))
transport = local["transport"]
record("launch", config, "Read actual launch settings with the installed CLI",
["stdio","python",["catalog_tool.py"],{"CATALOG_MODE":"readonly"}],
[transport["type"],transport["command"],transport["args"],transport["env"]])
record("bounds", config, "Read selected allow and deny lists and the two timeouts",
[["read_record","erase_record"],["erase_record"],8,4],
[local["enabled_tools"],local["disabled_tools"],local["startup_timeout_sec"],local["tool_timeout_sec"]])
remote = json.loads(cli("mcp","get","web_sample","--json"))
transport = remote["transport"]
record("http", config, "Read the disabled HTTP transport and header mappings without connecting",
[False,"streamable_http",{"X_Access":"MBSKY_CATALOG_TOKEN"},{"X_Site":"catalog"}],
[remote["enabled"],transport["type"],transport["env_http_headers"],transport["http_headers"]])
values = tomllib.loads(config)
record("oauth", config, "Parse OAuth callback and credential store settings locally",
[8192,"file"],[values["mcp_oauth_callback_port"],values["mcp_oauth_credentials_store"]])
version = cli("--version").strip()
tool = Path(__file__).with_name("catalog_tool.py")
proc = subprocess.run([sys.executable,"-B","-X","utf8",str(tool)],encoding="utf8",
capture_output=True,check=True,timeout=20)
outcomes = json.loads(proc.stdout)
for case in outcomes["cases"]:
record(case["name"],case["input"],case["operation"],case["expected"],case["actual"])
print(json.dumps(dict(version={"python":sys.version.split()[0],"codex":version,
"mcp":outcomes["sdk"],"protocol":outcomes["protocol"]},
codex_sha256=sha256(Path(codex).read_bytes()).hexdigest(),cases=cases)))
if __name__ == "__main__":
main()
本例需在 Windows、Python 3.12.14、MCP 1.30.0。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、Python 3.12.14、MCP 1.30.0。
入口文件:projects/codex-mcp_config/mcp_config_lab.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
mcp_config_lab.py,或下载右侧课程工程包。 - 确认本地已准备 Windows、Python 3.12.14、MCP 1.30.0。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a112.zip