Skip to content

Release MCP inspector sub-contexts promptly after each operation#18343

Merged
pelikhan merged 2 commits intomainfrom
copilot/release-mcp-inspector-contexts
Feb 25, 2026
Merged

Release MCP inspector sub-contexts promptly after each operation#18343
pelikhan merged 2 commits intomainfrom
copilot/release-mcp-inspector-contexts

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

connectStdioMCPServer and connectHTTPMCPServer created WithTimeout sub-contexts for each sequential MCP operation (ListTools, ListResources) but cancelled them via defer, holding timer resources until function return rather than releasing them immediately after each operation completed.

Changes

  • connectStdioMCPServer and connectHTTPMCPServer: replaced defer cancel() with an immediate cancel() after each sequential MCP operation
// Before — timer held until function return
listToolsCtx, cancel := context.WithTimeout(ctx, MCPOperationTimeout)
defer cancel()
toolsResult, err := session.ListTools(listToolsCtx, ...)

// After — timer released as soon as operation completes
listToolsCtx, cancel := context.WithTimeout(ctx, MCPOperationTimeout)
toolsResult, err := session.ListTools(listToolsCtx, ...)
cancel()

The connectCtx used for client.Connect() retains defer cancel() since it isn't sequential and doesn't benefit from early release.

Original prompt

This section details on the original issue you should resolve

<issue_title>[plan] Release MCP inspector sub-contexts promptly after each operation</issue_title>
<issue_description>## Objective

In pkg/cli/mcp_inspect_mcp.go, call cancel() immediately after each sequential MCP operation (ListTools, ListResources, ListPrompts) instead of deferring, to release context resources promptly.

Context

From discussion #18080 (go-sdk module review): The connectStdioMCPServer and connectHTTPMCPServer functions create three WithTimeout sub-contexts sequentially and cancel them via defer. Since only one operation runs at a time, cancelling immediately after each operation is complete (rather than at function return) releases timer resources sooner.

File to Modify

pkg/cli/mcp_inspect_mcp.goconnectStdioMCPServer and connectHTTPMCPServer functions

Change Required

// Before — deferred cancel held until function return
listToolsCtx, cancel := context.WithTimeout(ctx, MCPOperationTimeout)
defer cancel()
toolsResult, err := session.ListTools(listToolsCtx, ...)

// After — cancel immediately after operation completes
listToolsCtx, cancel := context.WithTimeout(ctx, MCPOperationTimeout)
toolsResult, err := session.ListTools(listToolsCtx, ...)
cancel()

Apply the same pattern to the ListResources and ListPrompts context pairs.

Acceptance Criteria

  • All three sequential sub-context cancel() calls are invoked immediately after use (not deferred) in both connectStdioMCPServer and connectHTTPMCPServer
  • make test-unit passes
  • make agent-finish passes

Generated by Plan Command for issue #discussion #18080

  • expires on Feb 27, 2026, 12:38 PM UTC

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Release MCP inspector sub-contexts promptly after operations Release MCP inspector sub-contexts promptly after each operation Feb 25, 2026
@pelikhan pelikhan marked this pull request as ready for review February 25, 2026 12:46
Copilot AI review requested due to automatic review settings February 25, 2026 12:46
@pelikhan pelikhan merged commit b70143d into main Feb 25, 2026
@pelikhan pelikhan deleted the copilot/release-mcp-inspector-contexts branch February 25, 2026 12:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR ensures MCP inspector operation timeout sub-contexts are cancelled immediately after each sequential operation, releasing timer resources promptly instead of holding them until the function returns.

Changes:

  • Replace defer cancel() with an immediate cancel() after ListTools in both stdio and HTTP connection flows.
  • Replace defer cancel() with an immediate cancel() after ListResources in both stdio and HTTP connection flows.
  • Reformat a smoke-test JSON file used for PR push validation.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/cli/mcp_inspect_mcp.go Cancels per-operation WithTimeout contexts immediately after ListTools/ListResources complete to release timers sooner.
.github/smoke-test-22395402042.json Formatting-only change to the smoke-test JSON file.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 193 to +196
// List tools
listToolsCtx, cancel := context.WithTimeout(ctx, MCPOperationTimeout)
defer cancel()

toolsResult, err := session.ListTools(listToolsCtx, &mcp.ListToolsParams{})
cancel()
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The PR description/acceptance criteria mention also applying this to ListPrompts, but this repo/file does not contain any ListPrompts call (only ListTools/ListResources). Please update the PR description (or the referenced acceptance criteria) to match the actual operations being changed, to avoid confusion for future readers.

Copilot uses AI. Check for mistakes.
@github-actions
Copy link
Contributor

Smoke test summary
PRs: #18343 Release MCP inspector sub-contexts promptly after each operation; #18331 fix: update wasm golden fixtures to Copilot CLI 0.0.417
GitHub MCP: ✅
Serena MCP: ✅
Playwright: ✅
Web-fetch: ✅
File write+cat: ✅
Build: ✅
Overall: PASS

🔮 The oracle has spoken through Smoke Codex

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.

[plan] Release MCP inspector sub-contexts promptly after each operation

3 participants