总结
豆包 AI 助手文章总结
首页 > 后端开发 > Golang > 正文

使用 go 为法学硕士编写工具 - mcp-golang

碧海醫心
发布: 2024-12-13 08:22:24
转载
1120人浏览过

使用 go 为法学硕士编写工具 - mcp-golang

我们刚刚开源了 mcp-golang

几周前,anthropic 发布了模型上下文协议 - 一个允许法学硕士使用工具并与世界互动的协议。

anthropic 编写的基础 sdks 是针对 typescriptpython 的,所以如果你想使用 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&current=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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号