Skip to content

refactor(mcp): reduce cognitive complexity of the test tool - #1758

Open
marcossevilla wants to merge 1 commit into
refactor/cc-packagesfrom
refactor/cc-mcp-server
Open

marcossevilla wants to merge 1 commit into
refactor/cc-packagesfrom
refactor/cc-mcp-server

Conversation

@marcossevilla

@marcossevilla marcossevilla commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Status

READY

Description

  • _parseTest builds its argument list in one expression from _flag and _option helpers, keeping the exact order of the original if-chain.
  • _runToolCommand hands its captured output buffer and its success and failure builders to a private _ToolRun, so the method only sets the working directory, runs the command and maps exceptions to failures. Stderr messages and StructuredToolError fields are unchanged.

timeout_seconds and paths are now read at the top of _parseTest. Well-formed input produces the same arguments, and badly typed input still throws a TypeError.

Found by the full cognitive complexity scan in #1755, which tests VeryGoodOpenSource/very_good_workflows#520. Every function in the touched files now scores 15 or under. Behavior, log output, exit codes and public APIs are unchanged, and no tests were needed because the existing ones already reach every new branch.

Function Before After
VeryGoodMCPServer._parseTest 21 2
VeryGoodMCPServer._runToolCommand 16 8

Part of a three-PR cleanup. Once all three merge, the full scan on #1755 passes:

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

🤖 Generated with Claude Code

///
/// Owns the buffer that captures the command's output and turns the outcome
/// of the run into a [CallToolResult].
class _ToolRun {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

move this to another file

Comment on lines +587 to +590
final exitCode = await run.capture(
(logger) => _commandRunnerBuilder(logger: logger).run(args),
);

if (exitCode == ExitCode.success.code) {
final captured = sanitizeCommandOutput(output.toString()).trim();
return CallToolResult(
content: [
TextContent(text: '"$toolName" completed successfully.'),
if (captured.isNotEmpty) TextContent(text: captured),
],
isError: false,
);
}

return errorResult(
'failed with exit code $exitCode.',
failureType: ToolFailureType.fromExitCode(exitCode),
);
return run.resultFor(exitCode);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

make run.capture return the exitCode directly

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@marcossevilla
marcossevilla force-pushed the refactor/cc-mcp-server branch from 13ec12e to ab446c8 Compare October 1, 2026 13:41
@marcossevilla
marcossevilla changed the base branch from main to refactor/cc-packages October 1, 2026 13:41
@marcossevilla
marcossevilla added this pull request to stack #1759 October 1, 2026 13:41
@marcossevilla

Copy link
Copy Markdown
Member Author

@vgvbot /review-pr

@unicoderbot

unicoderbot Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

I'll review the code and share my feedback shortly.

  • Preparing environment
    • Cloning repository
    • Setting up dev environment
  • Reviewing code
  • Posting review

Code review submitted.

@unicoderbot unicoderbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The refactor cleanly reduces cognitive complexity in both _parseTest and _runToolCommand. The _flag/_option helpers preserve argument order and null/type handling, and the _ToolRun extraction keeps stderr messages and StructuredToolError fields intact.

However, there is a critical bug: the _ToolRun constructor is declared as new({...}) (line 615) with no class-name prefix. This is not valid Dart constructor syntax — a constructor must begin with the type identifier (e.g. _ToolRun({...}) or _ToolRun.new({...})). The call site at line 571 uses _ToolRun(toolName: ...) and expects an unnamed constructor, so this file will fail to compile. The description claims no new tests were added because existing tests already cover the branches, so this would surface the first time the test suite is run.

/// Owns the buffer that captures the command's output and turns the outcome
/// of the run into a [CallToolResult].
class _ToolRun {
new({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not a valid Dart constructor declaration. A constructor must start with the class name — either _ToolRun({...}) (unnamed) or _ToolRun.new({...}) (explicit .new alias). Written as just new({...}), this file will not compile, and since _ToolRun is instantiated as _ToolRun(toolName: ...) at line 571 (an unnamed-constructor call), the implicit default constructor it would otherwise fall back to also wouldn't match. Change to _ToolRun({ required this.toolName, required this.commandString, required this.directory, required this.requestArguments });.

This branch has not been deployed

No deployments
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.

1 participant