使用golang快速构建你的MCP Server

MCP golang sdk

目前官方并没有go版本的sdk,我们选择的是https://github.com/mark3labs/mcp-go,这个项目✨已超过3000。

MCP Server 代码

这里我们构建一个Milvus助手,让他查询有哪些数据库,具体示例代码如下:

golang 复制代码
package main

import (
    "context"
    "errors"
    "fmt"
    "github.com/milvus-io/milvus-sdk-go/v2/client"
    "log"

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

var milvusDb client.Client

func init() {
    ctx := context.Background()
    config := client.Config{
       Address: "localhost:19530",
       //DBName:  "my_database",
    }
    var err error
    milvusDb, err = client.NewClient(ctx, config)
    if err != nil {
       log.Fatal("connect err:", err)
    }
}

func main() {
    // Create MCP server
    s := server.NewMCPServer(
       "Milvus 🚀",
       "1.0.0",
    )

    // Add tool
    tool := mcp.NewTool("list_database",
       mcp.WithDescription("Get all database of milvus"))

    // Add tool handler
    s.AddTool(tool, ListDatabaseHandler)

    // Start the stdio server
    if err := server.ServeStdio(s); err != nil {
       fmt.Printf("Server error: %v\n", err)
    }
}

func ListDatabaseHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
    databases, err := milvusDb.ListDatabases(ctx)
    if err != nil {
       return nil, err
    }
    cts := []mcp.Content{}
    for _, database := range databases {
       content := mcp.NewTextContent(database.Name)
       cts = append(cts, content)
    }
    return &mcp.CallToolResult{
       Content: cts,
    }, nil
}

构建完成后使用go build -o mcp_test mian.go编译出mcp_test文件,之后就可以测试这个server了。

测试Server

这里我们使用mark3labs的另外一个项目https://github.com/mark3labs/mcphost, mcphost支持本地ollama模型,我们使用Llama 3.1这个模型,需要注意的是,模型需要支持tools调用才行。

配置

ruby 复制代码
{
    "mcpServers": {
       "milvus":{
          "command": "/Users/jhonroxton/Desktop/code/golang/mcp-server-milvus/mcp_test", //路径替换成你自己的
          "args":[]
       }
  }
}

启动ollama测试

使用命令./mcphost -m ollama:llama3.1:latest启动项目

可以看到成功连接到了milvus,让它以表格形式返回milvus中有哪些数据库

可以看到调用了list_database这个tool,返回了defaultrag_test,而这两个和实际的也是对应上的:

总结

本文完整的展示了使用golang构建一个mcp server的示例,主要分四步:

  • 1.创建一个server
  • 2.向server注册tools
  • 3.为对应的tool添加相应的handler
  • 4.启动server

你也可以根据自身需求做扩展,构建出符合自己需求的servermcp在大型模型应用中展现出巨大的潜力和广阔的发展前景,希望本文能够为您的学习和了解提供有价值的参考。

相关推荐
智泊AI6 小时前
一文讲清:AI大模型的工作原理,它是怎么做到这么聪明的?
llm
大模型教程8 小时前
告别数据隐私焦虑!用FastGPT免费私有化部署AI个人知识管理系统!
程序员·llm·agent
大模型教程9 小时前
非常适合初学者的大模型应用开发教程,快速掌握 LLM 开发技能
程序员·llm·agent
AI大模型9 小时前
别再乱用Embedding了!揭秘RAG系统真正灵魂的3大核心组件——90%开发者都搞错了
程序员·llm·agent
AI大模型10 小时前
从零开始玩转大模型:微软的21节课带你学会大模型应用开发(附PPT)
程序员·llm·agent
爱听歌的周童鞋13 小时前
斯坦福大学 | CS336 | 从零开始构建语言模型 | Spring 2025 | 笔记 | Lecture 5: GPUs
llm·gpu·flashattention·cs336·tiling
爱听歌的周童鞋13 小时前
斯坦福大学 | CS336 | 从零开始构建语言模型 | Spring 2025 | 笔记 | Lecture 4: Mixtrue of experts
llm·router·moe·cs336·deepseek-moe
MichaelIp14 小时前
基于MCP协议的多AGENT文章自动编写系统
语言模型·langchain·prompt·ai写作·llamaindex·langgraph·mcp
风雨中的小七15 小时前
解密prompt系列62. Agent Memory一览 - MATTS & CFGM & MIRIX
llm·nlp