-
Notifications
You must be signed in to change notification settings - Fork 221
feat(mcp): add 'describe' tool and update documentation #3995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ankitsinghsisodya
wants to merge
7
commits into
knative:main
Choose a base branch
from
Ankitsinghsisodya:mcp-describe-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d4dbb84
feat(mcp): add 'describe' tool and update documentation
Ankitsinghsisodya 1da4fd0
feat(mcp): enhance describe tool to handle leading stderr warnings
Ankitsinghsisodya 309b216
feat(mcp): implement ExecuteSplit for improved stdout/stderr handling
Ankitsinghsisodya d87a9f9
Merge remote-tracking branch 'upstream/main' into mcp-describe-tool
Ankitsinghsisodya ff1d472
feat(mcp): enhance describe tool with middleware support and validati…
Ankitsinghsisodya ca9ff86
re-running-ci
Ankitsinghsisodya 29e164d
re-running-ci
Ankitsinghsisodya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| fn "knative.dev/func/pkg/functions" | ||
| ) | ||
|
|
||
| var describeTool = &mcp.Tool{ | ||
| Name: "describe", | ||
| Title: "Describe Function", | ||
| Description: "Describe a deployed Function: URL, routes, image, namespace, deployer, labels, revision, readiness, and event subscriptions.", | ||
| Annotations: &mcp.ToolAnnotations{ | ||
| Title: "Describe Function", | ||
| ReadOnlyHint: true, | ||
| IdempotentHint: true, // Describing the same function multiple times returns consistent results at any point in time. | ||
| }, | ||
| } | ||
|
|
||
| func (s *Server) describeHandler(ctx context.Context, r *mcp.CallToolRequest, input DescribeInput) (result *mcp.CallToolResult, output DescribeOutput, err error) { | ||
| // Validate: exactly one of Path or Name must be provided | ||
| if (input.Path != nil && input.Name != nil) || (input.Path == nil && input.Name == nil) { | ||
| err = fmt.Errorf("exactly one of 'path' or 'name' must be provided") | ||
| return | ||
| } | ||
|
Ankitsinghsisodya marked this conversation as resolved.
|
||
|
|
||
| // Validate: namespace only makes sense alongside 'name'. When describing | ||
| // by 'path', the Function's name and namespace are read from its own | ||
| // deploy identity (func.yaml); the CLI rejects a separate --namespace in | ||
| // that mode ("must also specify a name when specifying namespace"). | ||
| if input.Path != nil && input.Namespace != nil { | ||
| err = fmt.Errorf("'namespace' is only valid with 'name'; when describing by 'path', the namespace is read from the Function's own deploy identity") | ||
| return | ||
| } | ||
|
|
||
| // ExecuteSplit (rather than Execute/CombinedOutput) is required here: | ||
| // the CLI can write warnings to stderr on an otherwise-successful call | ||
| // (e.g. permission warnings from the knative describer), and stdout and | ||
| // stderr copied via CombinedOutput have no guaranteed relative ordering. | ||
| // Parsing JSON only ever out of a clean, unmixed stdout avoids that | ||
| // entirely rather than relying on any heuristic about stream ordering. | ||
| stdout, stderr, err := s.executor.ExecuteSplit(ctx, "describe", input.Args()...) | ||
| if err != nil { | ||
| err = fmt.Errorf("%w\nstdout: %s\nstderr: %s", err, string(stdout), string(stderr)) | ||
| return | ||
| } | ||
|
Ankitsinghsisodya marked this conversation as resolved.
|
||
|
|
||
| var instance fn.Instance | ||
| if err = json.Unmarshal(stdout, &instance); err != nil { | ||
| err = fmt.Errorf("failed to parse describe output: %w\n%s", err, string(stdout)) | ||
| return | ||
| } | ||
|
|
||
| var middleware *fn.Middleware | ||
| if instance.Middleware.Version != "" { | ||
| middleware = &instance.Middleware | ||
| } | ||
|
|
||
| output = DescribeOutput{ | ||
|
Ankitsinghsisodya marked this conversation as resolved.
|
||
| Name: instance.Name, | ||
| Namespace: instance.Namespace, | ||
| URL: instance.Route, | ||
| Routes: instance.Routes, | ||
| Image: instance.Image, | ||
| Ready: instance.Ready, | ||
| Deployer: instance.Deployer, | ||
| Labels: instance.Labels, | ||
| Subscriptions: instance.Subscriptions, | ||
| Middleware: middleware, | ||
| Revision: instance.Revision, | ||
| // A non-fatal warning on stderr (e.g. RBAC denying the eventing | ||
| // trigger list) can accompany an otherwise-successful, but partial, | ||
| // JSON payload on stdout (e.g. an empty subscriptions list). | ||
| // Surface it so the agent doesn't mistake "no subscriptions" for | ||
| // "no permission to see them". | ||
| Warnings: strings.TrimSpace(string(stderr)), | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // DescribeInput defines the input parameters for the describe tool. | ||
| // Exactly one of Path or Name must be provided. | ||
| type DescribeInput struct { | ||
| Path *string `json:"path,omitempty" jsonschema:"Path to the function project directory (mutually exclusive with name)"` | ||
| Name *string `json:"name,omitempty" jsonschema:"Name of the function to describe (mutually exclusive with path)"` | ||
| Namespace *string `json:"namespace,omitempty" jsonschema:"Kubernetes namespace to describe from (default: current or active namespace). Only valid together with 'name'; when describing by 'path' the namespace is read from the Function's own deploy identity"` | ||
| Verbose *bool `json:"verbose,omitempty" jsonschema:"Enable verbose logging output"` | ||
| } | ||
|
|
||
| func (i DescribeInput) Args() []string { | ||
| args := []string{} | ||
|
|
||
| // Either path flag or positional name argument | ||
| if i.Path != nil { | ||
| args = append(args, "--path", *i.Path) | ||
| } else if i.Name != nil { | ||
| args = append(args, *i.Name) | ||
| } | ||
|
|
||
| args = appendStringFlag(args, "--namespace", i.Namespace) | ||
| args = appendBoolFlag(args, "--verbose", i.Verbose) | ||
|
|
||
| // The tool's contract is structured JSON, regardless of caller input. | ||
| args = append(args, "--output", "json") | ||
|
Ankitsinghsisodya marked this conversation as resolved.
|
||
| return args | ||
| } | ||
|
|
||
| // DescribeOutput defines the structured output returned by the describe tool. | ||
| type DescribeOutput struct { | ||
| Name string `json:"name" jsonschema:"Function name"` | ||
| Namespace string `json:"namespace,omitempty" jsonschema:"Kubernetes namespace"` | ||
| URL string `json:"url,omitempty" jsonschema:"Primary route URL"` | ||
| Routes []string `json:"routes,omitempty" jsonschema:"All route URLs"` | ||
| Image string `json:"image,omitempty" jsonschema:"Deployed container image"` | ||
| Ready string `json:"ready,omitempty" jsonschema:"Overall readiness (true/false/unknown)"` | ||
| Deployer string `json:"deployer,omitempty" jsonschema:"Deployer backend (knative, k8s, keda)"` | ||
| Labels map[string]string `json:"labels,omitempty" jsonschema:"Function labels"` | ||
| Subscriptions []fn.Subscription `json:"subscriptions,omitempty" jsonschema:"Active event subscriptions"` | ||
| Middleware *fn.Middleware `json:"middleware,omitempty" jsonschema:"Middleware backend (e.g. keda) applied at deploy time, if any"` | ||
| Revision string `json:"revision,omitempty" jsonschema:"Source commit SHA, read from the OCI revision label baked into the built image"` | ||
|
Ankitsinghsisodya marked this conversation as resolved.
|
||
| Warnings string `json:"warnings,omitempty" jsonschema:"Non-fatal warnings emitted while gathering this Function's status (e.g. permission errors that caused partial data, such as an empty subscriptions list)"` | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.