@@ -2,13 +2,28 @@ package mcp
9+ "github.com/charmbracelet/crush/internal/config"
10+ "github.com/charmbracelet/crush/internal/env"
7 "github.com/modelcontextprotocol/go-sdk/mcp"
11 "github.com/modelcontextprotocol/go-sdk/mcp"
8 "github.com/stretchr/testify/require"
12 "github.com/stretchr/testify/require"
9 "go.uber.org/goleak"
13 "go.uber.org/goleak"
16+// shellResolverWithPath builds a shell resolver whose env carries PATH
17+// plus any caller-supplied overrides. Without PATH, $(cat), $(echo),
18+// etc. can't find their binaries in a test process where the shell env
19+// is otherwise empty.
20+func shellResolverWithPath(t *testing.T, overrides map[string]string) config.VariableResolver {
22+ m := map[string]string{"PATH": os.Getenv("PATH")}
23+ maps.Copy(m, overrides)
24+ return config.NewShellVariableResolver(env.NewFromMap(m))
12func TestMCPSession_CancelOnClose(t *testing.T) {
27func TestMCPSession_CancelOnClose(t *testing.T) {
13 defer goleak.VerifyNone(t)
28 defer goleak.VerifyNone(t)
@@ -36,3 +51,379 @@ func TestMCPSession_CancelOnClose(t *testing.T) {
36 // After Close, the context must be cancelled.
51 // After Close, the context must be cancelled.
37 require.ErrorIs(t, ctx.Err(), context.Canceled)
52 require.ErrorIs(t, ctx.Err(), context.Canceled)
55+// TestCreateTransport_URLResolution pins that m.URL goes through the
56+// same resolver seam as command, args, env, and headers. Covers both
57+// the HTTP and SSE branches, success and failure, so a regression in
58+// ResolvedURL wiring is caught at the transport layer rather than only
59+// at the config layer.
60+func TestCreateTransport_URLResolution(t *testing.T) {
63+ shell := config.NewShellVariableResolver(env.NewFromMap(map[string]string{
64+ "MCP_HOST": "mcp.example.com",
67+ t.Run("http success expands $VAR", func(t *testing.T) {
69+ m := config.MCPConfig{
70+ Type: config.MCPHttp,
71+ URL: "https://$MCP_HOST/api",
73+ tr, err := createTransport(t.Context(), m, shell)
74+ require.NoError(t, err)
75+ require.NotNil(t, tr)
76+ sct, ok := tr.(*mcp.StreamableClientTransport)
77+ require.True(t, ok, "expected StreamableClientTransport, got %T", tr)
78+ require.Equal(t, "https://mcp.example.com/api", sct.Endpoint)
81+ t.Run("sse success expands $(cmd)", func(t *testing.T) {
83+ m := config.MCPConfig{
85+ URL: "https://$(echo mcp.example.com)/events",
87+ tr, err := createTransport(t.Context(), m, shell)
88+ require.NoError(t, err)
89+ sse, ok := tr.(*mcp.SSEClientTransport)
90+ require.True(t, ok, "expected SSEClientTransport, got %T", tr)
91+ require.Equal(t, "https://mcp.example.com/events", sse.Endpoint)
94+ t.Run("http unset var surfaces error, no transport created", func(t *testing.T) {
96+ m := config.MCPConfig{
97+ Type: config.MCPHttp,
98+ URL: "https://$MCP_MISSING_HOST/api",
100+ tr, err := createTransport(t.Context(), m, shell)
101+ require.Error(t, err)
103+ require.Contains(t, err.Error(), "url:")
104+ require.Contains(t, err.Error(), "$MCP_MISSING_HOST")
107+ t.Run("sse failing $(cmd) surfaces error, no transport created", func(t *testing.T) {
109+ m := config.MCPConfig{
110+ Type: config.MCPSSE,
111+ URL: "https://$(false)/events",
113+ tr, err := createTransport(t.Context(), m, shell)
114+ require.Error(t, err)
116+ require.Contains(t, err.Error(), "url:")
117+ require.Contains(t, err.Error(), "$(false)")
120+ t.Run("http empty-after-resolve still fails the non-empty guard", func(t *testing.T) {
122+ // ${MCP_EMPTY:-} resolves to the empty string (no error),
123+ // then the existing TrimSpace guard in createTransport must
124+ // reject it so we never spawn a transport against "".
125+ m := config.MCPConfig{
126+ Type: config.MCPHttp,
127+ URL: "${MCP_EMPTY:-}",
129+ tr, err := createTransport(t.Context(), m, shell)
130+ require.Error(t, err)
132+ require.Contains(t, err.Error(), "non-empty 'url'")
135+ t.Run("identity resolver round-trips template verbatim", func(t *testing.T) {
137+ // Client mode forwards the template to the server; no local
138+ // expansion, no error on unset vars.
139+ tmpl := "https://$MCP_MISSING_HOST/api"
140+ m := config.MCPConfig{Type: config.MCPHttp, URL: tmpl}
141+ tr, err := createTransport(t.Context(), m, config.IdentityResolver())
142+ require.NoError(t, err)
143+ sct, ok := tr.(*mcp.StreamableClientTransport)
145+ require.Equal(t, tmpl, sct.Endpoint)
149+// TestCreateTransport_StdioResolution pins that command, args, and env
150+// for stdio MCPs go through the same resolver seam as the other
151+// transports. Covers both success (expansion produced the expected
152+// exec.Cmd) and failure (any one field erroring prevents transport
154+func TestCreateTransport_StdioResolution(t *testing.T) {
157+ t.Run("success expands command, args, and env", func(t *testing.T) {
159+ r := shellResolverWithPath(t, map[string]string{
160+ "MY_TOKEN": "hunter2",
162+ m := config.MCPConfig{
163+ Type: config.MCPStdio,
164+ Command: "forgejo-mcp",
165+ Args: []string{"--token", "$MY_TOKEN", "--host", "$(echo example.com)"},
166+ Env: map[string]string{
167+ "SECRET": "$(echo shh)",
169+ "REFERENCE": "$MY_TOKEN",
172+ tr, err := createTransport(t.Context(), m, r)
173+ require.NoError(t, err)
174+ require.NotNil(t, tr)
176+ ct, ok := tr.(*mcp.CommandTransport)
177+ require.True(t, ok, "expected CommandTransport, got %T", tr)
179+ // exec.Cmd.Args[0] is the command name; the rest are positional
181+ require.Equal(t, []string{"forgejo-mcp", "--token", "hunter2", "--host", "example.com"}, ct.Command.Args)
183+ // Env is os.Environ() + resolved entries (sorted). Check the
184+ // resolved entries are present with their expanded values.
185+ require.Contains(t, ct.Command.Env, "SECRET=shh")
186+ require.Contains(t, ct.Command.Env, "PLAIN=literal")
187+ require.Contains(t, ct.Command.Env, "REFERENCE=hunter2")
190+ t.Run("env resolution failure surfaces error, no transport created", func(t *testing.T) {
192+ r := shellResolverWithPath(t, nil)
193+ m := config.MCPConfig{
194+ Type: config.MCPStdio,
195+ Command: "forgejo-mcp",
196+ Env: map[string]string{"TOKEN": "$(false)"},
198+ tr, err := createTransport(t.Context(), m, r)
199+ require.Error(t, err)
201+ require.Contains(t, err.Error(), "env TOKEN")
204+ t.Run("unset env var is a hard error, not silent empty", func(t *testing.T) {
206+ // The regression at the heart of PLAN.md: unset vars used to
207+ // silently substitute "" and hand an empty credential to the
208+ // child process. Now they must error out before exec.
209+ r := shellResolverWithPath(t, nil)
210+ m := config.MCPConfig{
211+ Type: config.MCPStdio,
212+ Command: "forgejo-mcp",
213+ Env: map[string]string{"FORGEJO_ACCESS_TOKEN": "$FORGJO_TOKEN"},
215+ tr, err := createTransport(t.Context(), m, r)
216+ require.Error(t, err)
218+ require.Contains(t, err.Error(), "env FORGEJO_ACCESS_TOKEN")
221+ t.Run("args resolution failure surfaces error, no transport created", func(t *testing.T) {
223+ r := shellResolverWithPath(t, nil)
224+ m := config.MCPConfig{
225+ Type: config.MCPStdio,
226+ Command: "forgejo-mcp",
227+ Args: []string{"--token", "$(false)"},
229+ tr, err := createTransport(t.Context(), m, r)
230+ require.Error(t, err)
232+ require.Contains(t, err.Error(), "arg 1")
235+ t.Run("command resolution failure surfaces error, no transport created", func(t *testing.T) {
237+ r := shellResolverWithPath(t, nil)
238+ m := config.MCPConfig{
239+ Type: config.MCPStdio,
240+ Command: "$(false)",
242+ tr, err := createTransport(t.Context(), m, r)
243+ require.Error(t, err)
245+ require.Contains(t, err.Error(), "invalid mcp command")
248+ t.Run("identity resolver round-trips templates verbatim", func(t *testing.T) {
250+ // Client mode: no local expansion, no error on unset vars.
251+ m := config.MCPConfig{
252+ Type: config.MCPStdio,
253+ Command: "forgejo-mcp",
254+ Args: []string{"--token", "$MCP_MISSING"},
255+ Env: map[string]string{"TOKEN": "$(vault read -f token)"},
257+ tr, err := createTransport(t.Context(), m, config.IdentityResolver())
258+ require.NoError(t, err)
259+ ct, ok := tr.(*mcp.CommandTransport)
261+ require.Equal(t, []string{"forgejo-mcp", "--token", "$MCP_MISSING"}, ct.Command.Args)
262+ require.Contains(t, ct.Command.Env, "TOKEN=$(vault read -f token)")
266+// TestCreateTransport_HeadersResolution pins that a single failing
267+// header aborts HTTP/SSE transport creation and that the successful
268+// resolver passes every expanded header through to the round tripper.
269+func TestCreateTransport_HeadersResolution(t *testing.T) {
272+ t.Run("http headers success expands $(cmd)", func(t *testing.T) {
274+ r := shellResolverWithPath(t, map[string]string{
275+ "GITHUB_TOKEN": "gh-secret",
277+ m := config.MCPConfig{
278+ Type: config.MCPHttp,
279+ URL: "https://mcp.example.com/api",
280+ Headers: map[string]string{
281+ "Authorization": "$(echo Bearer $GITHUB_TOKEN)",
285+ tr, err := createTransport(t.Context(), m, r)
286+ require.NoError(t, err)
288+ sct, ok := tr.(*mcp.StreamableClientTransport)
290+ rt, ok := sct.HTTPClient.Transport.(*headerRoundTripper)
291+ require.True(t, ok, "expected headerRoundTripper, got %T", sct.HTTPClient.Transport)
292+ require.Equal(t, map[string]string{
293+ "Authorization": "Bearer gh-secret",
298+ t.Run("http failing header surfaces error, no transport", func(t *testing.T) {
300+ r := shellResolverWithPath(t, nil)
301+ m := config.MCPConfig{
302+ Type: config.MCPHttp,
303+ URL: "https://mcp.example.com/api",
304+ Headers: map[string]string{"Authorization": "$(false)"},
306+ tr, err := createTransport(t.Context(), m, r)
307+ require.Error(t, err)
309+ require.Contains(t, err.Error(), "header Authorization")
312+ t.Run("sse failing header surfaces error, no transport", func(t *testing.T) {
314+ r := shellResolverWithPath(t, nil)
315+ m := config.MCPConfig{
316+ Type: config.MCPSSE,
317+ URL: "https://mcp.example.com/events",
318+ Headers: map[string]string{"Authorization": "Bearer $MISSING_TOKEN"},
320+ tr, err := createTransport(t.Context(), m, r)
321+ require.Error(t, err)
323+ require.Contains(t, err.Error(), "header Authorization")
324+ require.Contains(t, err.Error(), "$MISSING_TOKEN")
328+// TestCreateSession_ResolutionFailureUpdatesState pins the user-visible
329+// half of the regression fix: when any of command/args/env/headers/url
330+// fails to resolve, createSession must publish StateError to the state
331+// map so crush_info and the TUI's MCP status card can render a real
332+// error instead of the MCP silently sitting in "starting" or being
333+// spawned with an empty credential.
335+// These subtests cannot run in parallel: `states` is a package-level
336+// csync.Map and each assertion reads the entry written by the call
337+// under test. They do use unique MCP names per subtest to keep them
338+// independent regardless of ordering.
339+func TestCreateSession_ResolutionFailureUpdatesState(t *testing.T) {
340+ r := shellResolverWithPath(t, nil)
345+ cfg config.MCPConfig
346+ wantErrContains string
349+ name: "stdio env failure",
350+ mcpName: "test-stdio-env-fail",
351+ cfg: config.MCPConfig{
352+ Type: config.MCPStdio,
354+ Env: map[string]string{"FORGEJO_ACCESS_TOKEN": "$(false)"},
356+ wantErrContains: "env FORGEJO_ACCESS_TOKEN",
359+ name: "stdio args failure",
360+ mcpName: "test-stdio-args-fail",
361+ cfg: config.MCPConfig{
362+ Type: config.MCPStdio,
364+ Args: []string{"--token", "$MCP_UNSET_TOKEN"},
366+ wantErrContains: "arg 1",
369+ name: "http url failure",
370+ mcpName: "test-http-url-fail",
371+ cfg: config.MCPConfig{
372+ Type: config.MCPHttp,
373+ URL: "https://$MCP_MISSING_HOST/api",
375+ wantErrContains: "url:",
378+ name: "http header failure",
379+ mcpName: "test-http-header-fail",
380+ cfg: config.MCPConfig{
381+ Type: config.MCPHttp,
382+ URL: "https://mcp.example.com/api",
383+ Headers: map[string]string{"Authorization": "$(false)"},
385+ wantErrContains: "header Authorization",
388+ name: "sse url failure",
389+ mcpName: "test-sse-url-fail",
390+ cfg: config.MCPConfig{
391+ Type: config.MCPSSE,
392+ URL: "https://$(false)/events",
394+ wantErrContains: "url:",
397+ name: "sse header failure",
398+ mcpName: "test-sse-header-fail",
399+ cfg: config.MCPConfig{
400+ Type: config.MCPSSE,
401+ URL: "https://mcp.example.com/events",
402+ Headers: map[string]string{"Authorization": "Bearer $MISSING_SSE_TOKEN"},
404+ wantErrContains: "header Authorization",
408+ for _, tc := range tests {
409+ t.Run(tc.name, func(t *testing.T) {
410+ // Guarantee a clean slate on the shared state map so a
411+ // stale entry from another test can't satisfy the
413+ states.Del(tc.mcpName)
414+ t.Cleanup(func() { states.Del(tc.mcpName) })
416+ sess, err := createSession(t.Context(), tc.mcpName, tc.cfg, r)
417+ require.Error(t, err)
418+ require.Nil(t, sess)
419+ require.Contains(t, err.Error(), tc.wantErrContains)
421+ info, ok := GetState(tc.mcpName)
422+ require.True(t, ok, "state entry must be written for %q", tc.mcpName)
423+ require.Equal(t, StateError, info.State, "expected StateError, got %s", info.State)
424+ require.Error(t, info.Error, "state must carry the failure error")
425+ require.Contains(t, info.Error.Error(), tc.wantErrContains)
426+ require.Nil(t, info.Client, "no client session on failure")