Skip to content

feat(cli): Model Context Protocol (MCP) support — client, TUI overlay, config & basic test #824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

jagzmz
Copy link

@jagzmz jagzmz commented May 5, 2025

This PR integrates Model Context Protocol (MCP) into Codex CLI, letting any MCP server’s tools appear as agent‑callable functions.


✨ Key additions

Area Change
Runtime MCPClient & MCPManager manage connections, cache server metadata, flatten tools for OpenAI function‑calling, and clean up on exit.
UI (Ink/React) MCPProvider, useMcpManager hook, and mcp‑overlay panel show live server status & tool lists; /mcp slash‑command “List MCP servers”.
Config loading AgentLoop now accepts an optional mcpTools array; quiet‑mode path instantiates/initialises MCPManager.
Package deps Adds @modelcontextprotocol/sdk ^1.11.0; updates lock‑file.
Tests New Jest suites for manager initialisation, tool‑flattening, and adjusted TerminalChatInput signature.
Polish Warn earlier when context budget is > 25 %; TerminalChatInput callbacks simplified to return void.

🛠️ Example Configuration

Add an mcpServers block to ~/.codex/config.json
Each key is a friendly name; values mirror a spawn‑style process descriptor:

{
  // …existing config …
  "mcpServers": {
    "jina ai": {
      "command": "npx",
      "args": [
        "-y",
        "jina-ai-mcp-server"
      ],
      "env": {
        "JINA_API_KEY=xxx"          // <- your key here
      }
    },
    "memory": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-memory"
      ]
    }
  }
}

📸 Demo

Attached: TUI listing two live MCP servers

718120

Copy link

github-actions bot commented May 5, 2025

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

@jagzmz
Copy link
Author

jagzmz commented May 5, 2025

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

github-actions bot added a commit that referenced this pull request May 5, 2025
@jagzmz
Copy link
Author

jagzmz commented May 5, 2025

Tagging maintainers @tibo-openai @bolinfest for review. Thank you!

@bolinfest
Copy link
Collaborator

@jagzmz this is exciting stuff and obviously a complex change! Incidentally, I am actively working on making the analogous change in the Rust CLI: #829.

@bolinfest
Copy link
Collaborator

@jagzmz in your example, I see JINA_API_KEY=xxx in args, but shouldn't that be in env?

@jagzmz
Copy link
Author

jagzmz commented May 6, 2025

@jagzmz in your example, I see JINA_API_KEY=xxx in args, but shouldn't that be in env?

Yes, it should be, i picked up an old example i had with me : P, anyways, I have updated the example.

@jagzmz
Copy link
Author

jagzmz commented May 6, 2025

this is exciting stuff and obviously a complex change! Incidentally, I am actively working on making the analogous change in the Rust CLI:

Nice! Are new feature updates to the TS variant paused ?

jagzmz added 14 commits May 6, 2025 08:45
This commit introduces a new MCP (Model Context Protocol) management system within the Codex CLI. Key changes include:

- Added MCPClient and MCPManager classes for handling connections to MCP servers and managing tools.
- Implemented functionality to initialize MCP connections and retrieve tools from connected servers.
- Enhanced the CLI to support new commands for interacting with MCP servers, including a dedicated overlay for server status.
- Updated the configuration to allow users to define MCP servers and their connection details.
- Introduced new utility functions for flattening tools and parsing tool calls.

This feature enhances the extensibility of the Codex CLI by allowing it to interact with external tools via MCP, improving the overall user experience and functionality.
…ions

This commit introduces several improvements to the MCP (Model Context Protocol) integration within the Codex CLI:

- Added `MCPProvider` and `useMcpManager` hook for managing MCP connections and state in a React context.
- Refactored MCP client and manager functionalities to streamline server connections and tool retrieval.
- Implemented utility functions for sanitizing tool names and flattening tools from multiple MCP clients.
- Updated various components to utilize the new MCP context, enhancing the user interface with real-time server status and tool availability.

These changes improve the overall architecture and usability of the MCP features, allowing for a more seamless interaction with external tools.
Changed the description of the "/mcp" command from "Open MCP server selection panel" to "List MCP servers" to better represent its purpose in the Codex CLI.
- Changed the return type of `submitInput` and `onCompact` props from `CompletionResult` and `Promise<void>` to `void` for better clarity and consistency.
- Enhanced context display logic to provide clearer feedback when context is low, adjusting thresholds for color changes and messages.
- Updated tests to reflect changes in props and ensure proper functionality of the TerminalChatInput component.
- Updated the `disconnectAll` method in `MCPManager` to be asynchronous, allowing for graceful disconnection of all clients.
- Added a call to `mcpManager.disconnectAll()` in the `onExit` function to ensure all MCP clients are properly closed when the terminal exits.
- Modified the MCP client instantiation to include `stderr: 1` for better error handling during command execution.
…ing display

- Refactored the HelpOverlay to enhance the presentation of available commands and keyboard shortcuts.
- Consolidated command descriptions and added new commands for better user guidance.
- Improved layout and styling for clarity, including padding adjustments and color coding for commands and shortcuts.
- Updated exit instructions for consistency with the new design.
- Updated the HelpOverlay component to use non-breaking hyphens in the term "slash‑commands" for improved readability.
- Adjusted command descriptions to maintain consistency in formatting, ensuring clarity in the display of available commands.
- Replaced hyphens with en dashes in the HelpOverlay component for consistency and improved readability.
- Updated command descriptions to ensure uniform formatting across the help overlay.
…for MCP overlay

- Updated the `convertTools` function to set tool parameters to `undefined` if not provided, ensuring consistent behavior.
- Added `openMcpOverlay` to multiple test files to enhance coverage for MCP-related functionalities.
- Updated the `pnpm-lock.yaml` to ensure the correct resolution and engine compatibility for `[email protected]`.
- Removed the `package-lock.json` file from the `codex-cli` directory as it is no longer needed, streamlining the dependency management process.
- Updated the MCPClient constructor to merge the provided environment variables with the existing process environment, ensuring that all necessary variables are available during command execution.
- Updated the MCPClient constructor to inherit default environment variables using `getDefaultEnvironment()`, ensuring all necessary variables are available during command execution while still allowing for custom environment overrides.
- Changed the `stderr` option in the MCPClient constructor from `1` to `"pipe"` so the error is PassThrough'd and doesn't pollute the user interface
- Added an en dash separator to the terminal chat input instructions for improved readability and clarity in user guidance.
@jagzmz
Copy link
Author

jagzmz commented May 8, 2025

Hi @bolinfest , would appreciate a review on this pull request. Thanks!

I am wondering if new features are halted on the TS variant?

@bolinfest
Copy link
Collaborator

@jagzmz Hi, sorry for dragging our feet here: we're actively discussing how we want to balance TS versus Rust going forward (and to publish the plan).

That said, I think @fouad-openai was going to try to review a number of the TS PRs in once he gets a free moment...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants