Skip to content

Refactor mcp_inspect.go into focused modules (1011→285 lines)#11877

Merged
pelikhan merged 2 commits intomainfrom
copilot/refactor-mcp-inspect-file
Jan 26, 2026
Merged

Refactor mcp_inspect.go into focused modules (1011→285 lines)#11877
pelikhan merged 2 commits intomainfrom
copilot/refactor-mcp-inspect-file

Conversation

Copy link
Contributor

Copilot AI commented Jan 26, 2026

mcp_inspect.go grew to 1011 lines with multiple distinct responsibilities (workflow listing, import processing, safe-inputs server lifecycle, inspector spawning). Refactored into 7 focused modules along semantic boundaries.

Module Breakdown

Core entry point (285 lines, was 1011)

  • mcp_inspect.go - InspectWorkflowMCP(), NewMCPInspectSubcommand()

New focused modules (6 files, 794 lines total)

  • mcp_inspect_list.go (58 lines) - listWorkflowsWithMCP(), filterOutSafeOutputs()
  • mcp_inspect_imports.go (78 lines) - applyImportsToFrontmatter()
  • mcp_inspect_safe_inputs_server.go (194 lines) - HTTP server lifecycle: startSafeInputsServer(), findAvailablePort(), waitForServerReady()
  • mcp_inspect_safe_inputs_files.go (111 lines) - writeSafeInputsFiles()
  • mcp_inspect_safe_inputs_inspector.go (131 lines) - spawnSafeInputsInspector()
  • mcp_inspect_inspector.go (222 lines) - spawnMCPInspector()

API Compatibility

Public API unchanged. All functions remain package-private. Shared logger (mcpInspectLog) used across modules.

Related Files

Pre-existing separation:

  • mcp_inspect_mcp.go (619 lines) - MCP connection logic
  • mcp_validation.go - Secret validation
  • mcp_tool_table.go - Tool rendering
Original prompt

This section details on the original issue you should resolve

<issue_title>[file-diet] Refactor mcp_inspect.go (1011 lines) into focused modules</issue_title>
<issue_description>### Overview

The file pkg/cli/mcp_inspect.go has grown to 1011 lines, making it difficult to maintain and test. This task involves refactoring it into smaller, focused files with improved test coverage.

Current State

  • File: pkg/cli/mcp_inspect.go
  • Size: 1011 lines
  • Test Coverage: 440 lines (test-to-source ratio: 0.44)
  • Complexity: Multiple distinct functional domains (workflow inspection, safe-inputs server management, HTTP server orchestration, MCP inspector spawning)
Full File Analysis

Function Distribution

The file contains 12 functions spanning 4 major functional domains:

Workflow MCP Inspection (3 functions, ~300 lines)

  • InspectWorkflowMCP (lines 112-294) - Main entry point for workflow inspection
  • listWorkflowsWithMCP (lines 297-330) - Lists workflows with MCP configurations
  • filterOutSafeOutputs (lines 33-41) - Filters safe-outputs from MCP list
  • applyImportsToFrontmatter (lines 45-109) - Merges imported MCP servers and tools

Safe-Inputs Server Management (4 functions, ~400 lines)

  • writeSafeInputsFiles (lines 333-431) - Writes safe-inputs files to disk
  • startSafeInputsHTTPServer (lines 434-462) - Starts Node.js HTTP server
  • findAvailablePort (lines 465-480) - Port discovery logic
  • waitForServerReady (lines 483-506) - HTTP server health checking
  • startSafeInputsServer (lines 509-603) - Orchestrates safe-inputs setup

Safe-Inputs Inspector (1 function, ~120 lines)

  • spawnSafeInputsInspector (lines 607-720) - Generates files, starts server, launches inspector

MCP Inspector Spawning (2 functions, ~200 lines)

  • spawnMCPInspector (lines 724-928) - Launches @modelcontextprotocol/inspector with stdio server management
  • NewMCPInspectSubcommand (lines 932-1011) - Cobra command configuration

Complexity Hotspots

  1. InspectWorkflowMCP (182 lines) - Handles frontmatter parsing, import processing, validation, safe-inputs detection, and server inspection
  2. startSafeInputsServer (94 lines) - Complex orchestration with temporary directory management, port discovery, server startup, and health checking
  3. spawnMCPInspector (204 lines) - Manages stdio server lifecycle, process monitoring with goroutines, and inspector spawning
  4. writeSafeInputsFiles (98 lines) - Writes 9+ JavaScript files and generates tool handlers dynamically

Coupling Issues

  • Safe-inputs server logic is tightly coupled to workflow inspection
  • HTTP server management mixed with file generation
  • Process lifecycle management (goroutines, cleanup) in same function as inspector spawning
  • Command configuration depends on multiple helper functions across different domains

Refactoring Strategy

Proposed File Splits

Based on semantic analysis, split the file into the following modules:

  1. mcp_inspect.go (Main command entry point)

    • Functions: NewMCPInspectSubcommand, InspectWorkflowMCP
    • Responsibility: CLI command configuration and workflow inspection orchestration
    • Estimated LOC: ~250 lines
  2. mcp_inspect_list.go (Workflow listing and filtering)

    • Functions: listWorkflowsWithMCP, filterOutSafeOutputs
    • Responsibility: Listing workflows with MCP servers
    • Estimated LOC: ~100 lines
  3. mcp_inspect_imports.go (Import processing)

    • Functions: applyImportsToFrontmatter
    • Responsibility: Merging imported MCP servers and tools into frontmatter
    • Estimated LOC: ~100 lines
  4. mcp_inspect_safe_inputs_server.go (Safe-inputs HTTP server management)

    • Functions: startSafeInputsServer, startSafeInputsHTTPServer, findAvailablePort, waitForServerReady
    • Responsibility: Safe-inputs HTTP server lifecycle (port discovery, startup, health checking)
    • Estimated LOC: ~200 lines
  5. mcp_inspect_safe_inputs_files.go (Safe-inputs file generation)

    • Functions: writeSafeInputsFiles
    • Responsibility: Generating and writing safe-inputs JavaScript/tool files
    • Estimated LOC: ~150 lines
  6. mcp_inspect_safe_inputs_inspector.go (Safe-inputs inspector)

    • Functions: spawnSafeInputsInspector
    • Responsibility: Spawning inspector for safe-inputs MCP server
    • Estimated LOC: ~150 lines
  7. mcp_inspect_inspector.go (MCP inspector spawning)

    • Functions: spawnMCPInspector
    • Responsibility: Launching @modelcontextprotocol/inspector with stdio server management
    • Estimated LOC: ~250 lines

Interface Abstractions

Consider introducing interfaces to reduce coupling:

// ServerStarter abstracts safe-inputs HTTP server startup
type ServerStarter interface {
    StartServer(config *workflow.SafeInputsConfig, verbose bool) (*parser.MCPServerConfig, *exec.Cmd, string, error)
}

// ...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes githubnext/gh-aw#11874

<!-- START COPILOT CODING AGENT TIPS -->
---Let Copilot coding agent [set things up for you](https://github.com/githubnext/gh-aw/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor mcp_inspect.go into focused modules Refactor mcp_inspect.go into focused modules (1011→285 lines) Jan 26, 2026
Copilot AI requested a review from pelikhan January 26, 2026 14:31
@pelikhan pelikhan marked this pull request as ready for review January 26, 2026 15:18
@pelikhan pelikhan merged commit 34b849f into main Jan 26, 2026
152 checks passed
@pelikhan pelikhan deleted the copilot/refactor-mcp-inspect-file branch January 26, 2026 15:26
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