go + vscode + cline +qwen 快速构建 MCP Server

go 编译自定义 mcp tool

  • current time tool 代码
bash 复制代码
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/mark3labs/mcp-go/mcp"
	"github.com/mark3labs/mcp-go/server"
)

func main() {
	// Create MCP server
	s := server.NewMCPServer(
		"Demo ?",
		"1.0.0",
	)
	// Add tool
	tool := mcp.NewTool("current time",
		mcp.WithDescription("Get current time with timezone, Asia/Shanghai is default"),
		mcp.WithString("timezone",
			mcp.Required(),
			mcp.Description("current time timezone"),
		),
	)
	// Add tool handler
	s.AddTool(tool, currentTimeHandler)
	// Start the stdio server
	if err := server.ServeStdio(s); err != nil {
		fmt.Printf("Server error: %v\n", err)
	}
}

func currentTimeHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
	timezone, ok := request.Params.Arguments["timezone"].(string)
	if !ok {
		return mcp.NewToolResultError("timezone must be a string"), nil
	}

	loc, err := time.LoadLocation(timezone)
	if err != nil {
		return mcp.NewToolResultError(fmt.Sprintf("parse timezone with error: %v", err)), nil
	}
	return mcp.NewToolResultText(fmt.Sprintf(`current time is %s`, time.Now().In(loc))), nil
}
  • 编译
bash 复制代码
go build -o current-time-tool.exe .\current_time.go

阿里云百炼平台获取大模型 api key

https://bailian.console.aliyun.com/#/home

vscode插件市场安装cline

  • 安装

    搜索cline安装即可

  • 配置 千问大模型

    API Provider 选择 OPENAI Compatiable

    Base URL 设置为 https://dashscope.aliyuncs.com/compatible-mode/v1

    API Key 设置为 从阿里云百炼平台获取的大模型 api key

    Model ID 设置为 qwen-max-latest

  • MCP Servers配置 current time tool

    MCP Servers -> Installed -> Configure MCP Servers , 配置成如下内容

bash 复制代码
{
    "mcpServers": {
        "current-time-tool": {
            "command": "E:\\GolangProjects\\src\\mcp-learning\\current_time\\current-time-tool.exe",
            "args": [],
            "env": {}
        }
    }
}
  • Auto-approve 选择 Use MCP Servers

cline 点击 + 新建对话

  • 输入 "上海时间现在是多少"
  • 输出之后,点击 Approve, 可以发现已经使用了 current-time-tool
相关推荐
裤裤兔17 小时前
Python打印输出换行
开发语言·python
一水鉴天17 小时前
整体设计 定稿 之24+ dashboard.html 增加三层次动态记录体系仪表盘 之2 程序 (Q208 之2)
开发语言·前端·javascript
啦哈拉哈17 小时前
【Python】知识点零碎学习3
开发语言·python·学习
mengzhi啊17 小时前
Qt自绘制动态字体,模糊的,毛茸茸的fuzzy。
开发语言·qt
识途老码17 小时前
python装饰器
开发语言·python
JIngJaneIL17 小时前
基于Java饮食营养管理信息平台系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
fresh hacker17 小时前
【Python数据分析】速通NumPy
开发语言·python·数据挖掘·数据分析·numpy
长安第一美人17 小时前
整车控制器标定软件介绍 || 汽车标定协议CPP
开发语言·qt
宠..17 小时前
获取输入内容
开发语言·c++·qt
郝学胜-神的一滴17 小时前
Linux系统调用中断机制深度解析
linux·运维·服务器·开发语言·c++·程序人生