Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions cmd/inspector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cmd

import (
"context"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"context"
"context"
"errors"

Required for errors.Is.

"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"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
cmdopts "github.com/mozilla-ai/mcpd/v2/internal/cmd/options"
cmdopts "github.com/mozilla-ai/mcpd/v2/internal/cmd/options"
"github.com/mozilla-ai/mcpd/v2/internal/runtime"

"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{
Copy link
Contributor

Choose a reason for hiding this comment

The 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 @modelcontextprotocol/inspector we run... or pin it explicitly to latest.

Copy link
Author

Choose a reason for hiding this comment

The 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 latest, so we don't have to update the version.

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("failed to run the inspector: %w", err)
return fmt.Errorf("error running the inspector: %w", err)

}

return nil
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func NewRootCmd(c *RootCmd) (*cobra.Command, error) {
NewRemoveCmd,
NewDaemonCmd,
config.NewConfigCmd,
NewInspectorCmd,
}

for _, fn := range fns {
Expand Down
9 changes: 9 additions & 0 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ You can also restrict access to allow only specific tools:
mcpd add time --tool get_current_time
```

!!! tip "Experiment with MCP servers"
You can quickly experiment with MCP servers before properly adding them to
the `mcpd` configuration, by running the inspector tool:
```bash
mcpd inspector
```
For more information, please refer to the [official documentation](https://modelcontextprotocol.io/docs/tools/inspector)
of the tool.

---

## 4. Set Startup Arguments
Expand Down