Skip to content

Conversation

yaonyan
Copy link

@yaonyan yaonyan commented Oct 12, 2025

Summary

Implements MCP sampling/createMessage capability, allowing MCP servers to make independent LLM calls through Codex with their own prompts and model preferences.

Closes #4929, follows mcp sampling spec: https://modelcontextprotocol.io/specification/2025-06-18/client/sampling

Key Changes

New CodexSamplingHandler (codex-rs/core/src/mcp_sampling_handler.rs)

  • Creates independent LLM calls per sampling request
  • Respects request's systemPrompt and model_preferences.hints
  • Uses separate configuration from main Codex session

Integration:

  • chat_completions.rs: Skip system message when instructions are empty
  • rmcp-client: Added SamplingHandler trait for delegation
  • McpConnectionManager: Wires sampling handler and declares capability

Benefits

Write mini agents inside Codex using MCP tools. See this code review agent demo below - it can also run in background.

image image

Checks Passed

✅ cargo test -p codex-core
✅ just fix -p codex-core
✅ just fix -p codex-rmcp-client

- Change CodexSamplingHandler to store Config instead of ModelClient
- Create independent LLM calls for each sampling request
- Use sampling request's systemPrompt (or empty if not provided)
- Select model from model_preferences.hints if available
- Skip system message in chat completions when instructions are empty

Per MCP spec, sampling requests should use their own systemPrompt
and model preferences, not inherit from the session's configuration.

Fixes the issue where MCP sampling was using Codex's full agent
instructions instead of the server's systemPrompt.

Might close openai#4929
Copy link

github-actions bot commented Oct 12, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@yaonyan yaonyan changed the title Add MCP Sampling Support, closes #4929 Add MCP Sampling Support Oct 12, 2025
@yaonyan
Copy link
Author

yaonyan commented Oct 12, 2025

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Oct 12, 2025
@yaonyan yaonyan changed the title Add MCP Sampling Support Add MCP Sampling Support to Enable Agentic MCP Tools Oct 12, 2025
Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

@yaonyan
Copy link
Author

yaonyan commented Oct 12, 2025

An example MCP server (with sampling request) is required to test out the sampling feature. I will provide one for reference:

experimental_use_rmcp_client = true

[mcp_servers.code-reviewer]
args = [
    "-y",
    "@mcpc-tech/cli",
    "--config-url",
    "https://raw.githubusercontent.com/mcpc-tech/mcpc/refs/heads/main/packages/cli/examples/configs/code-reviewer.json",
]
command = "npx"
env = { "MCPC_TRACING_ENABLED" = "true" }
startup_timeout_sec = 36
tool_timeout_sec = 3600

@yaonyan
Copy link
Author

yaonyan commented Oct 17, 2025

@gpeal, would you mind taking a look here? This would make codex the first CLI to support MCP sampling.

@gpeal
Copy link
Collaborator

gpeal commented Oct 17, 2025

Thanks for your work here. Unfortunately, this isn't something we can accept at this time. Although there are many valid use cases for this, we don't yet have an appropriate way to prevent people from abusing this to turn their ChatGPT Codex credits into generic completions API credits. We will continue to think through alternatives here and may revisit this in the future.

@gpeal gpeal closed this Oct 17, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Oct 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add MCP Client Sampling Support to Codex

3 participants