使用golang调用高德mcp server

58 阅读6分钟

使用golang 调用高德mcp server

使用go语言mcp客户端调用高德mcp server。先去高德开发者后台申请apikey并设置高德mcp api key环境变量AMAP_API_KEY

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"encoding/json"
	"github.com/joho/godotenv"
	"github.com/modelcontextprotocol/go-sdk/mcp"
)

func main() {
	ctx := context.Background()

	godotenv.Load()
	apiKey := os.Getenv("AMAP_API_KEY")
	client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil)

	ampURL := fmt.Sprintf("https://mcp.amap.com/mcp?key=%s", apiKey)
	log.Println("ampURL:", ampURL)
	transport := &mcp.StreamableClientTransport{Endpoint: ampURL}
	session, err := client.Connect(ctx, transport, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer session.Close()

	// 查询mcp tools 列表
	tools, err := session.ListTools(ctx, &mcp.ListToolsParams{})
	if err != nil {
		log.Fatalf("ListTools failed: %v", err)
	}
	if tools == nil {
		log.Fatal("no tools")
	}
	
	for _, tool := range tools.Tools {
		PrintJson(tool)
	}

	// 调用mcp tools
	params := &mcp.CallToolParams{
		Name:      "maps_geo",
		Arguments: map[string]any{"address": "杭州市市民中心","city":"杭州"},
	}
	res, err := session.CallTool(ctx, params)
	if err != nil {
		log.Fatalf("CallTool failed: %v", err)
	}
	if res == nil {
		log.Fatal("CallTool no result")
	}
	PrintJson(res)
	for _, c := range res.Content {
		log.Print(c.(*mcp.TextContent).Text)
	}
}

func PrintJson(v any) {
	jsonRaw, _ := json.Marshal(v)
	log.Printf("%s\n", string(jsonRaw))
}

执行结果如下:

