Token导航 LogoToken导航TokenDH.com
Go MCP SDK logo
AI代理未说明官方级别未说明来源级核验

Go MCP SDK

MCP Server

Go MCP SDK是一个用于构建符合模型上下文协议(MCP)的服务器的开发工具包,提供创建MCP服务器、注册工具和处理请求的基本构建块。

工具数

2

提示词数

0

GitHub Stars

0

资源数

0
服务器开发GoGo语言

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

RishiPradeep

提供方

RishiPradeep

最后核验

2026/5/17 20:23

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

Go MCP SDK

这是一个Go SDK,用于构建符合模型上下文协议(MCP)的服务器。它提供了创建MCP服务器、注册工具和处理请求的基本构建块。

免责声明

此SDK目前正在积极开发中,应被视为正在进行中的工作。API可能会更改,某些功能可能无法完全实现。请谨慎使用,并随时为其发展做出贡献。

特性

  • MCP服务器:可以处理MCP请求的服务器。
  • 工具注册:注册工具及其处理程序的简单方法。
  • JSON模式生成:自动生成用于工具输入的JSON模式。
  • 结构化日志记录:结构化日志记录,便于调试。

入门指南

本教程将指导您完成使用Go MCP SDK创建简单计算器服务器的过程。

先决条件

  • 在您的系统上安装1.21或更高版本。

使用SDK

由于此SDK尚未发布到公共存储库,因此您需要将其用作本地模块。为此,您可以使用 replace 指令在你 go.mod 文件指向您的本地副本 go-mcp-sdk 存储库。

例如:

replace go-mcp-sdk => /path/to/your/local/go-mcp-sdk

创建服务器

以下是创建新MCP服务器的步骤:

1.初始化服务器

首先,创建一个新的服务器实例,包括其名称、版本和功能。

package main

import (
	"context"
	"fmt"
	"log"

	"go-mcp-sdk/pkg/mcp"
	"go-mcp-sdk/pkg/protocol"
)

func main() {
	server := mcp.NewServer("GoCalculatorServer", "1.0.0", protocol.ServerCapabilities{
		Tools: &protocol.ServerToolCapabilities{},
	})

	// ...
}

2.定义刀具参数结构

对于每个工具,定义一个表示其输入参数的结构。SDK将根据这些结构自动生成JSON模式。

// AddParams defines the input for our "add" tool.
// The `description` tag is used to generate the schema description for each property.
type AddParams struct {
	A float64 `json:"a" description:"The first number to add."`
	B float64 `json:"b" description:"The second number to add."`
}

// SubtractParams defines the input for our "subtract" tool.
type SubtractParams struct {
	A float64 `json:"a" description:"The number to subtract from (minuend)."`
	B float64 `json:"b" description:"The number to subtract (subtrahend)."`
}

3.定义和注册工具

接下来,定义您的工具及其处理程序。处理程序是一个强类型函数,它接受上下文和指向参数结构的指针。

	toolsToRegister := []mcp.ToolRegistration{
		{
			Definition: protocol.Tool{
				Name:        "calculator/add",
				Title:       "Add Numbers",
				Description: "Calculates the sum of two numbers, a and b.",
			},
			Handler: func(ctx context.Context, params *AddParams) (string, error) {
				result := params.A + params.B
				return fmt.Sprintf("The sum of %f and %f is %f.", params.A, params.B, result), nil
			},
		},
		{
			Definition: protocol.Tool{
				Name:        "calculator/subtract",
				Title:       "Subtract Numbers",
				Description: "Calculates the difference between two numbers, a - b.",
			},
			Handler: func(ctx context.Context, params *SubtractParams) (string, error) {
				result := params.A - params.B
				return fmt.Sprintf("The difference of %f minus %f is %f.", params.A, params.B, result), nil
			},
		},
	}

	if err := server.RegisterTools(toolsToRegister); err != nil {
		log.Fatalf("Failed to register tools: %v", err)
	}

4.启动服务器

最后,启动服务器并监听连接。

	log.Println("Starting calculator server on :8080")
	if err := server.ListenAndServe(":8080"); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
}

完整示例: calculator-server

以下是位于中的示例服务器的完整代码 examples/calculator-server/main.go:

package main

import (
	"context"
	"fmt"
	"log"

	"go-mcp-sdk/pkg/mcp"
	"go-mcp-sdk/pkg/protocol"
)

// AddParams defines the input for our "add" tool.
type AddParams struct {
	A float64 `json:"a" description:"The first number to add."`
	B float64 `json:"b" description:"The second number to add."`
}

// SubtractParams defines the input for our "subtract" tool.
type SubtractParams struct {
	A float64 `json:"a" description:"The number to subtract from (minuend)."`
	B float64 `json:"b" description:"The number to subtract (subtrahend)."`
}

func main() {
	server := mcp.NewServer("GoCalculatorServer", "1.0.0", protocol.ServerCapabilities{
		Tools: &protocol.ServerToolCapabilities{},
	})

	toolsToRegister := []mcp.ToolRegistration{
		{
			Definition: protocol.Tool{
				Name:        "calculator/add",
				Title:       "Add Numbers",
				Description: "Calculates the sum of two numbers, a and b.",
			},
			Handler: func(ctx context.Context, params *AddParams) (string, error) {
				result := params.A + params.B
				return fmt.Sprintf("The sum of %f and %f is %f.", params.A, params.B, result), nil
			},
		},
		{
			Definition: protocol.Tool{
				Name:        "calculator/subtract",
				Title:       "Subtract Numbers",
				Description: "Calculates the difference between two numbers, a - b.",
			},
			Handler: func(ctx context.Context, params *SubtractParams) (string, error) {
				result := params.A - params.B
				return fmt.Sprintf("The difference of %f minus %f is %f.", params.A, params.B, result), nil
			},
		},
	}

	if err := server.RegisterTools(toolsToRegister); err != nil {
		log.Fatalf("Failed to register tools: %v", err)
	}

	log.Println("Starting calculator server on :8080")
	if err := server.ListenAndServe(":8080"); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
}

贡献

欢迎投稿!请随时打开问题或提交拉取请求。

目录标签

目录标签

服务器开发GoGo语言MCP协议本地部署工具注册JSON模式生成

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

none

工具数量(toolCount,工具数)

2

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明none部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP