
go sdk
基于 Model Context Protocol (MCP) 规范实现的 Go 语言 SDK,用于标准化 AI 模型上下文交互。
Repository Info
About This Server
基于 Model Context Protocol (MCP) 规范实现的 Go 语言 SDK,用于标准化 AI 模型上下文交互。
Model Context Protocol (MCP) - This server can be integrated with AI applications to provide additional context and capabilities, enabling enhanced AI interactions and functionality.
Documentation
Model Context Protocol (MCP) Go SDK
这是基于Model Context Protocol (MCP)规范实现的Go语言SDK。MCP旨在为AI模型上下文交互提供标准化协议,使应用程序能够与模型服务器进行高效交互。
功能特性
- 完整实现MCP协议规范
- 支持多种传输层实现:
- WebSocket
- HTTP+SSE (Server-Sent Events)
- 提供完整的客户端API,包括:
- 资源管理: 发现和获取资源
- 提示模板: 发现和使用提示模板
- 工具调用: 发现和调用服务器端工具
- 生命周期管理: 连接、初始化和关闭
项目结构
go-sdk/
├── pkg/ # 主要包
│ ├── client/ # MCP客户端实现
│ ├── protocol/ # 协议实现和消息处理
│ ├── schema/ # 协议数据结构和常量定义
│ └── transport/ # 传输层实现(WebSocket, HTTP+SSE)
├── examples/ # 示例代码
│ ├── simple_client/ # 基本客户端示例(WebSocket)
│ └── http_sse_client/ # HTTP+SSE客户端示例
└── README.md # 项目文档
快速开始
安装
go get github.com/modelcontextprotocol/go-sdk
基本用法
以下是一个使用WebSocket传输层的简单示例:
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/modelcontextprotocol/go-sdk/pkg/client"
"github.com/modelcontextprotocol/go-sdk/pkg/schema"
"github.com/modelcontextprotocol/go-sdk/pkg/transport"
)
func main() {
// 创建上下文
ctx := context.Background()
// 创建WebSocket传输层
wsOptions := &transport.WebSocketOptions{
URL: "ws://localhost:8080/mcp",
}
trans, err := transport.NewWebSocketClientTransport(wsOptions)
if err != nil {
log.Fatalf("创建WebSocket传输层失败: %v", err)
}
// 创建客户端
clientOpts := &client.ClientOptions{
ClientInfo: schema.ClientInfo{
Name: "Go-SDK-Example",
Version: "0.1.0",
},
RequestTimeout: 30 * time.Second,
}
c, err := client.NewClient(trans, clientOpts)
if err != nil {
log.Fatalf("创建客户端失败: %v", err)
}
defer c.Close()
// 连接到服务器
if err := c.Connect(ctx); err != nil {
log.Fatalf("连接服务器失败: %v", err)
}
// 获取服务器信息
serverInfo := c.GetServerInfo()
fmt.Printf("已连接到服务器: %s (版本 %s)\n", serverInfo.Name, serverInfo.Version)
// 获取资源列表(如果服务器支持)
if c.GetServerCapabilities().ResourceProvider {
resources, err := c.FetchResources(ctx)
if err != nil {
log.Fatalf("获取资源列表失败: %v", err)
}
fmt.Printf("发现 %d 个资源\n", len(resources))
for i, resource := range resources {
fmt.Printf("%d. %s - %s\n", i+1, resource.Title, resource.URI)
}
}
// 关闭连接
if err := c.Shutdown(ctx); err != nil {
log.Printf("关闭客户端失败: %v", err)
}
}
传输层选择
SDK支持多种传输层实现,可以根据需求选择合适的传输方式:
WebSocket
WebSocket提供全双工通信,适合需要实时双向通信的场景:
wsOptions := &transport.WebSocketOptions{
URL: "ws://localhost:8080/mcp",
// 可选配置
PingInterval: 30 * time.Second,
PongWait: 60 * time.Second,
}
trans, err := transport.NewWebSocketClientTransport(wsOptions)
HTTP+SSE
HTTP+SSE适合客户端主要接收服务器推送事件的场景:
httpSSEOptions := &transport.HTTPWithSSEOptions{
BaseURL: "http://localhost:8080",
SSEPath: "/mcp/events",
MessagePath: "/mcp/message",
ReconnectInterval: 5 * time.Second,
}
trans, err := transport.NewHTTPWithSSETransport(httpSSEOptions)
完整示例
请查看examples目录中的示例代码,了解更多使用方法:
examples/simple_client: WebSocket客户端示例examples/http_sse_client: HTTP+SSE客户端示例
协议版本支持
当前SDK支持MCP协议的2024-11-05版本。
贡献
欢迎贡献代码、报告问题或提出改进建议。请遵循贡献指南。
许可
本项目采用MIT许可证,详见LICENSE文件。
Quick Start
Clone the repository
git clone https://github.com/2404589803/go-sdkInstall dependencies
cd go-sdk
npm installFollow the documentation
Check the repository's README.md file for specific installation and usage instructions.
Repository Details
Recommended MCP Servers
Discord MCP
Enable AI assistants to seamlessly interact with Discord servers, channels, and messages.
Knit MCP
Connect AI agents to 200+ SaaS applications and automate workflows.
Apify MCP Server
Deploy and interact with Apify actors for web scraping and data extraction.
BrowserStack MCP
BrowserStack MCP Server for automated testing across multiple browsers.
Zapier MCP
A Zapier server that provides automation capabilities for various apps.