You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.9 KiB
122 lines
2.9 KiB
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"yunyan/lego/core"
|
|
"yunyan/lego/core/cbase"
|
|
"yunyan/lego/sys/log"
|
|
lgspotify "yunyan/sys/spotify"
|
|
"yunyan/utils"
|
|
"fmt"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/zmb3/spotify/v2"
|
|
)
|
|
|
|
// 音乐卡片 结构统一化输出
|
|
type SpotifyMusicCard struct {
|
|
Name string `json:"name"`
|
|
Images string `json:"image"`
|
|
Url string `json:"url"`
|
|
Sgener string `json:"sgener"`
|
|
}
|
|
|
|
// 天气查询
|
|
type tool_spotify_music_play struct {
|
|
cbase.ModuleCompBase
|
|
module *Mcp
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *tool_spotify_music_play) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.ModuleCompBase.Init(service, module, comp, opt)
|
|
this.module = module.(*Mcp)
|
|
|
|
return
|
|
}
|
|
|
|
func (this *tool_spotify_music_play) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
if this.module.AddTool(ToolGroup_GLOBAL, this.Tool(), this.Handl) {
|
|
if err := lgspotify.OnInit(this.module.service.GetSettings().Sys["spotify"]); err != nil {
|
|
panic(fmt.Sprintf("init sys.spotify err: %s", err.Error()))
|
|
} else {
|
|
log.Infof("init sys.spotify success!")
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *tool_spotify_music_play) Tool() (tool mcp.Tool) {
|
|
return mcp.NewTool("spotify_music_play",
|
|
mcp.WithDescription("为用户提供音乐播放服务"),
|
|
mcp.WithString("query",
|
|
mcp.Description("Music keyword information"),
|
|
mcp.Required(),
|
|
),
|
|
mcp.WithString("offset",
|
|
mcp.Description("music offset (max 9, default 0)"),
|
|
mcp.DefaultNumber(0),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Handl 工具处理接口
|
|
// 参数:
|
|
// - ctx: 上下文
|
|
// - request: MCP 工具调用请求
|
|
//
|
|
// 返回值:
|
|
// - result: MCP 工具调用结果
|
|
// - err: 处理过程中的错误
|
|
//
|
|
// 异常:
|
|
// - 无(不主动 panic)
|
|
func (this *tool_spotify_music_play) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
|
|
var (
|
|
offset int
|
|
query string
|
|
data []spotify.FullTrack
|
|
card *SpotifyMusicCard
|
|
context string
|
|
)
|
|
offset = request.GetInt("offset", 0)
|
|
if query, err = request.RequireString("query"); err != nil {
|
|
return
|
|
}
|
|
data, err = lgspotify.SearchMusic(ctx, query, 1, offset)
|
|
|
|
if err != nil {
|
|
context = "播放音乐失败"
|
|
log.Errorf("获取天气信息失败:%v", err)
|
|
return
|
|
} else {
|
|
card = &SpotifyMusicCard{
|
|
Name: data[0].Name,
|
|
Images: data[0].Album.Images[0].URL,
|
|
Url: string(data[0].URI),
|
|
Sgener: data[0].Album.Name,
|
|
}
|
|
context = utils.ToString(map[string]interface{}{
|
|
"card_music": card,
|
|
"result": fmt.Sprintf("正在为你播放音乐:%s", data[0].Name),
|
|
})
|
|
log.Debug("播放音乐成功",
|
|
log.Field{Key: "query", Value: query},
|
|
log.Field{Key: "card", Value: card},
|
|
)
|
|
}
|
|
|
|
result = &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: context,
|
|
},
|
|
},
|
|
}
|
|
// result.Meta = map[string]interface{}{
|
|
// "card_spotify": card,
|
|
// }
|
|
return
|
|
}
|
|
|