Skip to content

MCP Integration

Codex uses the Model Context Protocol (MCP) to discover tools provided by a server and request actions from that server. This exercise adds a single tool, score_rule, that returns the constraint used in Your First Session. You can test it without sending a prompt to a model. Official Codex MCP guide

Save this example as score-rules.py in the scratch repo. It uses the Python 3 standard library. Codex starts the process and communicates with one JSON-RPC message per line on stdin and stdout. The server advertises its tool during discovery and returns a fixed result when called. MCP stdio transport · MCP tool methods

import json
import sys
TOOL = {
"name": "score_rule",
"description": "Return the allowed score range.",
"inputSchema": {"type": "object", "properties": {}, "additionalProperties": False},
"annotations": {"readOnlyHint": True},
}
for line in sys.stdin:
request = json.loads(line)
if "id" not in request:
continue
method = request["method"]
params = request.get("params", {})
reply = {"jsonrpc": "2.0", "id": request["id"]}
if method == "initialize":
reply["result"] = {
"protocolVersion": "2025-11-25",
"capabilities": {"tools": {}},
"serverInfo": {"name": "score-rules", "version": "1.0.0"},
}
elif method == "ping":
reply["result"] = {}
elif method == "tools/list":
reply["result"] = {"tools": [TOOL]}
elif method == "tools/call":
if params.get("name") == TOOL["name"] and params.get("arguments", {}) == {}:
reply["result"] = {
"content": [{"type": "text", "text": "Valid scores are in [0, 1], inclusive."}]
}
else:
reply["error"] = {"code": -32602, "message": "Expected score_rule with no arguments"}
else:
reply["error"] = {"code": -32601, "message": "Method not found"}
print(json.dumps(reply), flush=True)

The example returns a constant and performs no file or network operations. Use the implementation to check the effect of a call. The readOnlyHint annotation communicates the server’s intent to clients. MCP tool annotations

From the scratch repository, create a temporary Codex home, then register the server using an absolute script path.

Terminal window
score_rules_home="$(mktemp -d "${TMPDIR:-/tmp}/codex-mcp.XXXXXX")" &&
CODEX_HOME="$score_rules_home" codex mcp add score_rules -- python3 "$PWD/score-rules.py" &&
CODEX_HOME="$score_rules_home" codex mcp list --json

Look for an entry named score_rules. That verifies the saved configuration. Codex may report that it skipped installing PATH helpers when its home is under the system temp directory. This exercise runs the server over stdio and does not use those helpers. Temporary directory check

To check that Codex can start the process and discover its tools, run the following App Server probe in the same shell. This interface is version-specific. The messages below were checked against Codex CLI 0.172.0. CLI MCP commands · App Server MCP request types

Terminal window
CODEX_HOME="$score_rules_home" codex app-server --listen stdio://

Send each line in order, waiting for the reply to request 1 before sending the notification. In the reply to request 2, find the score_rules entry in result.data and inspect its tools.score_rule definition. Status result

{"id":1,"method":"initialize","params":{"clientInfo":{"name":"handbook","version":"1.0.0"},"capabilities":{"experimentalApi":true}}}
{"method":"initialized"}
{"id":2,"method":"mcpServerStatus/list","params":{"serverName":"score_rules","detail":"toolsAndAuthOnly"}}
{"id":3,"method":"thread/start","params":{"ephemeral":true}}

Copy result.thread.id from the reply to request 3, substitute it for THREAD_ID, and send the call. Read result.content[0].text and check for Valid scores are in [0, 1], inclusive.

{"id":4,"method":"mcpServer/tool/call","params":{"threadId":"THREAD_ID","server":"score_rules","tool":"score_rule","arguments":{}}}

Press Ctrl+C to exit App Server.

Observation Next check
score_rules is missing from codex mcp list Check that registration and listing used the same temporary CODEX_HOME.
The server is listed and score_rule is missing Inspect the launcher path or tool discovery error. The server’s stdout is reserved for protocol messages. Stdio requirements
Discovery works and the call fails Reuse the returned thread ID and send the exact server and tool names with empty arguments. Direct call parameters

The probe directly asks App Server to call the tool. During a model-driven turn, an exposed MCP tool enters the harness through the tool router. McpHandler binds the invocation to the server, passes its arguments to the MCP call path, and wraps the result for the runtime. The server’s JSON-RPC request ID identifies a protocol exchange. The model’s call ID identifies the tool result in its conversation. Model-facing MCP handler · Dispatch and result wrapper · Tool result IDs

Evidence. Codex CLI 0.172.0 registered the example, listed score_rule through App Server, and returned the expected text when called by name. The model-driven path above is traced from source. Direct call parameters

Continue with Model Requests and Streaming to see how a provider response enters the same runtime.