Skip to content

fix(cli): pass non-nil context through namespace tool dispatch#85

Open
chiplay wants to merge 1 commit into
mainfrom
fix/cli-nil-context
Open

fix(cli): pass non-nil context through namespace tool dispatch#85
chiplay wants to merge 1 commit into
mainfrom
fix/cli-nil-context

Conversation

@chiplay
Copy link
Copy Markdown
Collaborator

@chiplay chiplay commented May 30, 2026

Problem

In the shipped Go CLI (@subtextdev/subtext-cli@1.0.0), every tool invocation across every namespace fails:

$ subtext doc list
error: fetching tool: net/http: nil Context
$ subtext live view-list --help
error: net/http: nil Context

Only version, auth whoami, and namespace-level --help work. The CLI cannot call a single MCP tool — its core purpose is non-functional.

Root cause

Namespace commands (live/comment/doc/artifact) use DisableFlagParsing and delegate by calling callCmd.RunE(callCmd, …) directly (namespaces.go), never through cobra's Execute. So callCmd's context is never set. runCall then passes cmd.Context()nil — into c.GetTool/c.CallToolhttp.NewRequestWithContext(nil, …)net/http: nil Context.

auth whoami and namespace listing survived only because they call ListTools(context.Background()) explicitly (help.go).

Fix

  • namespaces.go: propagate the namespace command's context to callCmd before delegating.
  • call.go: guard runCall with a context.Background() fallback and use it at all three call sites, so the HTTP request builders never receive a nil context (defense-in-depth; also makes runCall correct when called directly).

Test

Added integration_test.go — drives the real namespace dispatcher (namespaceRunEcallCmd) against a stub httptest MCP server, exercising the cmd.Context()→HTTP path the stubbed unit tests never reached. Verified it fails with the exact net/http: nil Context error on unpatched code and passes with the fix.

Verification

go build ./... + go test ./... clean. Locally-built binary verified against the production na1 endpoint:

  • doc list returns real proof docs ✅
  • per-tool --help fetches schemas ✅
  • live connectscreenshotartifact uploadcomment adddisconnect full round-trip ✅
  • version / auth whoami / namespace --help unaffected ✅

Warrants a v1.0.1 patch release once merged.

🤖 Generated with Claude Code


Note

Low Risk
Small, localized CLI dispatch fix with a regression test; no auth or server changes.

Overview
Fixes broken MCP tool calls for namespace commands (doc, live, comment, artifact). Those paths delegate to internal callCmd without going through Cobra Execute, so callCmd had a nil context and every GetTool / CallTool failed with net/http: nil Context.

namespaceRunE now copies the namespace command’s context onto callCmd before delegating. runCall also normalizes context (uses context.Background() when still nil) and routes help, schema fetch, and tool invocation through that ctx.

Adds TestNamespaceDispatchCallsTool, an httptest integration test that runs namespaceRunEcallCmd and asserts a full tools/list + tools/call round trip—covering the path unit tests did not hit.

Reviewed by Cursor Bugbot for commit 6084902. Bugbot is set up for automated code reviews on this repo. Configure here.

Namespace commands (live/comment/doc/artifact) delegate to callCmd by
invoking its RunE directly rather than through cobra's Execute, so
callCmd's context is never populated. runCall then passed cmd.Context()
— nil — into http.NewRequestWithContext, producing "net/http: nil
Context" on every tool invocation and every per-tool --help. Only
version, auth whoami, and namespace-level --help (which use
context.Background() explicitly) worked.

Propagate the namespace command's context to callCmd before delegating,
and guard runCall with a context.Background() fallback so the HTTP
request builders never receive a nil context.

Add an integration test that drives the real namespace dispatcher
against a stub MCP server — the path the stubbed unit tests never
exercised. It fails with the exact nil-context error on unpatched code
and passes with the fix.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@chiplay
Copy link
Copy Markdown
Collaborator Author

chiplay commented May 30, 2026

@jurassix I tried using the CLI 1.0.0 and hit this issue. Can you take a look next week and see if this is the right fix?

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