-
Notifications
You must be signed in to change notification settings - Fork 3
Introduce the inspector
command
#157
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||||
package cmd | ||||||||
|
||||||||
import ( | ||||||||
"context" | ||||||||
"errors" | ||||||||
"fmt" | ||||||||
"os" | ||||||||
"os/exec" | ||||||||
"os/signal" | ||||||||
"strings" | ||||||||
"syscall" | ||||||||
|
||||||||
"github.com/spf13/cobra" | ||||||||
|
||||||||
internalcmd "github.com/mozilla-ai/mcpd/v2/internal/cmd" | ||||||||
cmdopts "github.com/mozilla-ai/mcpd/v2/internal/cmd/options" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
"github.com/mozilla-ai/mcpd/v2/internal/runtime" | ||||||||
) | ||||||||
|
||||||||
// InspectorCmd represents the 'inspector' command. | ||||||||
type InspectorCmd struct { | ||||||||
*internalcmd.BaseCmd | ||||||||
} | ||||||||
|
||||||||
// NewInspectorCmd creates a newly configured (Cobra) command. | ||||||||
func NewInspectorCmd(baseCmd *internalcmd.BaseCmd, _ ...cmdopts.CmdOption) (*cobra.Command, error) { | ||||||||
c := &InspectorCmd{ | ||||||||
BaseCmd: baseCmd, | ||||||||
} | ||||||||
|
||||||||
cobraCommand := &cobra.Command{ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ I think perhaps we should mention in the docs (long) which version of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is more of a dev functionality, let's pin it to |
||||||||
Use: "inspector [command] [args]", | ||||||||
Short: "Start the MCP inspector tool", | ||||||||
Long: "Start the MCP inspector tool via npx for quickly testing MCP servers. " + | ||||||||
"Optionally pass your desired command and arguments to the inspector. " + | ||||||||
"Note that the latest version of the inspector package is used (@modelcontextprotocol/inspector@latest). " + | ||||||||
"For more information, see https://modelcontextprotocol.io/docs/tools/inspector.", | ||||||||
RunE: c.run, | ||||||||
} | ||||||||
|
||||||||
return cobraCommand, nil | ||||||||
} | ||||||||
|
||||||||
// run is configured (via NewInspectorCmd) to be called by the Cobra framework when the command is executed. | ||||||||
// It may return an error (or nil, when there is no error, or there was a graceful shutdown). | ||||||||
func (c *InspectorCmd) run(cmd *cobra.Command, args []string) error { | ||||||||
ctx, stop := signal.NotifyContext( | ||||||||
context.Background(), | ||||||||
os.Interrupt, | ||||||||
syscall.SIGTERM, syscall.SIGINT, | ||||||||
) | ||||||||
defer stop() | ||||||||
|
||||||||
// Create a new npx process with the user provided arguments | ||||||||
// Bind the process's stdout and stderr to our own for streaming output | ||||||||
npxArgs := append([]string{"@modelcontextprotocol/inspector@latest"}, args...) | ||||||||
npxCommand := exec.CommandContext(ctx, string(runtime.NPX), npxArgs...) | ||||||||
npxCommand.Stdout = os.Stdout | ||||||||
npxCommand.Stderr = os.Stderr | ||||||||
|
||||||||
_, _ = fmt.Fprintf( | ||||||||
cmd.OutOrStdout(), "Starting the MCP inspector: npx %s...\n", | ||||||||
strings.Join(npxArgs, " "), | ||||||||
) | ||||||||
err := npxCommand.Start() | ||||||||
if err != nil { | ||||||||
return fmt.Errorf("error running the inspector: %w", err) | ||||||||
} | ||||||||
|
||||||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Press Ctrl+C to stop.\n") | ||||||||
err = npxCommand.Wait() | ||||||||
|
||||||||
if errors.Is(ctx.Err(), context.Canceled) { | ||||||||
// Graceful shutdown with Ctrl+C (or SIGTERM) | ||||||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\nShutting down the inspector...\n") | ||||||||
return nil | ||||||||
} | ||||||||
|
||||||||
if err != nil { | ||||||||
return fmt.Errorf("failed to run the inspector: %w", err) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
} | ||||||||
|
||||||||
return nil | ||||||||
} |
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.
Required for
errors.Is
.