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.
113 lines
3.0 KiB
113 lines
3.0 KiB
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"yunyan/lego/core"
|
|
"yunyan/lego/core/cbase"
|
|
"yunyan/lego/sys/log"
|
|
"yunyan/sys/juhe"
|
|
"fmt"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
)
|
|
|
|
// 股票信息卡片
|
|
type FinanceCard struct {
|
|
CompanyName string `json:"companyName"`
|
|
CurrPrice string `json:"currPrice"`
|
|
OpenPrice string `json:"openPrice"`
|
|
TodayLow string `json:"todayLow"`
|
|
TodayHigh string `json:"todayHigh"`
|
|
Volume string `json:"volume"`
|
|
}
|
|
|
|
// 股票查询
|
|
type tool_finance struct {
|
|
cbase.ModuleCompBase
|
|
module *Mcp
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *tool_finance) 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_finance) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
if this.module.AddTool(ToolGroup_CHINA, this.Tool(), this.Handl) {
|
|
if err := juhe.OnInit(this.module.service.GetSettings().Sys["juhe"]); err != nil {
|
|
panic(fmt.Sprintf("init sys.juhe err: %s", err.Error()))
|
|
} else {
|
|
log.Infof("init sys.juhe success!")
|
|
}
|
|
}
|
|
return
|
|
}
|
|
func (this *tool_finance) Tool() (tool mcp.Tool) {
|
|
return mcp.NewTool("finance_query",
|
|
mcp.WithDescription("根据提供的股票代码查询股票信息"),
|
|
mcp.WithString("code",
|
|
mcp.Description("股票代码"),
|
|
mcp.Required(),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Handl 工具处理接口
|
|
// 参数:
|
|
// - ctx: 上下文
|
|
// - request: MCP 工具调用请求
|
|
//
|
|
// 返回值:
|
|
// - result: MCP 工具调用结果
|
|
// - err: 处理过程中的错误
|
|
//
|
|
// 异常:
|
|
// - 无(不主动 panic)
|
|
func (this *tool_finance) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
|
|
var (
|
|
code string
|
|
data *juhe.StockResponse
|
|
card *FinanceCard
|
|
context string
|
|
)
|
|
if code, err = request.RequireString("code"); err != nil {
|
|
return
|
|
}
|
|
if data, err = juhe.Financebygid(code); err != nil {
|
|
context = "获取天气信息失败"
|
|
} else {
|
|
context = fmt.Sprintf("当前%s股票情况:今日开盘价:%s,昨日收盘价:%s,当前价格:%s,今日最高价:%s,今日最低价:%s,成交量:%s,成交金额:%s",
|
|
data.FinanceResult[0].Data.Name,
|
|
data.FinanceResult[0].Data.TodayStartPri,
|
|
data.FinanceResult[0].Data.YestodEndPri,
|
|
data.FinanceResult[0].Data.NowPri,
|
|
data.FinanceResult[0].Data.TodayMax,
|
|
data.FinanceResult[0].Data.TodayMin,
|
|
data.FinanceResult[0].Data.TraNumber,
|
|
data.FinanceResult[0].Data.TraAmount,
|
|
)
|
|
card = &FinanceCard{
|
|
CompanyName: code,
|
|
CurrPrice: data.FinanceResult[0].Data.NowPri,
|
|
OpenPrice: data.FinanceResult[0].Data.TodayStartPri,
|
|
TodayLow: data.FinanceResult[0].Data.TodayMin,
|
|
TodayHigh: data.FinanceResult[0].Data.TodayMax,
|
|
Volume: data.FinanceResult[0].Data.TraNumber,
|
|
}
|
|
}
|
|
|
|
result = &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: context,
|
|
},
|
|
},
|
|
}
|
|
result.Meta = map[string]interface{}{
|
|
"tool_finance": card,
|
|
}
|
|
return
|
|
}
|
|
|