Back to MCP directory
verified_userVerifieddnsLocal runtime

mcp-golang

mcp-golang是一个非官方的Go语言实现的Model Context Protocol库,支持快速构建MCP服务器和客户端,提供类型安全、低代码量、模块化和双向通信等特性。

article

README

🚀 使用 Go 语言实现的 MCP 服务器客户端和服务器端功能详解

本项目使用 Go 语言实现了 MCP 服务器的客户端和服务器端功能,重点在于实现客户端与服务端的双向通信,具备工具调用、提示调用和资源管理等特性。

🚀 快速开始

本项目实现了客户端与服务端的双向通信,下面将详细介绍其核心功能模块。

✨ 主要特性

工具调用

  • 支持工具调用,用户可通过定义的工具接口进行功能扩展。
  • 使用原生 Go 结构体作为参数,保障数据传递的安全性和类型安全。
  • 提供程序化生成工具列表端点,方便客户端动态获取可用工具信息。
  • 实现更改通知机制,让客户端及时获知服务端工具的更新情况。
  • 支持分页功能,适用于工具数量较多时的高效数据管理。

提示调用

  • 支持提示调用,用户能通过预设的提示模板与服务端进行交互。
  • 同样使用 Go 结构体作为参数,确保数据的一致性和安全性。
  • 自动生成提示列表端点,客户端可动态获取可用提示信息。
  • 实现更改通知机制,保持客户端与服务端信息同步。
  • 支持分页功能,适用于大量提示模板的管理场景。

资源管理

  • 支持资源的增删改查操作,方便用户管理各类配置和数据。
  • 自动生成资源列表端点,客户端能够实时获取最新资源信息。
  • 实现更改通知机制,确保客户端与服务端数据同步。
  • 支持分页功能,适用于大量资源的高效管理。

💻 使用示例

基础用法

工具调用

package main

import (
    "context"
    "log"
    mcp "github.com/metoro-io/mcp-golang"
    "github.com/metoro-io/mcp-golang/transport/stdio"
)

// 定义工具调用的参数结构体
type CalculateArgs struct {
    Operation string `json:"operation"`
    A         int    `json:"a"`
    B         int    `json:"b"`
}

func main() {
    // 创建并初始化客户端
    transport := stdio.NewStdioServerTransport()
    client := mcp.NewClient(transport)

    if _, err := client.Initialize(context.Background()); err != nil {
        log.Fatalf("Failed to initialize: %v", err)
    }

    // 调用工具示例
    args := CalculateArgs{
        Operation: "add",
        A:         10,
        B:         5,
    }
    
    response, err := client.CallTool(context.Background(), "calculate", args)
    if err != nil {
        log.Fatalf("Failed to call tool: %v", err)
    }

    if response != nil && len(response.Content) > 0 {
        log.Printf("Result: %s", response.Content[0].TextContent.Text)
    }
}

提示调用

package main

import (
    "context"
    "log"
    mcp "github.com/metoro-io/mcp-golang"
    "github.com/metoro-io/mcp-golang/transport/stdio"
)

// 定义提示调用的参数结构体
type QueryArgs struct {
    Keywords    []string `json:"keywords"`
    PageSize   int      `json:"page_size"`
    PageNumber int      `json:"page_number"`
}

func main() {
    transport := stdio.NewStdioServerTransport()
    client := mcp.NewClient(transport)

    if _, err := client.Initialize(context.Background()); err != nil {
        log.Fatalf("Failed to initialize: %v", err)
    }

    // 调用提示示例
    args := QueryArgs{
        Keywords:    []string{"大数据", "分析"},
        PageSize:   10,
        PageNumber: 1,
    }
    
    response, err := client.CallPrompt(context.Background(), "data_analysis", args)
    if err != nil {
        log.Fatalf("Failed to call prompt: %v", err)
    }

    if response != nil && len(response.Content) > 0 {
        log.Printf("Result: %s", response.Content[0].TextContent.Text)
    }
}

资源管理

package main

import (
    "context"
    "log"
    mcp "github.com/metoro-io/mcp-golang"
    "github.com/metoro-io/mcp-golang/transport/stdio"
)
// 此处代码示例原文档未完整给出,可根据实际情况补充完整
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client