Skip to content

fix(mcp): omit empty subcommand in defaultExecutor argument list#3667

Open
Ankitsinghsisodya wants to merge 1 commit intoknative:mainfrom
Ankitsinghsisodya:fix/mcp-empty-subcommand
Open

fix(mcp): omit empty subcommand in defaultExecutor argument list#3667
Ankitsinghsisodya wants to merge 1 commit intoknative:mainfrom
Ankitsinghsisodya:fix/mcp-empty-subcommand

Conversation

@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor

Problem

Every help resource handler calls executor.Execute(ctx, "", cmd..., "--help"), passing an empty string as the subcommand argument. defaultExecutor.Execute unconditionally appended that empty string to the command parts, producing invocations like:

func "" create --help

Cobra receives "" as the first positional argument, fails to match it against any known subcommand, and returns an error. Every help resource was broken when used against a real func binary.

A second instance of the same class of bug existed on the root help resource registration, which passed "" as a cmd variadic argument:

// before
newHelpResource(s, "Help", "help for the command root", "")

This injected a second empty argument even with the executor fix in place.

The test suite did not catch either defect because every resource test uses a mock executor via WithExecutor, so defaultExecutor is never exercised.

Fix

  • Extract buildArgs(prefix, subcommand string, args []string) []string from defaultExecutor.Execute. The helper omits the subcommand when it is empty.
  • Drop the spurious "" argument from the root help resource registration.

Tests

  • TestBuildArgs — five table-driven cases directly testing buildArgs, including the critical empty-subcommand case and multi-word prefix handling.
  • TestResource_Help — eleven table-driven cases covering every registered help URI end-to-end through the resource handler → mock executor path. Asserts that subcommand is always "" and no element in args is ever an empty string.

- Implemented TestBuildArgs to ensure buildArgs constructs command arguments correctly, omitting empty subcommands.
- Added TestResource_Help to confirm that help resource handlers invoke the executor with the correct arguments, preventing empty strings in command execution.
- Updated mcp.go to utilize the new buildArgs function for command argument construction, enhancing clarity and correctness in command execution.
Copilot AI review requested due to automatic review settings May 6, 2026 22:17
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 6, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Ankitsinghsisodya
Once this PR has been reviewed and has the lgtm label, please assign dprotaso for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 6, 2026 22:17
@knative-prow knative-prow Bot added size/L 🤖 PR changes 100-499 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 6, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 6, 2026

Hi @Ankitsinghsisodya. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link
Copy Markdown
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

Fixes MCP help resource execution against real func binaries by preventing empty command parts from being injected into the executed argument list, and adds targeted regression tests.

Changes:

  • Refactors command argument construction into buildArgs(...) and omits the subcommand when it’s empty.
  • Removes the spurious "" argument from the root help resource registration.
  • Adds tests covering buildArgs behavior and verifying help resource handlers don’t pass empty command parts to the executor.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/mcp/mcp.go Removes empty root help cmd segment and uses new buildArgs helper to construct exec args without empty subcommand injection.
pkg/mcp/mcp_test.go Adds table-driven tests validating buildArgs behavior across empty subcommand and multi-word prefixes.
pkg/mcp/resources_test.go Adds end-to-end-ish resource-handler tests to ensure help URIs map to correct executor args and never include empty strings.

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

Comment thread pkg/mcp/mcp.go
Comment on lines 153 to 156
func (e defaultExecutor) Execute(ctx context.Context, subcommand string, args ...string) ([]byte, error) {
// Parse prefix: "func" or "kn func" -> ["func"] or ["kn", "func"]
cmdParts := strings.Fields(e.s.prefix)
cmdParts = append(cmdParts, subcommand)
cmdParts = append(cmdParts, args...)

cmdParts := buildArgs(e.s.prefix, subcommand, args)
cmd := exec.CommandContext(ctx, cmdParts[0], cmdParts[1:]...)
// cmd.Dir not set - inherits process working directory which is the current working directory
Comment thread pkg/mcp/resources_test.go
Comment on lines +87 to +91
// TestResource_Help verifies that every help resource handler invokes the
// executor with an empty subcommand and the correct command arguments ending
// with "--help". This is the regression test for the bug where the empty
// subcommand was unconditionally appended, injecting a spurious "" argument
// into every help command.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 56.73%. Comparing base (ef88a9b) to head (297747a).

Files with missing lines Patch % Lines
pkg/mcp/mcp.go 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3667      +/-   ##
==========================================
+ Coverage   56.02%   56.73%   +0.70%     
==========================================
  Files         181      181              
  Lines       20732    20734       +2     
==========================================
+ Hits        11615    11763     +148     
+ Misses       7905     7759     -146     
  Partials     1212     1212              
Flag Coverage Δ
e2e 36.32% <0.00%> (-4.08%) ⬇️
e2e go 33.00% <0.00%> (-0.01%) ⬇️
e2e node 28.70% <0.00%> (-0.01%) ⬇️
e2e python 33.35% <0.00%> (-0.03%) ⬇️
e2e quarkus 28.84% <0.00%> (-0.01%) ⬇️
e2e rust 28.24% <0.00%> (-0.01%) ⬇️
e2e springboot 26.73% <0.00%> (-0.01%) ⬇️
e2e typescript 28.81% <0.00%> (-0.01%) ⬇️
e2e-config-ci 18.02% <0.00%> (-0.01%) ⬇️
integration 17.43% <0.00%> (-0.01%) ⬇️
unit macos-14 44.33% <85.71%> (+0.06%) ⬆️
unit macos-latest 44.33% <85.71%> (+0.06%) ⬆️
unit ubuntu-24.04-arm 44.50% <87.50%> (+0.05%) ⬆️
unit ubuntu-latest 45.19% <85.71%> (+0.06%) ⬆️
unit windows-latest 44.31% <85.71%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test 🤖 Needs an org member to approve testing size/L 🤖 PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants