Skip to content

feat(mcp): integrate tools with functions.Client API instead of CLI shell-out#3889

Open
vishwas-droid wants to merge 4 commits into
knative:mainfrom
vishwas-droid:feat/mcp-native-client-api
Open

feat(mcp): integrate tools with functions.Client API instead of CLI shell-out#3889
vishwas-droid wants to merge 4 commits into
knative:mainfrom
vishwas-droid:feat/mcp-native-client-api

Conversation

@vishwas-droid

@vishwas-droid vishwas-droid commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replace MCP tool handlers that shelled out to the func binary with a native Service layer backed by functions.Client
  • Wire cmd/mcp start to construct clients via the existing ClientFactory, decoupling MCP startup from WithMCPServer on the same client instance
  • Add four new MCP tools: describe, invoke, run, and logs
  • Return structured JSON outputs from tool handlers (e.g. BuildOutput.Image, DeployOutput.URL, ListOutput.Functions) instead of parsing CLI stdout

Help resources (func://help/*) and templates listing continue to use CLI subprocess for --help text.

The MCP server had an explicit comment that shell-out should be replaced with direct functions.Client integration. The subprocess approach caused cwd/path issues (documented in pkg/mcp/instructions.md) and produced unstructured stdout that agents could not reliably parse.

Test plan

  • CGO_ENABLED=0 go test ./pkg/mcp/...
  • CGO_ENABLED=0 go test ./cmd/ -run MCP
  • Manual: func mcp start with Cursor/Claude MCP client
  • Manual: verify build, deploy, list, describe, invoke tools return structured JSON
  • Manual: verify readonly mode still blocks deploy and delete

@knative-prow

knative-prow Bot commented Jun 10, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: vishwas-droid
Once this PR has been reviewed and has the lgtm label, please assign gauron99 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 June 10, 2026 11:31
@knative-prow knative-prow Bot added the size/XXL 🤖 PR changes 1000+ lines, ignoring generated files. label Jun 10, 2026
@knative-prow

knative-prow Bot commented Jun 10, 2026

Copy link
Copy Markdown

Hi @vishwas-droid. 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.

@knative-prow knative-prow Bot added the needs-ok-to-test 🤖 Needs an org member to approve testing label Jun 10, 2026
@vishwas-droid vishwas-droid force-pushed the feat/mcp-native-client-api branch from 6185eb4 to 797955f Compare June 10, 2026 11:55
…hell-out

Replace MCP tool handlers that exec'd the func binary with a native service
layer backed by functions.Client. This enables structured JSON outputs,
correct path handling, and readonly enforcement at the API level.

Adds describe, invoke, run, and logs MCP tools. Help resources continue to
use CLI subprocess for --help text.

Copilot AI 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.

Pull request overview

This PR migrates the MCP server implementation from shelling out to the func CLI toward a native Service layer backed by functions.Client, enabling structured JSON outputs for tool responses and avoiding subprocess cwd/path fragility.

Changes:

  • Introduces a Service abstraction that executes MCP tool operations via functions.Client (build/deploy/list/delete/create + new describe/invoke/run/logs tools).
  • Refactors MCP server startup (cmd/mcp start) to inject a client factory into the MCP server, decoupling MCP from fn.WithMCPServer(...).
  • Updates MCP resources (e.g., languages, function state) to use the native service layer where possible, while keeping help/templates via CLI executor.

Reviewed changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
pkg/mcp/types.go Adds ClientConfig and ClientFactory types for constructing functions.Client instances.
pkg/mcp/tools_test.go Adds initTestFunction helper to create an initialized function for tests.
pkg/mcp/tools_run.go Adds new run MCP tool and handler wired to the service layer.
pkg/mcp/tools_logs.go Adds new logs MCP tool and handler wired to the service layer.
pkg/mcp/tools_list.go Switches list tool from CLI executor output to service-backed structured output.
pkg/mcp/tools_list_test.go Updates list tool tests to use the service-backed implementation.
pkg/mcp/tools_invoke.go Adds new invoke MCP tool and handler wired to the service layer.
pkg/mcp/tools_describe.go Adds new describe MCP tool and handler wired to the service layer.
pkg/mcp/tools_deploy.go Switches deploy tool to service-backed deploy and structured output.
pkg/mcp/tools_deploy_test.go Updates deploy tool tests (keeps readonly rejection coverage).
pkg/mcp/tools_delete.go Switches delete tool to service-backed delete while retaining input validation + readonly behavior.
pkg/mcp/tools_delete_test.go Updates delete tool tests (keeps readonly rejection coverage).
pkg/mcp/tools_create.go Switches create tool to service-backed create and structured output.
pkg/mcp/tools_create_test.go Updates create tool test to create into a temp dir via service-backed handler.
pkg/mcp/tools_config_volumes.go Switches config volumes tools to service-backed operations.
pkg/mcp/tools_config_volumes_test.go Updates config volumes tests to use real service-backed behavior.
pkg/mcp/tools_config_labels.go Switches config labels tools to service-backed operations.
pkg/mcp/tools_config_labels_test.go Updates config labels tests to use real service-backed behavior.
pkg/mcp/tools_config_envs.go Switches config envs tools to service-backed operations (validation still present).
pkg/mcp/tools_config_envs_test.go Updates config envs tests to use real service-backed behavior.
pkg/mcp/tools_build.go Switches build tool to service-backed build and structured output.
pkg/mcp/tools_build_test.go Updates build test to avoid requiring CLI executor; checks for missing factory error.
pkg/mcp/service.go Adds the core service implementation for MCP tool operations using functions.Client.
pkg/mcp/service_test.go Adds service-level tests for config operations + list/create basics.
pkg/mcp/resources_test.go Adjusts resource tests for languages/templates now that languages is service-backed.
pkg/mcp/resources_language.go Switches languages resource from CLI executor to service Runtimes().
pkg/mcp/resources_function.go Switches function-state resource to service FunctionState().
pkg/mcp/mcp.go Adds WithClientFactory, registers new tools/resources, and retains executor only for help.
pkg/mcp/mcp_test.go Injects a test client factory into server construction.
pkg/mcp/client_options.go Adds shared helpers to build functions.Client options for builders/deployers/credentials.
cmd/mcp.go Refactors mcp start to create an MCP server with a client factory (no longer uses fn.WithMCPServer).
cmd/mcp_test.go Updates MCP start tests to validate server startup via in-memory transports.

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

Comment thread pkg/mcp/service.go
Comment thread pkg/mcp/service.go
Comment thread pkg/mcp/service.go
Comment thread pkg/mcp/tools_build_test.go
Comment thread pkg/mcp/tools_config_envs_test.go
Comment thread pkg/mcp/tools_config_volumes_test.go
@gauron99

Copy link
Copy Markdown
Contributor

Hi, did you run anything manually? test this works as expected? worked through some scenarios yourself with the different func commands? any screenshots showing anything important? highlights?

Default push to true when unset during deploy, select deployer from
func.yaml, and apply deployDecorator on deploy/pipeline clients.

Co-authored-by: Cursor <cursoragent@cursor.com>
@vishwas-droid vishwas-droid force-pushed the feat/mcp-native-client-api branch from 13a3f36 to c25ae60 Compare June 10, 2026 15:15
@vishwas-droid

vishwas-droid commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

@gauron99 Yes, I tested this on my machine.

I went through the main MCP tools — create, config, list, healthcheck, run, and invoke — and they all return proper structured JSON now instead of shelling out to the CLI. Readonly mode correctly blocks deploy/delete too. Unit tests pass as well.

Also fixed a few things I noticed while testing — push default, deployer from func.yaml, and deployDecorator.

Couldn't test build/deploy on a real cluster since I don't have Docker/k8s set up locally. would be good if someone with a cluster setup can give that a quick pass.
Screenshot 2026-06-11 at 10 48 52 AM
Screenshot 2026-06-11 at 10 49 08 AM
Screenshot 2026-06-11 at 10 49 11 AM

Screenshot 2026-06-11 at 12 02 52 PM Screenshot 2026-06-11 at 12 04 49 PM

@matejvasek

Copy link
Copy Markdown
Contributor

Some check are failing.

Drop resultToString and CLI arg-building test helpers that became
dead code when MCP tools switched to the functions.Client service
layer. Fixes golangci-lint unused violations blocking CI precheck.
@vishwas-droid vishwas-droid force-pushed the feat/mcp-native-client-api branch from c25ae60 to f795888 Compare June 11, 2026 12:30
@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 31.08108% with 663 lines in your changes missing coverage. Please review.
✅ Project coverage is 52.19%. Comparing base (cdf3c96) to head (f795888).

Files with missing lines Patch % Lines
pkg/mcp/service.go 23.02% 498 Missing and 47 partials ⚠️
pkg/mcp/client_options.go 51.97% 70 Missing and 3 partials ⚠️
pkg/mcp/tools_describe.go 0.00% 7 Missing ⚠️
pkg/mcp/tools_invoke.go 0.00% 7 Missing ⚠️
pkg/mcp/tools_logs.go 0.00% 7 Missing ⚠️
pkg/mcp/tools_run.go 0.00% 7 Missing ⚠️
cmd/mcp.go 60.00% 5 Missing and 1 partial ⚠️
pkg/mcp/mcp.go 84.21% 2 Missing and 1 partial ⚠️
pkg/mcp/resources_language.go 50.00% 2 Missing and 1 partial ⚠️
pkg/mcp/tools_delete.go 0.00% 2 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3889      +/-   ##
==========================================
- Coverage   53.72%   52.19%   -1.53%     
==========================================
  Files         200      206       +6     
  Lines       23674    24469     +795     
==========================================
+ Hits        12719    12772      +53     
- Misses       9730    10400     +670     
- Partials     1225     1297      +72     
Flag Coverage Δ
e2e 32.27% <0.00%> (-1.19%) ⬇️
e2e go 28.30% <0.00%> (?)
e2e node 24.76% <0.00%> (?)
e2e python 28.62% <0.00%> (?)
e2e quarkus 24.89% <0.00%> (?)
e2e rust 24.38% <0.00%> (?)
e2e springboot 23.11% <0.00%> (?)
e2e typescript 24.88% <0.00%> (?)
e2e-config-ci 25.92% <0.00%> (-1.01%) ⬇️
integration 15.15% <0.00%> (-0.52%) ⬇️
unit macos-14 41.53% <34.13%> (-1.28%) ⬇️
unit macos-latest 41.53% <34.13%> (-1.28%) ⬇️
unit ubuntu-24.04-arm 41.78% <31.08%> (-1.35%) ⬇️
unit ubuntu-latest 42.36% <34.13%> (-1.30%) ⬇️
unit windows-latest 41.60% <34.13%> (-1.28%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 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.

@vishwas-droid

vishwas-droid commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

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/XXL 🤖 PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants