0908
This commit is contained in:
40
aio-mcp/internal/tools/system/echo.go
Normal file
40
aio-mcp/internal/tools/system/echo.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
// echoArgs 是 echo 工具的输入参数。
|
||||
// json tag 决定参数名;jsonschema tag 提供描述,供客户端(LLM)理解参数。
|
||||
type echoArgs struct {
|
||||
Message string `json:"message" jsonschema:"要回显的文本,必填"`
|
||||
Times int `json:"times,omitempty" jsonschema:"回显次数,默认 1"`
|
||||
}
|
||||
|
||||
// registerEcho 注册 echo 工具:演示带参数的工具写法。
|
||||
func registerEcho(s *mcp.Server) {
|
||||
mcp.AddTool(s, &mcp.Tool{
|
||||
Name: "echo",
|
||||
Description: "把传入的文本原样回显指定次数,用于演示参数传递与工具调用。",
|
||||
}, func(_ context.Context, _ *mcp.CallToolRequest, args echoArgs) (*mcp.CallToolResult, any, error) {
|
||||
times := args.Times
|
||||
if times <= 0 {
|
||||
times = 1
|
||||
}
|
||||
// 用换行分隔,避免重复文本粘连。
|
||||
line := strings.TrimRight(args.Message, "\n")
|
||||
lines := make([]string, times)
|
||||
for i := range lines {
|
||||
lines[i] = line
|
||||
}
|
||||
return &mcp.CallToolResult{
|
||||
Content: []mcp.Content{
|
||||
&mcp.TextContent{Text: fmt.Sprintf("%s\n(共 %d 次)", strings.Join(lines, "\n"), times)},
|
||||
},
|
||||
}, nil, nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user