Skip to content

Add an MCP initialize readiness check - #6374

Merged
ChrisJBurns merged 6 commits into
stacklok:mainfrom
Whxuan0701:pr/mcp-initialize-check
Nov 26, 2026
Merged

ChrisJBurns merged 6 commits into
stacklok:mainfrom
Whxuan0701:pr/mcp-initialize-check

Conversation

@Whxuan0701

@Whxuan0701 Whxuan0701 commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Agent harnesses and CI need a protocol-level readiness signal, but thv mcp list performs capability enumeration after initialize and is unsuitable for servers that intentionally expose no tools, resources, or prompts. A lightweight preflight should validate the MCP session handshake itself.

  • Add thv mcp check with auto, SSE, and streamable HTTP transport support.
  • Perform only transport setup, initialize, and connection close.
  • Report negotiated protocol/server/capability metadata as text or JSON and return a non-zero exit code on failure.
  • Keep check-specific flags isolated from sibling MCP commands and route output through Cobra writers.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task -t Taskfile.focused.yml test, using the repository's -race and linker settings for ./pkg/mcp/client and ./cmd/thv/app)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Regenerated all CLI, Swagger, and Helm documentation with task docs, then verified the CLI documentation byte-for-byte against a fresh go run cmd/help/main.go output directory.

API Compatibility

  • This PR does not break the v1beta1 API.

Changes

File Change
pkg/mcp/client/client.go Add a Probe API that returns initialize metadata and the selected transport without enumerating capabilities.
pkg/mcp/client/client_test.go Exercise a real streamable HTTP MCP server and assert that the probe sends initialize but no list methods.
cmd/thv/app/mcp_check.go Add the command with isolated flags, timeout handling, and text/JSON output through Cobra writers.
cmd/thv/app/mcp_check_test.go Verify the five-second default survives parent-command construction and JSON is capturable through the command writer.
cmd/thv/app/mcp.go Register the new command.
docs/cli/thv_mcp_check.md Document the generated CLI workflow and flags.
docs/cli/thv_mcp.md Link the new command from the generated parent command reference.

Does this introduce a user-facing change?

Yes. Users can now use thv mcp check --server ... --format json as an MCP-native readiness gate for CI, GitOps, and Agent harnesses.

Special notes for reviewers

The new probe intentionally does not call tools/list, resources/list, prompts/list, or any tool. Existing Connect callers retain their behavior and API.

Expose a protocol-level preflight for Agent harnesses and CI. The check performs only transport setup and the MCP initialize handshake, then reports negotiated metadata without enumerating or invoking capabilities.

Signed-off-by: XD <[email protected]>
Keep mcp check flags isolated from sibling commands and route text and JSON results through Cobra output writers. Add real initialize-only and CLI regression coverage, then regenerate the command documentation.

Signed-off-by: XD <[email protected]>
ChrisJBurns
ChrisJBurns previously approved these changes Nov 26, 2026
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.43%. Comparing base (9300f3f) to head (c08744c).

Files with missing lines Patch % Lines
pkg/mcp/client/client.go 86.66% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6374      +/-   ##
==========================================
+ Coverage   82.40%   82.43%   +0.03%     
==========================================
  Files         937      937              
  Lines       95821    95847      +26     
==========================================
+ Hits        78962    79014      +52     
+ Misses      16854    16828      -26     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, the shape is right: Connect and its callers are untouched, the new flags are local to check, and JSON on stdout is clean enough to pipe into jq.

I built the branch and ran thv mcp check against:

  • Real workloads, by name and by URL: a stdio server behind the streamable proxy, the same server behind the SSE proxy, and a native streamable-http server. All reported ready with the right transport. After 5 back-to-back checks, thv mcp list still worked against the stdio backend, so the cached-initialize path from #6153 holds up. With the backend container paused, the check failed correctly (by name and by URL).
  • Local test servers: one with no capabilities, an SSE-only server, a stateless server (negotiated 2026-07-28), and a server that returns 405 on DELETE. All passed. Servers returning 401, returning 503, hanging, or not listening all failed with exit 1.

Two things need fixing before this works as a CI gate. Details are inline.

Comment thread pkg/mcp/client/client.go Resolved
Comment thread pkg/mcp/client/client.go Resolved
In auto mode the readiness check collapsed every failure into one
generic message, hiding whether a server rejected credentials (fix
config) or was not ready yet (retry). Keep each transport attempt's
error so the cause reaches the user.

A failed session-teardown DELETE also marked a server that completed
the initialize handshake as not ready, and in auto mode triggered a
pointless SSE fallback. Readiness now depends only on the handshake;
close failures are logged at WARN, matching the other thv mcp commands.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both findings are fixed in c08744c. I re-ran the same local servers against the rebuilt branch:

  • In auto mode, the error now names each transport and its real cause: 401, 503 backend not ready, connection refused, and context deadline exceeded for the hanging server.
  • When the server resets the connection on the closing DELETE, the check reports ready (exit 0) in both auto and explicit streamable-http mode. The close error is logged as a WARN on stderr, so --format json | jq still parses.

Both new tests fail against the previous client.go and pass with -race -count=5, so they would catch a regression. LGTM once CI is green.

@ChrisJBurns
ChrisJBurns merged commit fe576c4 into stacklok:main Nov 26, 2026
43 checks passed
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.

3 participants