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
相关推荐
珹洺16 分钟前
C++从入门到实战(五)类和对象(第一部分)为什么有类,及怎么使用类,类域概念详解(附带图谱等更好对比理解)
java·c语言·开发语言·数据结构·c++·redis·缓存
Evand J26 分钟前
GNSS(GPS、北斗等)与UWB的融合定位例程,matlab,二维平面,使用卡尔曼滤波
开发语言·matlab·平面
笑口常开xpr34 分钟前
C 语 言 --- 扫 雷 游 戏(初 阶 版)
c语言·开发语言
Cindy辛蒂39 分钟前
C语言:穷举法编程韩信点兵问题四种做法
c语言·开发语言·算法
为美好的生活献上中指1 小时前
java每日精进 3.21 【SpringBoot规范2.0】
java·开发语言·spring boot·log4j·async·mail
❆VE❆1 小时前
【工具分享】vscode+deepseek的接入与使用
ide·vscode·编辑器·ai编程·工具·deepseek
碧海饮冰1 小时前
招聘面试季--一文顿悟,Java中字节流和字符流的区别及使用场景上的差异
java·开发语言·面试
重生成为码农‍1 小时前
类加载机制
java·开发语言·jvm
Ai 编码助手2 小时前
PHP与Python无缝融合,开启跨语言开发新纪元
开发语言·python·php
19岁开始学习2 小时前
PHP之依赖注入
android·开发语言·php