mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-06 08:19:45 +08:00
feat: call CallMCPTool in parser
This commit is contained in:
45
parser.go
45
parser.go
@@ -1,6 +1,7 @@
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"context"
|
||||
builtinJSON "encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -18,14 +19,20 @@ import (
|
||||
"github.com/httprunner/funplugin/fungo"
|
||||
"github.com/httprunner/httprunner/v5/code"
|
||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v5/pkg/mcphost"
|
||||
mcp2 "github.com/mark3labs/mcp-go/mcp"
|
||||
)
|
||||
|
||||
func NewParser() *Parser {
|
||||
return &Parser{}
|
||||
return &Parser{
|
||||
ctx: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
type Parser struct {
|
||||
Plugin funplugin.IPlugin // plugin is used to call functions
|
||||
ctx context.Context
|
||||
Plugin funplugin.IPlugin // plugin is used to call functions
|
||||
MCPHost *mcphost.MCPHost
|
||||
}
|
||||
|
||||
func buildURL(baseURL, stepURL string, queryParams url.Values) (fullUrl *url.URL) {
|
||||
@@ -213,7 +220,7 @@ func (p *Parser) ParseString(raw string, variablesMapping map[string]interface{}
|
||||
return raw, err
|
||||
}
|
||||
|
||||
result, err := p.callFunc(funcName, parsedArgs.([]interface{})...)
|
||||
result, err := p.CallFunc(funcName, parsedArgs.([]interface{})...)
|
||||
if err != nil {
|
||||
log.Error().Str("funcName", funcName).Interface("arguments", arguments).
|
||||
Err(err).Msg("call function failed")
|
||||
@@ -275,9 +282,9 @@ func (p *Parser) ParseString(raw string, variablesMapping map[string]interface{}
|
||||
return parsedString, nil
|
||||
}
|
||||
|
||||
// callFunc calls function with arguments
|
||||
// CallFunc calls function with arguments
|
||||
// only support return at most one result value
|
||||
func (p *Parser) callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
||||
func (p *Parser) CallFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
||||
// call with plugin function
|
||||
if p.Plugin != nil {
|
||||
if p.Plugin.Has(funcName) {
|
||||
@@ -300,6 +307,34 @@ func (p *Parser) callFunc(funcName string, arguments ...interface{}) (interface{
|
||||
return fungo.CallFunc(fn, arguments...)
|
||||
}
|
||||
|
||||
// CallMCPTool calls a MCP tool on a specific MCP server
|
||||
func (p *Parser) CallMCPTool(serverName, funcName string, arguments map[string]interface{}) (interface{}, error) {
|
||||
if p.MCPHost == nil {
|
||||
return nil, fmt.Errorf("mcphost is not initialized")
|
||||
}
|
||||
|
||||
tools := p.MCPHost.GetTools(p.ctx)
|
||||
log.Warn().Interface("tools", tools).Msg("tools")
|
||||
|
||||
result, err := p.MCPHost.InvokeTool(p.ctx, serverName, funcName, arguments)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "invoke tool %s/%s failed", serverName, funcName)
|
||||
}
|
||||
if result.IsError {
|
||||
return nil, fmt.Errorf("invoke tool %s/%s failed: %v", serverName, funcName, result.Content)
|
||||
}
|
||||
|
||||
// extract text content
|
||||
var resultText string
|
||||
for _, item := range result.Content {
|
||||
if contentMap, ok := item.(mcp2.TextContent); ok {
|
||||
resultText += fmt.Sprintf("%v ", contentMap.Text)
|
||||
}
|
||||
}
|
||||
|
||||
return resultText, nil
|
||||
}
|
||||
|
||||
// merge two variables mapping, the first variables have higher priority
|
||||
func mergeVariables(variables, overriddenVariables map[string]interface{}) map[string]interface{} {
|
||||
if overriddenVariables == nil {
|
||||
|
||||
Reference in New Issue
Block a user