使用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在大型模型应用中展现出巨大的潜力和广阔的发展前景,希望本文能够为您的学习和了解提供有价值的参考。

相关推荐
大千AI助手10 小时前
HotpotQA:推动多跳推理问答发展的标杆数据集
人工智能·神经网络·llm·qa·大千ai助手·hotpotqa·多跳推理能力
Kratos开源社区14 小时前
跟 Blades 学 Agent 设计 - 01 用“提示词链”让你的 AI 助手变身超级特工
llm·go·agent
山顶夕景17 小时前
【RLVR】GRPO中奖励函数的设计逻辑
llm·强化学习·rl·奖励函数·reward
在未来等你17 小时前
AI Agent设计模式 Day 2:Plan-and-Execute模式:先规划后执行的智能策略
设计模式·llm·react·ai agent·plan-and-execute
有意义18 小时前
从零搭建:json-server+Bootstrap+OpenAI 全栈 AI 小项目
前端·后端·llm
数据智能老司机21 小时前
构建多智能体系统——使用工具
llm·agent·mcp
数据智能老司机21 小时前
构建一个 DeepSeek 模型——通过键值缓存(Key-Value Cache, KV Cache)解决推理瓶颈
架构·llm·deepseek
在未来等你1 天前
AI Agent设计模式 Day 3:Self-Ask模式:自我提问驱动的推理链
设计模式·llm·react·ai agent·plan-and-execute
Larcher2 天前
新手也能学会,100行代码玩AI LOGO
前端·llm·html
架构师日志2 天前
使用大模型+LangExtract从复杂文本提取结构化数据(三)——提取表格列表类型数据
llm