feat: hrp mcphost

This commit is contained in:
lilong.129
2025-05-16 12:18:57 +08:00
parent ce38ef3be0
commit f8b7a42560
36 changed files with 564 additions and 55 deletions

View File

@@ -12,8 +12,8 @@ import (
var CmdBuild = &cobra.Command{
Use: "build $path ...",
Short: "build plugin for testing",
Long: `build python/go plugin for testing`,
Short: "Build plugin for testing",
Long: `Build python/go plugin for testing`,
Example: ` $ hrp build plugin/debugtalk.go
$ hrp build plugin/debugtalk.py`,
Args: cobra.ExactArgs(1),

View File

@@ -21,6 +21,7 @@ func addAllCommands() {
cmd.RootCmd.AddCommand(cmd.CmdScaffold)
cmd.RootCmd.AddCommand(cmd.CmdServer)
cmd.RootCmd.AddCommand(cmd.CmdWiki)
cmd.RootCmd.AddCommand(cmd.CmdMCPHost)
cmd.RootCmd.AddCommand(ios.CmdIOSRoot)
cmd.RootCmd.AddCommand(adb.CmdAndroidRoot)

View File

@@ -16,7 +16,7 @@ import (
var CmdConvert = &cobra.Command{
Use: "convert $path...",
Short: "convert multiple source format to HttpRunner JSON/YAML/gotest/pytest cases",
Short: "Convert multiple source format to HttpRunner JSON/YAML/gotest/pytest cases",
Args: cobra.MinimumNArgs(1),
SilenceUsage: false,
RunE: func(cmd *cobra.Command, args []string) error {

49
cmd/mcphost.go Normal file
View File

@@ -0,0 +1,49 @@
package cmd
import (
"context"
"fmt"
"github.com/httprunner/httprunner/v5/pkg/mcphost"
"github.com/spf13/cobra"
)
// CmdMCPHost represents the mcphost command
var CmdMCPHost = &cobra.Command{
Use: "mcphost",
Short: "Export MCP server tools to JSON description",
Long: `Export all tools from MCP servers to JSON description.
The tools will be exported with their descriptions, parameters, and return values.`,
RunE: func(cmd *cobra.Command, args []string) error {
// Create MCP host
host, err := mcphost.NewMCPHost(mcpConfigPath)
if err != nil {
return fmt.Errorf("failed to create MCP host: %w", err)
}
// Initialize servers
ctx := context.Background()
if err := host.InitServers(ctx); err != nil {
return fmt.Errorf("failed to initialize MCP servers: %w", err)
}
defer host.CloseServers()
// Export tools to JSON
if dumpPath != "" {
if err := host.ExportToolsToJSON(ctx, dumpPath); err != nil {
return err
}
}
return nil
},
}
var (
mcpConfigPath string
dumpPath string
)
func init() {
CmdMCPHost.Flags().StringVarP(&mcpConfigPath, "mcp-config", "c", "$HOME/.hrp/mcp.json", "path to the MCP config file")
CmdMCPHost.Flags().StringVar(&dumpPath, "dump", "", "path to save the exported tools JSON file")
}

View File

@@ -15,7 +15,7 @@ import (
var CmdPytest = &cobra.Command{
Use: "pytest $path ...",
Short: "run API test with pytest",
Short: "Run API test with pytest",
Args: cobra.MinimumNArgs(1),
DisableFlagParsing: true, // allow to pass any args to pytest
RunE: func(cmd *cobra.Command, args []string) (err error) {

View File

@@ -9,8 +9,8 @@ import (
// runCmd represents the run command
var CmdRun = &cobra.Command{
Use: "run $path...",
Short: "run API test with go engine",
Long: `run yaml/json testcase files for API test`,
Short: "Run API test with go engine",
Long: `Run yaml/json testcase files for API test`,
Example: ` $ hrp run demo.json # run specified json testcase file
$ hrp run demo.yaml # run specified yaml testcase file
$ hrp run examples/ # run testcases in specified folder`,

View File

@@ -12,8 +12,8 @@ import (
var CmdScaffold = &cobra.Command{
Use: "startproject $project_name",
Aliases: []string{"scaffold"},
Short: "create a scaffold project",
Args: cobra.ExactValidArgs(1),
Short: "Create a scaffold project",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
if !ignorePlugin && !genPythonPlugin && !genGoPlugin {
return errors.New("please specify function plugin type")

View File

@@ -10,8 +10,8 @@ import (
// serverCmd represents the server command
var CmdServer = &cobra.Command{
Use: "server start",
Short: "start hrp server",
Long: `start hrp server, call httprunner by HTTP`,
Short: "Start hrp server",
Long: `Start hrp server, call httprunner by HTTP`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
router := server.NewRouter()
@@ -23,10 +23,7 @@ var CmdServer = &cobra.Command{
},
}
var (
port int
mcpConfigPath string
)
var port int
func init() {
CmdServer.Flags().IntVarP(&port, "port", "p", 8082, "port to run the server on")