Files
magpie/internal/library/codex_transport_keys_test.go
T
yetone 7625481b9e library: a Codex MCP entry never keeps the other transport's keys
kwanyu (Discord #help, 11-06): with Codex connected to magpie, the
ChatGPT/Codex app wouldn't start: "config.toml:209:1: env_vars is not
supported for streamable_http". Codex refuses a command's keys (args,
env, env_vars, cwd) beside a url, and a url's (bearer_token_env_var,
http_headers, env_http_headers, http_headers_helper, oauth,
oauth_resource, auth) beside a command (codex-rs/config/src/
mcp_types.rs, TryFrom RawMcpServerConfig).

merged kept every old key magpie doesn't own. env_vars is magpie's only
while the server references a variable in its env, so a server changed
from a command to a URL (in the Library, or one imported from the
user's own stdio entry) kept its env_vars and cwd under the new url.
The converse kept bearer_token_env_var and env_http_headers under a
command. The sync's in-step check decodes neither key, so an entry
already broken was never written again.

Now codexRefused names the other transport's keys. merged drops them,
and behind reports an entry holding one, so the next sync (and a
project's .codex/config.toml) rewrites it. The user's keys of the
entry's own transport stay (enabled_tools, tool_timeout_sec, scopes, a
stdio entry's own env_vars). bearer_token, which Codex refuses for both
transports, is left as the user wrote it.

Run (mote): go test ./internal/library -count=1 ok (77.8s); GOOS=linux
and GOOS=windows go vet ./internal/library ok; gofmt -l clean.
Broken by hand: the four new tests against the old mcp.go are all red
(env_vars/cwd kept under a URL; bearer_token_env_var, env_http_headers
and oauth_resource under a command; a broken entry left in step; a
project entry read as in step).
Real Codex (codex-cli 0.165.0, sandbox CODEX_HOME, codex mcp list): a
url + env_vars entry fails with the reporter's exact error. The three
config.toml files the new tests write load and list their servers.
Not tried: the reporter's own config.toml (we only have the error line);
the ChatGPT desktop app itself; Grok's config (written by the same TOML
writer, its own transport rules not checked); a full suite before push
(runs after).
2026-11-06 02:04:53 +08:00

136 lines
5.6 KiB
Go

package library
import (
"path/filepath"
"slices"
"strings"
"testing"
)
// codexLoads fails the test when Codex would refuse the entry: a key of
// the other transport beside a command or a URL (codex-rs/config/src/
// mcp_types.rs, TryFrom RawMcpServerConfig).
func codexLoads(t *testing.T, cfg, name string) map[string]any {
t.Helper()
e := codexEntry(t, cfg, name)
if e == nil {
t.Fatalf("no %s in:\n%s", name, read(t, cfg))
}
stdio := []string{"args", "env", "env_vars", "cwd"}
remote := []string{"url", "bearer_token_env_var", "http_headers", "env_http_headers", "http_headers_helper", "oauth", "oauth_resource", "auth"}
_, cmd := e["command"]
for k := range e {
if cmd && slices.Contains(remote, k) && k != "url" || !cmd && slices.Contains(stdio, k) {
t.Errorf("Codex refuses %s here:\n%s", k, read(t, cfg))
}
}
return e
}
// A server that was a command passing a variable on (env_vars), with a
// folder the user gave it (cwd), and is changed to a URL is written to
// Codex with neither: Codex won't load a config.toml with "env_vars is
// not supported for streamable_http", and the ChatGPT app won't start
// (kwanyu, Discord #help, 11-06). The user's keys both transports take
// stay.
func TestCodexServerMadeRemoteLeavesCommandKeys(t *testing.T) {
h := sandbox(t)
cfg := filepath.Join(h, ".codex/config.toml")
srv := Server{Name: "docs", Transport: "stdio", Command: "npx", Args: []string{"-y", "docs-mcp"},
Env: map[string]string{"DOCS_TOKEN": "${DOCS_TOKEN}", "MODE": "fast"}, Agents: []string{"codex"}}
ok(t)(SaveServer("", srv))
s := read(t, cfg)
if !strings.Contains(s, "env_vars") {
t.Fatalf("no env_vars for the referenced variable:\n%s", s)
}
// the user's own: a folder, and the tools it may call
write(t, cfg, strings.Replace(s, "command = ", "cwd = \"/srv\"\nenabled_tools = [\"search\"]\ncommand = ", 1))
srv.Transport, srv.Command, srv.Args, srv.Env = "http", "", nil, nil
srv.URL = "https://docs.example/mcp"
ok(t)(SaveServer("docs", srv))
e := codexLoads(t, cfg, "docs")
if e["url"] != srv.URL {
t.Errorf("url = %v:\n%s", e["url"], read(t, cfg))
}
if got, _ := e["enabled_tools"].([]any); len(got) != 1 || got[0] != "search" {
t.Errorf("the user's enabled_tools went: %v\n%s", e["enabled_tools"], read(t, cfg))
}
}
// The converse: a URL with a bearer token from a variable, a header and
// the user's own OAuth settings, changed to a command, is written without
// any key Codex takes only for a URL.
func TestCodexServerMadeLocalLeavesURLKeys(t *testing.T) {
h := sandbox(t)
cfg := filepath.Join(h, ".codex/config.toml")
srv := Server{Name: "web", Transport: "http", URL: "https://x.example/mcp",
Headers: map[string]string{"Authorization": "Bearer ${WEB_TOKEN}", "X-Key": "${WEB_KEY}", "X-Team": "a"}, Agents: []string{"codex"}}
ok(t)(SaveServer("", srv))
s := read(t, cfg)
for _, w := range []string{"bearer_token_env_var", "env_http_headers", "http_headers"} {
if !strings.Contains(s, w) {
t.Fatalf("no %s:\n%s", w, s)
}
}
write(t, cfg, strings.Replace(s, "url = ", "oauth_resource = \"https://x.example\"\nscopes = [\"read\"]\ntool_timeout_sec = 90\nurl = ", 1))
srv.Transport, srv.URL, srv.Headers = "stdio", "", nil
srv.Command, srv.Args = "web-mcp", []string{"--stdio"}
ok(t)(SaveServer("web", srv))
e := codexLoads(t, cfg, "web")
if e["command"] != "web-mcp" || e["url"] != nil {
t.Errorf("not a command:\n%s", read(t, cfg))
}
if !isNumber(e["tool_timeout_sec"], 90) {
t.Errorf("the user's tool_timeout_sec went:\n%s", read(t, cfg))
}
if got, _ := e["scopes"].([]any); len(got) != 1 {
t.Errorf("scopes, which Codex takes for both, went:\n%s", read(t, cfg))
}
}
// An entry magpie wrote before with a command's keys beside its URL, the
// reporter's shape, is written again on the next sync, though the server
// itself reads as unchanged; one of a project's .codex/config.toml too.
// A stdio entry's own env_vars, which the user gave it, stays.
func TestCodexEntryWithOtherTransportKeysRepaired(t *testing.T) {
h := sandbox(t)
cfg := filepath.Join(h, ".codex/config.toml")
ok(t)(SaveServer("", Server{Name: "remote", Transport: "http", URL: "https://r.example/mcp", Agents: []string{"codex"}}))
ok(t)(SaveServer("", Server{Name: "local", Transport: "stdio", Command: "npx", Args: []string{"-y", "l"}, Agents: []string{"codex"}}))
s := read(t, cfg)
s = strings.Replace(s, "url = ", "env_vars = [\"HOME_TOKEN\", { name = \"T\", source = \"local\" }]\nargs = []\nenabled_tools = [\"a\"]\nurl = ", 1)
s = strings.Replace(s, "command = ", "env_vars = [\"MINE\"]\nbearer_token_env_var = \"X\"\ncommand = ", 1)
write(t, cfg, s)
ok(t)(Sync())
r := codexLoads(t, cfg, "remote")
if got, _ := r["enabled_tools"].([]any); len(got) != 1 {
t.Errorf("the user's enabled_tools went:\n%s", read(t, cfg))
}
l := codexLoads(t, cfg, "local")
if got, _ := l["env_vars"].([]any); len(got) != 1 || got[0] != "MINE" {
t.Errorf("the user's env_vars on a command went: %v\n%s", l["env_vars"], read(t, cfg))
}
}
// A project's .codex/config.toml is held to the same: a server made remote
// there leaves its command's keys.
func TestCodexProjectEntryLeavesCommandKeys(t *testing.T) {
f := &mcpFile{Format: fmtCodex, Path: filepath.Join(t.TempDir(), ".codex", "config.toml")}
write(t, f.Path, "[mcp_servers.p]\ncommand = \"npx\"\nargs = []\ncwd = \"/w\"\nenv_vars = [\"P_TOKEN\"]\n")
es, err := f.entries()
if err != nil {
t.Fatal(err)
}
s := &Server{Name: "p", Transport: "http", URL: "https://p.example/mcp"}
if !f.behind(s, es["p"]) {
t.Error("an entry with a command's keys beside a URL reads as in step")
}
if err := f.put(s, es["p"]); err != nil {
t.Fatal(err)
}
codexLoads(t, f.Path, "p")
}