Skip to content

feat(mcp): add run and run_stop tools to the MCP server - #3998

Open
Ankitsinghsisodya wants to merge 4 commits into
knative:mainfrom
Ankitsinghsisodya:feat/mcp-run-tools
Open

feat(mcp): add run and run_stop tools to the MCP server#3998
Ankitsinghsisodya wants to merge 4 commits into
knative:mainfrom
Ankitsinghsisodya:feat/mcp-run-tools

Conversation

@Ankitsinghsisodya

Copy link
Copy Markdown
Contributor

Summary

Adds two new MCP tools that let clients start and stop a Function locally through the MCP server, mirroring func run:

  • run — builds the Function if needed, starts it in the background, and returns once it's ready with its pid and url. Accepts optional path, registry, build, and port parameters. Only one run is allowed per Function path at a time.
  • run_stop — gracefully stops a Function previously started with run (SIGTERM, then SIGKILL after a grace period), matched by the same absolute path.

Both tools are disabled when the server is running in read-only mode, consistent with deploy/delete.

Implementation details

  • pkg/mcp/process.go: new processStarter abstraction that spawns func run --json as a subprocess, parses its stdout for a JSON readiness line (host/port), and returns a stop func to terminate it. Also introduces a runRegistry to track active runs keyed by function path.
  • pkg/mcp/tools_run.go / pkg/mcp/tools_run_stop.go: tool definitions, input/output schemas, and handlers for run and run_stop.
  • pkg/mcp/mock/process_starter.go: mock processStarter for tests, injected via new WithProcessStarter server option.
  • pkg/mcp/mcp.go: wires up the new tools and default process starter/run registry on the server.
  • pkg/mcp/instructions.md / pkg/mcp/instructions_warning.md: document the new tools' usage/path requirements and note them as disabled in read-only mode.

Test plan

  • make test — new unit tests in pkg/mcp/process_test.go, pkg/mcp/tools_run_test.go, and pkg/mcp/tools_run_stop_test.go cover readonly rejection, duplicate-run rejection, start errors, and stop success/failure paths using the mock process starter.
  • make check

- Introduced 'run' tool for executing Functions locally, including detailed usage instructions.
- Added 'run_stop' tool to stop Functions that were started with 'run'.
- Updated documentation to reflect these new tools and their parameters.
- Enhanced server structure to track active local runs.

This update enhances the local development experience by allowing users to run and manage Functions directly from the MCP server.
- Updated formatting in mcp.go for better readability.
- Replaced b.WriteString with fmt.Fprintf in process_test.go for consistency in string formatting.
- Added missing newline at the end of tools_run_test.go to adhere to file formatting standards.

These changes enhance code clarity and maintainability across the MCP package.
Copilot AI lite review requested due to automatic review settings August 7, 2026 19:24
@knative-prow
knative-prow Bot requested review from dsimansk and jrangelramos August 7, 2026 19:24

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@knative-prow

knative-prow Bot commented Aug 7, 2026

Copy link
Copy Markdown

[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 jrangelramos 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 added the size/XL 🤖 PR changes 500-999 lines, ignoring generated files. label Aug 7, 2026
@Ankitsinghsisodya
Ankitsinghsisodya marked this pull request as draft August 7, 2026 19:25
@knative-prow knative-prow Bot added the do-not-merge/work-in-progress 🤖 PR should not merge because it is a work in progress. label Aug 7, 2026

@lkingland lkingland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Love that this one is starting to show why an MCP server helps do things a simple skill cannot do as elegantly.

Small suggestions around error and exit code handling below:

Comment thread pkg/mcp/tools_run.go Outdated
Comment thread pkg/mcp/process.go
Comment thread pkg/mcp/process.go Outdated
Comment thread pkg/mcp/tools_run_stop.go Outdated
Comment thread pkg/mcp/tools_run.go Outdated
Comment thread pkg/mcp/instructions_warning.md Outdated
Comment thread pkg/mcp/tools_run.go Outdated
Comment thread pkg/mcp/tools_run_stop.go Outdated
- Updated documentation to clarify that local operations (run/stop) are allowed even in read-only mode.
- Improved the handling of function runs, ensuring that stopping a function with no active run is idempotent and succeeds without error.
- Refactored the run registry to support reserving and activating function paths, preventing concurrent run conflicts.
- Adjusted the run and run_stop tools to reflect the new behavior and requirements for absolute paths.

These changes improve the usability of the MCP server for local function management while maintaining safety in read-only environments.
@knative-prow knative-prow Bot added size/XXL 🤖 PR changes 1000+ lines, ignoring generated files. and removed size/XL 🤖 PR changes 500-999 lines, ignoring generated files. labels Aug 10, 2026
@Ankitsinghsisodya
Ankitsinghsisodya marked this pull request as ready for review August 10, 2026 19:59
@knative-prow knative-prow Bot removed the do-not-merge/work-in-progress 🤖 PR should not merge because it is a work in progress. label Aug 10, 2026
- Replaced hardcoded temporary paths with a new utility function `testAbsPath` to ensure platform-native absolute paths are used in tests for the 'run' and 'run_stop' tools.
- This change enhances compatibility across different operating systems and improves the reliability of function execution in tests.

These updates contribute to better path management in the MCP server's local function tools.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XXL 🤖 PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants