Skip to content

mcp: add logs tool to retrieve deployed Function logs#3819

Open
Ankitsinghsisodya wants to merge 2 commits into
knative:mainfrom
Ankitsinghsisodya:feat/mcp-logs-tool
Open

mcp: add logs tool to retrieve deployed Function logs#3819
Ankitsinghsisodya wants to merge 2 commits into
knative:mainfrom
Ankitsinghsisodya:feat/mcp-logs-tool

Conversation

@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor

@Ankitsinghsisodya Ankitsinghsisodya commented May 21, 2026

Summary

Adds a logs MCP tool that lets agents retrieve logs from a deployed Function. This closes the observe gap in the agentic workflow: an agent can now deploy a Function, invoke it, and immediately inspect its logs without leaving the MCP context.

Note: This is a re-submission of #3794, which was accidentally closed when the head repository was deleted.

Changes

  • pkg/mcp/tools_logs.go — new logs tool with LogsInput / LogsOutput types and logsHandler. Supports identification by path (reads func.yaml) or by name, with optional namespace, since (time window, e.g. 5m, 1h), and verbose flags. Enforces mutual exclusion of path and name at the handler level. Marked ReadOnlyHint: true and DestructiveHint: false.
  • pkg/mcp/tools_logs_test.go — four unit tests following the existing tool test pattern:
    • TestTool_Logs_Args_ByPath — all flags via path
    • TestTool_Logs_Args_ByName — identification by function name
    • TestTool_Logs_MutuallyExclusive — both path + name → error
    • TestTool_Logs_NoArgs — no args falls through to CLI default (server cwd)
  • pkg/mcp/mcp.go — registers the logs tool and the func://help/logs help resource alongside the existing tools.
  • pkg/mcp/instructions.md — adds ### logs agent guidance and func://help/logs to the pre-invocation checklist.

Test plan

  • make test passes
  • make check passes
  • Manual: func mcp start → call logs tool with a deployed Function path and confirm log output is returned
  • Manual: call logs with both path and name set → confirm error is returned without executing the command

- Introduced a new 'logs' tool to fetch recent logs from deployed functions.
- Updated documentation to include usage instructions for the logs command.
- Added tests to ensure correct argument handling and mutual exclusivity between path and name parameters.

This enhancement allows users to diagnose function behavior by accessing logs, improving the overall usability of the MCP tools.
…xclusivity

- Revised the logs tool description to clarify the usage of the 'since' argument.
- Updated error message in logsHandler to indicate that 'path' and 'name' are mutually exclusive, allowing for at most one instead of exactly one.

These changes enhance the clarity of the tool's documentation and improve user feedback on input errors.
Copilot AI review requested due to automatic review settings May 21, 2026 11:09
@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 21, 2026 11:09
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 21, 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 matejvasek 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 size/L 🤖 PR changes 100-499 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 21, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 21, 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

Note

Copilot was unable to run its full agentic suite in this review.

Adds a new MCP “logs” tool to retrieve Knative Function logs, including server wiring, documentation updates, and test coverage for argument handling.

Changes:

  • Introduces logs MCP tool, handler, and argument mapping (path/name, namespace, since, verbose)
  • Registers the tool and help resource in the MCP server
  • Adds documentation and unit tests for logs tool behavior (args + mutual exclusivity)

Reviewed changes

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

File Description
pkg/mcp/tools_logs.go Adds the logs tool definition, input/output types, and handler implementation
pkg/mcp/tools_logs_test.go Adds tests validating argument translation and mutual exclusivity behavior
pkg/mcp/mcp.go Registers the new tool and adds a help resource entry
pkg/mcp/instructions.md Documents how/when to use the logs tool and its parameters

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

Comment thread pkg/mcp/tools_logs.go
Comment on lines +22 to +30
func (s *Server) logsHandler(ctx context.Context, r *mcp.CallToolRequest, input LogsInput) (result *mcp.CallToolResult, output LogsOutput, err error) {
if input.Path != nil && input.Name != nil {
err = fmt.Errorf("'path' and 'name' are mutually exclusive: provide at most one")
return
}

out, err := s.executor.Execute(ctx, "logs", input.Args()...)
if err != nil {
err = fmt.Errorf("%w\n%s", err, string(out))
Comment on lines +44 to +53
result, err := client.CallTool(t.Context(), &mcp.CallToolParams{
Name: "logs",
Arguments: buildInputArgs(stringFlags, boolFlags),
})
if err != nil {
t.Fatal(err)
}
if result.IsError {
t.Fatalf("unexpected error result: %v", result)
}
@lkingland lkingland added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/L 🤖 PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants