2025-08-18 13:08:53 -07:00
|
|
|
use anyhow::Context;
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use anyhow::anyhow;
|
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
use std::fs;
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
use ts_rs::TS;
|
|
|
|
|
|
|
|
|
|
const HEADER: &str = "// GENERATED CODE! DO NOT MODIFY BY HAND!\n\n";
|
|
|
|
|
|
|
|
|
|
pub fn generate_ts(out_dir: &Path, prettier: Option<&Path>) -> Result<()> {
|
|
|
|
|
ensure_dir(out_dir)?;
|
|
|
|
|
|
|
|
|
|
// Generate TS bindings
|
2025-09-09 19:32:57 -07:00
|
|
|
mcp_types::InitializeResult::export_all_to(out_dir)?;
|
2025-08-18 13:08:53 -07:00
|
|
|
codex_protocol::mcp_protocol::ConversationId::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::InputItem::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::ClientRequest::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::ServerRequest::export_all_to(out_dir)?;
|
fix: separate `codex mcp` into `codex mcp-server` and `codex app-server` (#4471)
This is a very large PR with some non-backwards-compatible changes.
Historically, `codex mcp` (or `codex mcp serve`) started a JSON-RPC-ish
server that had two overlapping responsibilities:
- Running an MCP server, providing some basic tool calls.
- Running the app server used to power experiences such as the VS Code
extension.
This PR aims to separate these into distinct concepts:
- `codex mcp-server` for the MCP server
- `codex app-server` for the "application server"
Note `codex mcp` still exists because it already has its own subcommands
for MCP management (`list`, `add`, etc.)
The MCP logic continues to live in `codex-rs/mcp-server` whereas the
refactored app server logic is in the new `codex-rs/app-server` folder.
Note that most of the existing integration tests in
`codex-rs/mcp-server/tests/suite` were actually for the app server, so
all the tests have been moved with the exception of
`codex-rs/mcp-server/tests/suite/mod.rs`.
Because this is already a large diff, I tried not to change more than I
had to, so `codex-rs/app-server/tests/common/mcp_process.rs` still uses
the name `McpProcess` for now, but I will do some mechanical renamings
to things like `AppServer` in subsequent PRs.
While `mcp-server` and `app-server` share some overlapping functionality
(like reading streams of JSONL and dispatching based on message types)
and some differences (completely different message types), I ended up
doing a bit of copypasta between the two crates, as both have somewhat
similar `message_processor.rs` and `outgoing_message.rs` files for now,
though I expect them to diverge more in the near future.
One material change is that of the initialize handshake for `codex
app-server`, as we no longer use the MCP types for that handshake.
Instead, we update `codex-rs/protocol/src/mcp_protocol.rs` to add an
`Initialize` variant to `ClientRequest`, which takes the `ClientInfo`
object we need to update the `USER_AGENT_SUFFIX` in
`codex-rs/app-server/src/message_processor.rs`.
One other material change is in
`codex-rs/app-server/src/codex_message_processor.rs` where I eliminated
a use of the `send_event_as_notification()` method I am generally trying
to deprecate (because it blindly maps an `EventMsg` into a
`JSONNotification`) in favor of `send_server_notification()`, which
takes a `ServerNotification`, as that is intended to be a custom enum of
all notification types supported by the app server. So to make this
update, I had to introduce a new variant of `ServerNotification`,
`SessionConfigured`, which is a non-backwards compatible change with the
old `codex mcp`, and clients will have to be updated after the next
release that contains this PR. Note that
`codex-rs/app-server/tests/suite/list_resume.rs` also had to be update
to reflect this change.
I introduced `codex-rs/utils/json-to-toml/src/lib.rs` as a small utility
crate to avoid some of the copying between `mcp-server` and
`app-server`.
2025-09-30 00:06:18 -07:00
|
|
|
codex_protocol::mcp_protocol::InitializeResponse::export_all_to(out_dir)?;
|
2025-08-18 13:08:53 -07:00
|
|
|
codex_protocol::mcp_protocol::NewConversationResponse::export_all_to(out_dir)?;
|
2025-09-09 11:06:59 -07:00
|
|
|
codex_protocol::mcp_protocol::ListConversationsResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::ResumeConversationResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::ArchiveConversationResponse::export_all_to(out_dir)?;
|
2025-08-18 13:08:53 -07:00
|
|
|
codex_protocol::mcp_protocol::AddConversationSubscriptionResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::RemoveConversationSubscriptionResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::SendUserMessageResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::SendUserTurnResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::InterruptConversationResponse::export_all_to(out_dir)?;
|
2025-08-22 13:10:11 -07:00
|
|
|
codex_protocol::mcp_protocol::GitDiffToRemoteResponse::export_all_to(out_dir)?;
|
2025-09-11 09:16:34 -07:00
|
|
|
codex_protocol::mcp_protocol::LoginApiKeyParams::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::LoginApiKeyResponse::export_all_to(out_dir)?;
|
2025-08-18 13:08:53 -07:00
|
|
|
codex_protocol::mcp_protocol::LoginChatGptResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::CancelLoginChatGptResponse::export_all_to(out_dir)?;
|
2025-08-22 13:10:11 -07:00
|
|
|
codex_protocol::mcp_protocol::LogoutChatGptResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::GetAuthStatusResponse::export_all_to(out_dir)?;
|
2025-08-18 13:08:53 -07:00
|
|
|
codex_protocol::mcp_protocol::ApplyPatchApprovalResponse::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::ExecCommandApprovalResponse::export_all_to(out_dir)?;
|
2025-09-29 12:19:09 -07:00
|
|
|
codex_protocol::mcp_protocol::FuzzyFileSearchParams::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::FuzzyFileSearchResult::export_all_to(out_dir)?;
|
|
|
|
|
codex_protocol::mcp_protocol::FuzzyFileSearchResponse::export_all_to(out_dir)?;
|
2025-09-04 16:26:41 -07:00
|
|
|
codex_protocol::mcp_protocol::GetUserSavedConfigResponse::export_all_to(out_dir)?;
|
2025-09-11 23:44:17 -07:00
|
|
|
codex_protocol::mcp_protocol::SetDefaultModelResponse::export_all_to(out_dir)?;
|
2025-09-08 10:30:13 -07:00
|
|
|
codex_protocol::mcp_protocol::GetUserAgentResponse::export_all_to(out_dir)?;
|
2025-09-10 17:03:35 -07:00
|
|
|
codex_protocol::mcp_protocol::UserInfoResponse::export_all_to(out_dir)?;
|
2025-09-09 11:06:59 -07:00
|
|
|
|
|
|
|
|
// All notification types reachable from this enum will be generated by
|
|
|
|
|
// induction, so they do not need to be listed individually.
|
2025-08-20 20:36:34 -07:00
|
|
|
codex_protocol::mcp_protocol::ServerNotification::export_all_to(out_dir)?;
|
fix: separate `codex mcp` into `codex mcp-server` and `codex app-server` (#4471)
This is a very large PR with some non-backwards-compatible changes.
Historically, `codex mcp` (or `codex mcp serve`) started a JSON-RPC-ish
server that had two overlapping responsibilities:
- Running an MCP server, providing some basic tool calls.
- Running the app server used to power experiences such as the VS Code
extension.
This PR aims to separate these into distinct concepts:
- `codex mcp-server` for the MCP server
- `codex app-server` for the "application server"
Note `codex mcp` still exists because it already has its own subcommands
for MCP management (`list`, `add`, etc.)
The MCP logic continues to live in `codex-rs/mcp-server` whereas the
refactored app server logic is in the new `codex-rs/app-server` folder.
Note that most of the existing integration tests in
`codex-rs/mcp-server/tests/suite` were actually for the app server, so
all the tests have been moved with the exception of
`codex-rs/mcp-server/tests/suite/mod.rs`.
Because this is already a large diff, I tried not to change more than I
had to, so `codex-rs/app-server/tests/common/mcp_process.rs` still uses
the name `McpProcess` for now, but I will do some mechanical renamings
to things like `AppServer` in subsequent PRs.
While `mcp-server` and `app-server` share some overlapping functionality
(like reading streams of JSONL and dispatching based on message types)
and some differences (completely different message types), I ended up
doing a bit of copypasta between the two crates, as both have somewhat
similar `message_processor.rs` and `outgoing_message.rs` files for now,
though I expect them to diverge more in the near future.
One material change is that of the initialize handshake for `codex
app-server`, as we no longer use the MCP types for that handshake.
Instead, we update `codex-rs/protocol/src/mcp_protocol.rs` to add an
`Initialize` variant to `ClientRequest`, which takes the `ClientInfo`
object we need to update the `USER_AGENT_SUFFIX` in
`codex-rs/app-server/src/message_processor.rs`.
One other material change is in
`codex-rs/app-server/src/codex_message_processor.rs` where I eliminated
a use of the `send_event_as_notification()` method I am generally trying
to deprecate (because it blindly maps an `EventMsg` into a
`JSONNotification`) in favor of `send_server_notification()`, which
takes a `ServerNotification`, as that is intended to be a custom enum of
all notification types supported by the app server. So to make this
update, I had to introduce a new variant of `ServerNotification`,
`SessionConfigured`, which is a non-backwards compatible change with the
old `codex mcp`, and clients will have to be updated after the next
release that contains this PR. Note that
`codex-rs/app-server/tests/suite/list_resume.rs` also had to be update
to reflect this change.
I introduced `codex-rs/utils/json-to-toml/src/lib.rs` as a small utility
crate to avoid some of the copying between `mcp-server` and
`app-server`.
2025-09-30 00:06:18 -07:00
|
|
|
codex_protocol::mcp_protocol::ClientNotification::export_all_to(out_dir)?;
|
2025-08-18 13:08:53 -07:00
|
|
|
|
2025-08-25 10:23:32 -07:00
|
|
|
generate_index_ts(out_dir)?;
|
|
|
|
|
|
2025-08-18 13:08:53 -07:00
|
|
|
// Prepend header to each generated .ts file
|
|
|
|
|
let ts_files = ts_files_in(out_dir)?;
|
|
|
|
|
for file in &ts_files {
|
|
|
|
|
prepend_header_if_missing(file)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format with Prettier by passing individual files (no shell globbing)
|
2025-08-19 13:22:02 -07:00
|
|
|
if let Some(prettier_bin) = prettier
|
|
|
|
|
&& !ts_files.is_empty()
|
|
|
|
|
{
|
|
|
|
|
let status = Command::new(prettier_bin)
|
|
|
|
|
.arg("--write")
|
|
|
|
|
.args(ts_files.iter().map(|p| p.as_os_str()))
|
|
|
|
|
.status()
|
|
|
|
|
.with_context(|| format!("Failed to invoke Prettier at {}", prettier_bin.display()))?;
|
|
|
|
|
if !status.success() {
|
|
|
|
|
return Err(anyhow!("Prettier failed with status {}", status));
|
2025-08-18 13:08:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ensure_dir(dir: &Path) -> Result<()> {
|
|
|
|
|
fs::create_dir_all(dir)
|
|
|
|
|
.with_context(|| format!("Failed to create output directory {}", dir.display()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn prepend_header_if_missing(path: &Path) -> Result<()> {
|
|
|
|
|
let mut content = String::new();
|
|
|
|
|
{
|
|
|
|
|
let mut f = fs::File::open(path)
|
|
|
|
|
.with_context(|| format!("Failed to open {} for reading", path.display()))?;
|
|
|
|
|
f.read_to_string(&mut content)
|
|
|
|
|
.with_context(|| format!("Failed to read {}", path.display()))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if content.starts_with(HEADER) {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut f = fs::File::create(path)
|
|
|
|
|
.with_context(|| format!("Failed to open {} for writing", path.display()))?;
|
|
|
|
|
f.write_all(HEADER.as_bytes())
|
|
|
|
|
.with_context(|| format!("Failed to write header to {}", path.display()))?;
|
|
|
|
|
f.write_all(content.as_bytes())
|
|
|
|
|
.with_context(|| format!("Failed to write content to {}", path.display()))?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ts_files_in(dir: &Path) -> Result<Vec<PathBuf>> {
|
|
|
|
|
let mut files = Vec::new();
|
|
|
|
|
for entry in
|
|
|
|
|
fs::read_dir(dir).with_context(|| format!("Failed to read dir {}", dir.display()))?
|
|
|
|
|
{
|
|
|
|
|
let entry = entry?;
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
if path.is_file() && path.extension() == Some(OsStr::new("ts")) {
|
|
|
|
|
files.push(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-25 10:23:32 -07:00
|
|
|
files.sort();
|
2025-08-18 13:08:53 -07:00
|
|
|
Ok(files)
|
|
|
|
|
}
|
2025-08-25 10:23:32 -07:00
|
|
|
|
|
|
|
|
/// Generate an index.ts file that re-exports all generated types.
|
|
|
|
|
/// This allows consumers to import all types from a single file.
|
|
|
|
|
fn generate_index_ts(out_dir: &Path) -> Result<PathBuf> {
|
|
|
|
|
let mut entries: Vec<String> = Vec::new();
|
|
|
|
|
let mut stems: Vec<String> = ts_files_in(out_dir)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|p| {
|
|
|
|
|
let stem = p.file_stem()?.to_string_lossy().into_owned();
|
|
|
|
|
if stem == "index" { None } else { Some(stem) }
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
stems.sort();
|
|
|
|
|
stems.dedup();
|
|
|
|
|
|
|
|
|
|
for name in stems {
|
|
|
|
|
entries.push(format!("export type {{ {name} }} from \"./{name}\";\n"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut content =
|
2025-09-22 20:30:16 +01:00
|
|
|
String::with_capacity(HEADER.len() + entries.iter().map(String::len).sum::<usize>());
|
2025-08-25 10:23:32 -07:00
|
|
|
content.push_str(HEADER);
|
|
|
|
|
for line in &entries {
|
|
|
|
|
content.push_str(line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let index_path = out_dir.join("index.ts");
|
|
|
|
|
let mut f = fs::File::create(&index_path)
|
|
|
|
|
.with_context(|| format!("Failed to create {}", index_path.display()))?;
|
|
|
|
|
f.write_all(content.as_bytes())
|
|
|
|
|
.with_context(|| format!("Failed to write {}", index_path.display()))?;
|
|
|
|
|
Ok(index_path)
|
|
|
|
|
}
|