Skip to content

refactor(mcp): call pkg/functions directly for create and delete tools#3780

Open
sneharathod7 wants to merge 1 commit into
knative:mainfrom
sneharathod7:feat-mcp-direct-client
Open

refactor(mcp): call pkg/functions directly for create and delete tools#3780
sneharathod7 wants to merge 1 commit into
knative:mainfrom
sneharathod7:feat-mcp-direct-client

Conversation

@sneharathod7
Copy link
Copy Markdown

Summary
This PR refactors the Model Context Protocol (MCP) server's create and delete tools to call the core pkg/functions library directly, migrating away from the subprocess shellout pattern (completing part of #3771).

To support seamless coordination with the patterns established in #3778, this PR utilizes a lazy closure provider to wire the fn.Client directly into the MCP Server structure, bypassing circular instantiation dependencies. A backward-compatible executor fallback is preserved for backward compatibility and to guarantee that existing mock unit tests remain green.

🛠️ What Changed
Lazy Client Provider Wiring

Extended pkg/mcp/Server to include a clientProvider func() *fn.Client field.
Exposed the functional option WithClientProvider to initialize it.
Configured the lazy provider closure in cmd/mcp.go to capture the initialized client and bypass the circular dependency between newClient and mcp.New.
Direct create Scaffolding

Refactored tools_create.go to invoke client.Init(...) directly when clientProvider is active.
Implemented a clean, platform-independent deriveNameAndPath helper to accurately resolve target directories.
Added custom template repository support by dynamically instantiating localized template registries if --repository is requested.
Direct delete Deletion

Refactored tools_delete.go to invoke client.Remove(...) directly when clientProvider is active.
Incorporated a robust namespace defaulting mechanism (getNamespace) that queries the client's configuration, active Kubernetes context via k8s.GetDefaultNamespace(), or falls back to "default", matching CLI behavior.
Integration Testing & green coverage

Created TestTool_Create_DirectClient in tools_create_test.go to verify end-to-end scaffolding inside temporary testing directories.
Created TestTool_Delete_DirectClient in tools_delete_test.go to verify path-based and name-based deletions using real client contexts.
Maintained all previous unit tests by retaining a safe fallback to s.executor if the clientProvider is nil.
🧪 Verification & Test Runs
All unit and integration tests compile, run, and pass with 100% success:

bash
go test -v ./pkg/mcp
text
=== RUN TestTool_Create_DirectClient
--- PASS: TestTool_Create_DirectClient (0.04s)
=== RUN TestTool_Delete_DirectClient
--- PASS: TestTool_Delete_DirectClient (0.04s)
PASS
ok knative.dev/func/pkg/mcp 0.605s
The entire project compiles flawlessly:

bash
go build -o func.exe ./cmd/func
(Exit code: 0)

🔒 Checklist
I have read and followed the Knative contribution guidelines.
All existing unit tests pass cleanly.
New integration tests added to cover create and delete direct library client paths.
Core CLI subcommands build perfectly.

@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 18, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sneharathod7
Once this PR has been reviewed and has the lgtm label, please assign dsimansk 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 18, 2026 04:26
@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 18, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 18, 2026

Hi @sneharathod7. 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.

Regular contributors should join the org to skip this step.

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.

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.

1 participant