我们刚刚开源了 mcp-golang!
几周前,anthropic 发布了模型上下文协议 - 一个允许法学硕士使用工具并与世界互动的协议。
anthropic 编写的基础 sdks 是针对 typescript 和 python 的,所以如果你想使用 go,那你就不走运了。
使用 mcp-golang,您现在只需几行代码即可在 go 中编写工具服务器。
立即学习“go语言免费学习笔记(深入)”;
下面的服务器允许法学硕士了解任何地点的天气。
package main import ( "fmt" mcp_golang "github.com/metoro-io/mcp-golang" "github.com/metoro-io/mcp-golang/transport/stdio" "io" "net/http" ) type WeatherArguments struct { Longitude float64 `json:"longitude" jsonschema:"required,description=The longitude of the location to get the weather for"` Latitude float64 `json:"latitude" jsonschema:"required,description=The latitude of the location to get the weather for"` } // This is explained in the docs at https://mcpgolang.com/tools func main() { done := make(chan struct{}) server := mcp_golang.NewServer(stdio.NewStdioServerTransport()) err := server.RegisterTool("get_weather", "Get the weather forecast for temperature, wind speed and relative humidity", func(arguments WeatherArguments) (*mcp_golang.ToolResponse, error) { url := fmt.Sprintf("https://api.open-meteo.com/v1/forecast?latitude=%f&longitude=%f¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m", arguments.Latitude, arguments.Longitude) resp, err := http.Get(url) if err != nil { return nil, err } defer resp.Body.Close() output, err := io.ReadAll(resp.Body) if err != nil { return nil, err } return mcp_golang.NewToolReponse(mcp_golang.NewTextContent(string(output))), nil }) err = server.Serve() if err != nil { panic(err) } <-done }
查看演示https://youtu.be/kfflqcgvvde!
编码愉快!
以上就是使用 go 为法学硕士编写工具 - mcp-golang的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号