refactor(mcp): call pkg/functions directly for create and delete tools#3780
refactor(mcp): call pkg/functions directly for create and delete tools#3780sneharathod7 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: sneharathod7 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
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 Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions 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. |
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.