Skip to content

[PROBLEM] codex mcp doesn't start codex CLI as an MCP server #2208

@ahmedaymanzekry

Description

@ahmedaymanzekry

File to be fixed
The issue is in https://github.com/openai/openai-cookbook/blob/main/examples/codex/codex_mcp_agents_sdk/building_consistent_workflows_codex_cli_agents_sdk.ipynb

The problem
The MCPServerStdio object is instantiated with args=["-y", "codex", "mcp"]
This produced a help verbose for the codex mcp command as if I started codex CLI in the terminal and then gave it the /mcp directive. And then the Codex MCP server in the example cookbook, that was supposed to run and receive commands through stdio, close connection at initialization.

Solution
As described in here https://github.com/openai/codex/blob/main/docs/advanced.md#model-context-protocol-mcp the command is supposed to be codex mcp-server NOT codex mcp.
So, adding "-server" to the "mcp" argument fixed it for me.

Additional context
The error thrown:

Error initializing MCP server: Connection closed
Traceback (most recent call last):
  File ".../mcp_server.py", line 22, in <module>
    asyncio.run(main())
  File ".../my_env/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File ".../my_env/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File ".../mcp_server.py", line 8, in main
    async with MCPServerStdio(
  File ".../my_env/lib/python3.10/site-packages/agents/mcp/server.py", line 242, in __aenter__
    await self.connect()
  File ".../my_env/lib/python3.10/site-packages/agents/mcp/server.py", line 283, in connect
    server_result = await session.initialize()
  File ".../my_env/lib/python3.10/site-packages/mcp/client/session.py", line 152, in initialize
    result = await self.send_request(
  File ".../my_env/lib/python3.10/site-packages/mcp/shared/session.py", line 286, in send_request
    raise McpError(response_or_error.error)
mcp.shared.exceptions.McpError: Connection closed

My system:
Linux: Ubuntu 20.04.6 LTS
Node version v22.21.0
codex-cli version 0.50.0

My environment Packages:

# packages in environment:
#
# Name                                      Version          Build               Channel
_libgcc_mutex                               0.1              conda_forge         conda-forge
_openmp_mutex                               4.5              2_gnu               conda-forge
aiofiles                                    25.1.0           pypi_0              pypi
aiohappyeyeballs                            2.6.1            pypi_0              pypi
aiohttp                                     3.13.1           pypi_0              pypi
aiosignal                                   1.4.0            pypi_0              pypi
annotated-types                             0.7.0            pypi_0              pypi
anyio                                       4.11.0           pypi_0              pypi
async-timeout                               5.0.1            pypi_0              pypi
attrs                                       25.4.0           pypi_0              pypi
av                                          15.1.0           pypi_0              pypi
bzip2                                       1.0.8            hda65f42_8          conda-forge
ca-certificates                             2025.10.5        hbd8a1cb_0          conda-forge
certifi                                     2025.10.5        pypi_0              pypi
cffi                                        2.0.0            pypi_0              pypi
charset-normalizer                          3.4.4            pypi_0              pypi
click                                       8.3.0            pypi_0              pypi
colorama                                    0.4.6            pypi_0              pypi
coloredlogs                                 15.0.1           pypi_0              pypi
distro                                      1.9.0            pypi_0              pypi
docstring-parser                            0.17.0           pypi_0              pypi
dotenv                                      0.9.9            pypi_0              pypi
eval-type-backport                          0.2.2            pypi_0              pypi
exceptiongroup                              1.3.0            pypi_0              pypi
filelock                                    3.20.0           pypi_0              pypi
flatbuffers                                 25.9.23          pypi_0              pypi
frozenlist                                  1.8.0            pypi_0              pypi
fsspec                                      2025.9.0         pypi_0              pypi
gitdb                                       4.0.12           pypi_0              pypi
gitpython                                   3.1.45           pypi_0              pypi
googleapis-common-protos                    1.71.0           pypi_0              pypi
griffe                                      1.14.0           pypi_0              pypi
grpcio                                      1.76.0           pypi_0              pypi
h11                                         0.16.0           pypi_0              pypi
hf-xet                                      1.2.0            pypi_0              pypi
httpcore                                    1.0.9            pypi_0              pypi
httpx                                       0.28.1           pypi_0              pypi
httpx-sse                                   0.4.3            pypi_0              pypi
huggingface-hub                             0.36.0           pypi_0              pypi
humanfriendly                               10.0             pypi_0              pypi
idna                                        3.11             pypi_0              pypi
importlib-metadata                          8.7.0            pypi_0              pypi
iniconfig                                   2.3.0            pypi_0              pypi
jinja2                                      3.1.6            pypi_0              pypi
jiter                                       0.11.1           pypi_0              pypi
jsonschema                                  4.25.1           pypi_0              pypi
jsonschema-specifications                   2025.9.1         pypi_0              pypi
ld_impl_linux-64                            2.44             h1aa0949_4          conda-forge
libexpat                                    2.7.1            hecca717_0          conda-forge
libffi                                      3.5.2            h9ec8514_0          conda-forge
libgcc                                      15.2.0           h767d61c_7          conda-forge
libgcc-ng                                   15.2.0           h69a702a_7          conda-forge
libgomp                                     15.2.0           h767d61c_7          conda-forge
liblzma                                     5.8.1            hb9d3cd8_2          conda-forge
libnsl                                      2.0.1            hb9d3cd8_1          conda-forge
libsqlite                                   3.50.4           h0c1763c_0          conda-forge
libstdcxx                                   15.2.0           h8f9b012_7          conda-forge
libuuid                                     2.41.2           he9a06e4_0          conda-forge
libxcrypt                                   4.4.36           hd590300_1          conda-forge
libzlib                                     1.3.1            hb9d3cd8_2          conda-forge
livekit                                     1.0.17           pypi_0              pypi
livekit-agents                              1.2.15           pypi_0              pypi
livekit-api                                 1.0.7            pypi_0              pypi
livekit-blingfire                           1.0.0            pypi_0              pypi
livekit-plugins-noise-cancellation          0.2.5            pypi_0              pypi
livekit-plugins-openai                      1.2.15           pypi_0              pypi
livekit-plugins-silero                      1.2.15           pypi_0              pypi
livekit-plugins-turn-detector               1.2.15           pypi_0              pypi
livekit-protocol                            1.0.8            pypi_0              pypi
markupsafe                                  3.0.3            pypi_0              pypi
mcp                                         1.19.0           pypi_0              pypi
mpmath                                      1.3.0            pypi_0              pypi
multidict                                   6.7.0            pypi_0              pypi
ncurses                                     6.5              h2d0b736_3          conda-forge
nest-asyncio                                1.6.0            pypi_0              pypi
numpy                                       2.2.6            pypi_0              pypi
onnxruntime                                 1.23.2           pypi_0              pypi
openai                                      2.6.1            pypi_0              pypi
openai-agents                               0.4.2            pypi_0              pypi
openssl                                     3.5.4            h26f9b46_0          conda-forge
opentelemetry-api                           1.38.0           pypi_0              pypi
opentelemetry-exporter-otlp                 1.38.0           pypi_0              pypi
opentelemetry-exporter-otlp-proto-common    1.38.0           pypi_0              pypi
opentelemetry-exporter-otlp-proto-grpc      1.38.0           pypi_0              pypi
opentelemetry-exporter-otlp-proto-http      1.38.0           pypi_0              pypi
opentelemetry-proto                         1.38.0           pypi_0              pypi
opentelemetry-sdk                           1.38.0           pypi_0              pypi
opentelemetry-semantic-conventions          0.59b0           pypi_0              pypi
packaging                                   25.0             pypi_0              pypi
pillow                                      12.0.0           pypi_0              pypi
pip                                         25.2             pyh8b19718_0        conda-forge
pluggy                                      1.6.0            pypi_0              pypi
prometheus-client                           0.23.1           pypi_0              pypi
propcache                                   0.4.1            pypi_0              pypi
protobuf                                    6.33.0           pypi_0              pypi
psutil                                      7.1.2            pypi_0              pypi
pycparser                                   2.23             pypi_0              pypi
pydantic                                    2.12.3           pypi_0              pypi
pydantic-core                               2.41.4           pypi_0              pypi
pydantic-settings                           2.11.0           pypi_0              pypi
pygments                                    2.19.2           pypi_0              pypi
pyjwt                                       2.10.1           pypi_0              pypi
pytest                                      8.4.2            pypi_0              pypi
python                                      3.10.19          h3c07f61_2_cpython  conda-forge
python-dotenv                               1.2.1            pypi_0              pypi
python-multipart                            0.0.20           pypi_0              pypi
pyyaml                                      6.0.3            pypi_0              pypi
readline                                    8.2              h8c095d6_2          conda-forge
referencing                                 0.37.0           pypi_0              pypi
regex                                       2025.10.23       pypi_0              pypi
requests                                    2.32.5           pypi_0              pypi
rpds-py                                     0.28.0           pypi_0              pypi
safetensors                                 0.6.2            pypi_0              pypi
setuptools                                  80.9.0           pyhff2d567_0        conda-forge
smmap                                       5.0.2            pypi_0              pypi
sniffio                                     1.3.1            pypi_0              pypi
sounddevice                                 0.5.3            pypi_0              pypi
sse-starlette                               3.0.2            pypi_0              pypi
starlette                                   0.48.0           pypi_0              pypi
sympy                                       1.14.0           pypi_0              pypi
tk                                          8.6.13           noxft_hd72426e_102  conda-forge
tokenizers                                  0.22.1           pypi_0              pypi
tomli                                       2.3.0            pypi_0              pypi
tqdm                                        4.67.1           pypi_0              pypi
transformers                                4.57.1           pypi_0              pypi
types-protobuf                              6.32.1.20250918  pypi_0              pypi
types-requests                              2.32.4.20250913  pypi_0              pypi
typing-extensions                           4.15.0           pypi_0              pypi
typing-inspection                           0.4.2            pypi_0              pypi
tzdata                                      2025b            h78e105d_0          conda-forge
urllib3                                     2.5.0            pypi_0              pypi
uvicorn                                     0.38.0           pypi_0              pypi
watchfiles                                  1.1.1            pypi_0              pypi
websockets                                  15.0.1           pypi_0              pypi
wheel                                       0.45.1           pyhd8ed1ab_1        conda-forge
yarl                                        1.22.0           pypi_0              pypi
zipp                                        3.23.0           pypi_0              pypi
zstd                                        1.5.7            hb8e6e7a_2          conda-forge


Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions