This repository was archived by the owner on Jun 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
feat: MCP phase 1 read-only operations #579
Draft
kfelternv
wants to merge
7
commits into
NVIDIA:main
Choose a base branch
from
kfelternv:feat-cli-mcp-mode
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7e8f02a
feat: Add nico-mcp server mode to nicocli
kfelternv 4cfc3ba
fix: Address CodeRabbit review feedback for MCP mode
kfelternv eec5d99
refactor: Make MCP serve param-driven and drop config-file loading
kfelternv ef42717
feat: Surface NICo REST pagination metadata in MCP tool results
kfelternv 1f2d0e5
fix: Harden MCP path and schema validation
kfelternv dce5e27
docs: Clarify MCP serve path flag behavior
kfelternv 3776f57
fix: Tighten MCP tool argument validation
kfelternv 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
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,175 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package mcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
| urfave "github.com/urfave/cli/v2" | ||
| ) | ||
|
|
||
| // Command returns the "mcp" urfave/cli command tree for nicocli. Wire | ||
| // it into the binary's command list from cmd/cli/main.go alongside the | ||
| // dynamically-generated commands the rest of the CLI ships with. | ||
| // | ||
| // specData is the OpenAPI YAML the rest of the CLI is built from; the | ||
| // command's "serve" action passes it to BuildServer so the MCP tool | ||
| // catalogue stays in lockstep with every nicocli build. | ||
| func Command(specData []byte) *urfave.Command { | ||
| return &urfave.Command{ | ||
| Name: "mcp", | ||
| Usage: "Run an MCP server that exposes the NICo REST read surface over streamable-HTTP", | ||
| Subcommands: []*urfave.Command{ | ||
| serveCommand(specData), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func serveCommand(specData []byte) *urfave.Command { | ||
| return &urfave.Command{ | ||
| Name: "serve", | ||
| Usage: "Start the streamable-HTTP MCP server", | ||
| Description: "Serves the NICo REST read surface as MCP tools at /mcp on the\n" + | ||
| "configured listen address. The server is stateless and never emits\n" + | ||
| "text/event-stream responses; every tool/call returns a single JSON\n" + | ||
| "body. In production, place an MCP-aware gateway in front and rely on\n" + | ||
| "the inbound Authorization header for per-call authentication.", | ||
| Flags: []urfave.Flag{ | ||
| &urfave.StringFlag{ | ||
| Name: "listen", | ||
| Usage: "address:port to listen on", | ||
| EnvVars: []string{"NICO_MCP_LISTEN"}, | ||
| Value: ":8080", | ||
| }, | ||
| &urfave.StringFlag{ | ||
| Name: "path", | ||
| Usage: "HTTP path the MCP handler is mounted at", | ||
| EnvVars: []string{"NICO_MCP_PATH"}, | ||
| Value: "/mcp", | ||
| }, | ||
| &urfave.DurationFlag{ | ||
| Name: "shutdown-timeout", | ||
| Usage: "graceful shutdown timeout when SIGINT/SIGTERM arrives", | ||
| EnvVars: []string{"NICO_MCP_SHUTDOWN_TIMEOUT"}, | ||
| Value: 10 * time.Second, | ||
| }, | ||
| }, | ||
| Action: func(c *urfave.Context) error { | ||
| return runServe(c, specData) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // runServe wires the urfave context into Options, builds the MCP server, | ||
| // and runs an http.Server until SIGINT/SIGTERM. It is split out from the | ||
| // urfave Action closure so tests can drive it directly. | ||
| func runServe(c *urfave.Context, specData []byte) error { | ||
| opts := buildServeOptions(c) | ||
|
|
||
| server, err := BuildServer(specData, opts) | ||
| if err != nil { | ||
| return fmt.Errorf("building MCP server: %w", err) | ||
| } | ||
|
|
||
| listen := c.String("listen") | ||
| path := c.String("path") | ||
| if path == "" || path[0] != '/' { | ||
| return fmt.Errorf("invalid --path %q: must be non-empty and start with '/'", path) | ||
| } | ||
| shutdownTimeout := c.Duration("shutdown-timeout") | ||
|
|
||
| mux := http.NewServeMux() | ||
| if err := registerHandler(mux, path, NewHandler(server)); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| httpServer := &http.Server{ | ||
| Addr: listen, | ||
| Handler: mux, | ||
| ReadHeaderTimeout: 10 * time.Second, | ||
| } | ||
|
|
||
| opts.Log.Infof("nico-mcp: listening on %s, MCP at %s (stateless, JSONResponse)", listen, path) | ||
|
|
||
| errCh := make(chan error, 1) | ||
| go func() { | ||
| errCh <- httpServer.ListenAndServe() | ||
| }() | ||
|
|
||
| sigCh := make(chan os.Signal, 1) | ||
| signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) | ||
| defer signal.Stop(sigCh) | ||
|
|
||
| select { | ||
| case err := <-errCh: | ||
| if err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
| return fmt.Errorf("http server: %w", err) | ||
| } | ||
| return nil | ||
| case sig := <-sigCh: | ||
| opts.Log.Infof("nico-mcp: received %s, shutting down", sig) | ||
| ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) | ||
| defer cancel() | ||
| if err := httpServer.Shutdown(ctx); err != nil { | ||
| return fmt.Errorf("graceful shutdown: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func registerHandler(mux *http.ServeMux, path string, handler http.Handler) (err error) { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| err = fmt.Errorf("invalid --path %q: %v", path, r) | ||
| } | ||
| }() | ||
| mux.Handle(path, handler) | ||
| return nil | ||
| } | ||
|
|
||
| // buildServeOptions resolves the MCP server's start-up defaults from the | ||
| // process flags (each of which also reads its NICO_* environment | ||
| // variable). Unlike the dynamically-generated commands, mcp serve does | ||
| // NOT read ~/.nico/config.yaml: the server is stateless and entirely | ||
| // parameter-driven, so every connection detail is supplied per tool call | ||
| // via resolveCallConfig, with these flag values as the only fallback. | ||
| // This lets "nicocli mcp serve" start cleanly with no config file present. | ||
| func buildServeOptions(c *urfave.Context) Options { | ||
| log := logrus.NewEntry(logrus.StandardLogger()) | ||
| if c.Bool("debug") { | ||
| log.Logger.SetLevel(logrus.DebugLevel) | ||
| } | ||
|
|
||
| return Options{ | ||
| BaseURL: c.String("base-url"), | ||
| Org: c.String("org"), | ||
| APIName: c.String("api-name"), | ||
| Token: c.String("token"), | ||
| TokenCommand: c.String("token-command"), | ||
| Debug: c.Bool("debug"), | ||
| Log: log, | ||
| } | ||
| } | ||
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,38 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package mcp | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRegisterHandler_ValidPath(t *testing.T) { | ||
| mux := http.NewServeMux() | ||
| err := registerHandler(mux, "/mcp", http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestRegisterHandler_InvalidPatternReturnsError(t *testing.T) { | ||
| mux := http.NewServeMux() | ||
| err := registerHandler(mux, "/{", http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "invalid --path") | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: NVIDIA/infra-controller-rest
Length of output: 171
🏁 Script executed:
Repository: NVIDIA/infra-controller-rest
Length of output: 146
🏁 Script executed:
Repository: NVIDIA/infra-controller-rest
Length of output: 54
🏁 Script executed:
Repository: NVIDIA/infra-controller-rest
Length of output: 1846
🏁 Script executed:
Repository: NVIDIA/infra-controller-rest
Length of output: 671
🏁 Script executed:
Repository: NVIDIA/infra-controller-rest
Length of output: 167
🏁 Script executed:
Repository: NVIDIA/infra-controller-rest
Length of output: 9983
Stop signal notifications on exit to avoid lingering signal registrations.
signal.Notify(sigCh, ...)registerssigChwith the runtime, but this code never callssignal.Stop(sigCh), so repeated in-process invocations (e.g., tests) can accumulate stale registrations even after the server goroutine exits.Proposed fix
sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) + defer signal.Stop(sigCh)📝 Committable suggestion
🤖 Prompt for AI Agents