mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-10 17:43:00 +08:00
feat: invoke tool in hrp server
This commit is contained in:
@@ -12,13 +12,21 @@ var serverCmd = &cobra.Command{
|
||||
Long: `start hrp server, call httprunner by HTTP`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return server.NewRouter().Run(port)
|
||||
router := server.NewRouter()
|
||||
if mcpConfigPath != "" {
|
||||
router.InitMCPHub(mcpConfigPath)
|
||||
}
|
||||
return router.Run(port)
|
||||
},
|
||||
}
|
||||
|
||||
var port int
|
||||
var (
|
||||
port int
|
||||
mcpConfigPath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(serverCmd)
|
||||
serverCmd.Flags().IntVarP(&port, "port", "p", 8082, "Port to run the server on")
|
||||
serverCmd.Flags().IntVarP(&port, "port", "p", 8082, "port to run the server on")
|
||||
serverCmd.Flags().StringVarP(&mcpConfigPath, "mcp-config", "c", "", "path to the MCP config file")
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
v5.0.0-beta-2504012111
|
||||
v5.0.0-beta-2504012155
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/mcp"
|
||||
"github.com/httprunner/httprunner/v5/uixt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -20,6 +22,24 @@ func NewRouter() *Router {
|
||||
|
||||
type Router struct {
|
||||
*gin.Engine
|
||||
mcpHub *mcp.MCPHub
|
||||
}
|
||||
|
||||
func (r *Router) InitMCPHub(configPath string) error {
|
||||
mcpHub, err := mcp.NewMCPHub(configPath)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("init MCP hub failed")
|
||||
return err
|
||||
}
|
||||
|
||||
err = mcpHub.InitServers(context.Background())
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("init MCP servers failed")
|
||||
return err
|
||||
}
|
||||
|
||||
r.mcpHub = mcpHub
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Router) Init() {
|
||||
@@ -30,6 +50,9 @@ func (r *Router) Init() {
|
||||
r.Engine.GET("/api/v1/devices", r.listDeviceHandler)
|
||||
r.Engine.POST("/api/v1/browser/create_browser", createBrowserHandler)
|
||||
|
||||
// tool operations
|
||||
r.Engine.POST("/api/v1/tool/invoke", r.invokeToolHandler)
|
||||
|
||||
apiV1PlatformSerial := r.Group("/api/v1").Group("/:platform").Group("/:serial")
|
||||
|
||||
// UI operations
|
||||
|
||||
34
server/tool.go
Normal file
34
server/tool.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ToolRequest struct {
|
||||
ServerName string `json:"server_name"`
|
||||
ToolName string `json:"tool_name"`
|
||||
Args map[string]interface{} `json:"args"`
|
||||
}
|
||||
|
||||
func (r *Router) invokeToolHandler(c *gin.Context) {
|
||||
if r.mcpHub == nil {
|
||||
RenderError(c, errors.New("mcp hub not initialized"))
|
||||
return
|
||||
}
|
||||
|
||||
var req ToolRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
RenderError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := r.mcpHub.InvokeTool(c.Request.Context(),
|
||||
req.ServerName, req.ToolName, req.Args)
|
||||
if err != nil {
|
||||
RenderError(c, err)
|
||||
return
|
||||
}
|
||||
RenderSuccess(c, result)
|
||||
}
|
||||
@@ -71,3 +71,43 @@ func TestTapHandler(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvokeToolHandler(t *testing.T) {
|
||||
router := NewRouter()
|
||||
router.InitMCPHub("../internal/mcp/test.mcp.json")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
toolReq ToolRequest
|
||||
wantStatus int
|
||||
}{
|
||||
{
|
||||
name: "invoke tool",
|
||||
path: "/api/v1/tool/invoke",
|
||||
toolReq: ToolRequest{
|
||||
ServerName: "weather",
|
||||
ToolName: "get_alerts",
|
||||
Args: map[string]interface{}{"state": "CA"},
|
||||
},
|
||||
wantStatus: http.StatusOK,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
reqBody, _ := json.Marshal(tt.toolReq)
|
||||
req := httptest.NewRequest(http.MethodPost, tt.path, bytes.NewBuffer(reqBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, tt.wantStatus, w.Code)
|
||||
|
||||
var got HttpResponse
|
||||
err := json.Unmarshal(w.Body.Bytes(), &got)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user