ampURL:https://mcp.amap.com/mcp?key=xxxxxxxxxxxxxxx
2025/10/29 15:03:06 {"description":"骑行路径规划用于规划骑行通勤方案,规划时会考虑天桥、单行线、封路等情况。最大支持 500km 的骑行路线规划","inputSchema":{"properties":{"destination":{"description":"目的地经纬度,坐标格式为:经度,纬度","type":"string"},"origin":{"description":"出发点经纬度,坐标格式为:经度,纬度","type":"string"}},"required":["origin","destination"],"type":"object"},"name":"maps_direction_bicycling"}
2025/10/29 15:03:06 {"description":"驾车路径规划 API 可以根据用户起终点经纬度坐标规划以小客车、轿车通勤出行的方案,并且返回通勤方案的数据。","inputSchema":{"properties":{"destination":{"description":"目的地经纬度,坐标格式为:经度,纬度","type":"string"},"origin":{"description":"出发点经纬度,坐标格式为:经度,纬度","type":"string"}},"required":["origin","destination"],"type":"object"},"name":"maps_direction_driving"}
2025/10/29 15:03:06 {"description":"根据用户起终点经纬度坐标规划综合各类公共(火车、公交、地铁)交通方式的通勤方案,并且返回通勤方案的数据,跨城场景下必须传起点城市与终点城市","inputSchema":{"properties":{"city":{"description":"公共交通规划起点城市","type":"string"},"cityd":{"description":"公共交通规划终点城市","type":"string"},"destination":{"description":"目的地经纬度,坐标格式为:经度,纬度","type":"string"},"origin":{"description":"出发点经纬度,坐标格式为:经度,纬度","type":"string"}},"required":["origin","destination","city","cityd"],"type":"object"},"name":"maps_direction_transit_integrated"}
2025/10/29 15:03:06 {"description":"根据输入起点终点经纬度坐标规划100km 以内的步行通勤方案,并且返回通勤方案的数据","inputSchema":{"properties":{"destination":{"description":"目的地经度,纬度,坐标格式为:经度,纬度","type":"string"},"origin":{"description":"出发点经度,纬度,坐标格式为:经度,纬度","type":"string"}},"required":["origin","destination"],"type":"object"},"name":"maps_direction_walking"}
2025/10/29 15:03:06 {"description":"测量两个经纬度坐标之间的距离,支持驾车、步行以及球面距离测量","inputSchema":{"properties":{"destination":{"description":"终点经度,纬度,坐标格式为:经度,纬度","type":"string"},"origins":{"description":"起点经度,纬度,可以传多个坐标,使用竖线隔离,比如120,30|120,31,坐标格式为:经度,纬度","type":"string"},"type":{"description":"距离测量类型,1代表驾车距离测量,0代表直线距离测量,3步行距离测量","type":"string"}},"required":["origins","destination"],"type":"object"},"name":"maps_distance"}
2025/10/29 15:03:06 {"description":"将详细的结构化地址转换为经纬度坐标。支持对地标性名胜景区、建筑物名称解析为经纬度坐标","inputSchema":{"properties":{"address":{"description":"待解析的结构化地址信息","type":"string"},"city":{"description":"指定查询的城市","type":"string"}},"required":["address"],"type":"object"},"name":"maps_geo"}
2025/10/29 15:03:06 {"description":"将一个高德经纬度坐标转换为行政区划地址信息","inputSchema":{"properties":{"location":{"description":"经纬度","type":"string"}},"required":["location"],"type":"object"},"name":"maps_regeocode"}
2025/10/29 15:03:06 {"description":"IP 定位根据用户输入的 IP 地址,定位 IP 的所在位置","inputSchema":{"properties":{"ip":{"description":"IP地址","type":"string"}},"required":["ip"],"type":"object"},"name":"maps_ip_location"}
2025/10/29 15:03:06 {"description":"用于行程规划结果在高德地图展示。将行程规划位置点按照行程顺序填入lineList,返回结果为高德地图打开的URI链接,该结果不需总结,直接返回!","inputSchema":{"properties":{"lineList":{"description":"行程列表","items":{"properties":{"pointInfoList":{"description":"行程目标位置点描述","items":{"properties":{"lat":{"description":"行程目标位置点纬度","type":"number"},"lon":{"description":"行程目标位置点经度","type":"number"},"name":{"description":"行程目标位置点名称","type":"string"},"poiId":{"description":"行程目标位置点POIID","type":"string"}},"required":["name","lon","lat","poiId"],"type":"object"},"type":"array"},"title":{"description":"行程名称描述(按行程顺序)","type":"string"}},"required":["title","pointInfoList"],"type":"object"},"type":"array"},"orgName":{"description":"行程规划地图小程序名称","type":"string"}},"required":["orgName","lineList"],"type":"object"},"name":"maps_schema_personal_map"}
2025/10/29 15:03:06 {"description":"周边搜,根据用户传入关键词以及坐标location,搜索出radius半径范围的POI","inputSchema":{"properties":{"keywords":{"description":"搜索关键词","type":"string"},"location":{"description":"中心点经度纬度","type":"string"},"radius":{"description":"搜索半径","type":"string"}},"required":["keywords","location"],"type":"object"},"name":"maps_around_search"}
2025/10/29 15:03:06 {"description":"查询关键词搜或者周边搜获取到的POI ID的详细信息","inputSchema":{"properties":{"id":{"description":"关键词搜或者周边搜获取到的POI ID","type":"string"}},"required":["id"],"type":"object"},"name":"maps_search_detail"}
2025/10/29 15:03:06 {"description":"关键字搜索 API 根据用户输入的关键字进行 POI 搜索,并返回相关的信息","inputSchema":{"properties":{"city":{"description":"查询城市","type":"string"},"citylimit":{"default":false,"description":"是否限制城市范围内搜索,默认不限制","type":"boolean"},"keywords":{"description":"查询关键字","type":"string"}},"required":["keywords"],"type":"object"},"name":"maps_text_search"}
2025/10/29 15:03:06 {"description":" Schema唤醒客户端-导航页面,用于根据用户输入终点信息,返回一个拼装好的客户端唤醒URI,用户点击该URI即可唤起对应的客户端APP。唤起客户端后,会自动跳转到导航页面。","inputSchema":{"properties":{"lat":{"description":"终点纬度","type":"string"},"lon":{"description":"终点经度","type":"string"}},"required":["lon","lat"],"type":"object"},"name":"maps_schema_navi"}
2025/10/29 15:03:06 {"description":"根据用户输入的起点和终点信息,返回一个拼装好的客户端唤醒URI,直接唤起高德地图进行打车。直接展示生成的链接,不需要总结","inputSchema":{"properties":{"dlat":{"description":"终点纬度","type":"string"},"dlon":{"description":"终点经度","type":"string"},"dname":{"description":"终点名称","type":"string"},"slat":{"description":"起点纬度","type":"string"},"slon":{"description":"起点经度","type":"string"},"sname":{"description":"起点名称","type":"string"}},"required":["dlon","dlat","dname"],"type":"object"},"name":"maps_schema_take_taxi"}
2025/10/29 15:03:06 {"description":"根据城市名称或者标准adcode查询指定城市的天气","inputSchema":{"properties":{"city":{"description":"城市名称或者adcode","type":"string"}},"required":["city"],"type":"object"},"name":"maps_weather"}
2025/10/29 15:03:06 {"content":[{"type":"text","text":"{\"results\":[{\"country\":\"中国\",\"province\":\"浙江省\",\"city\":\"杭州市\",\"citycode\":\"0571\",\"district\":\"临安区\",\"street\":[],\"number\":[],\"adcode\":\"330112\",\"location\":\"119.752949,30.254876\",\"level\":\"兴趣点\"}]}"}]}
2025/10/29 15:03:06 {"results":[{"country":"中国","province":"浙江省","city":"杭州市","citycode":"0571","district":"临安区","street":[],"number":[],"adcode":"330112","location":"119.752949,30.254876","level":"兴趣点"}]}