Go 中的构建模型上下文协议(MCP)服务器
概览
模型上下文协议(MCP)的Go实现,提供客户端和服务器功能,用于与LLM表面集成。 模型上下文协议允许应用程序以标准化方式为LLM提供上下文,将上下文提供与实际LLM交互的顾虑分开。该Go SDK实现了完整的MCP规范,使得以下操作变得简单: - 构建可连接任意MCP服务器的MCP客户端 - 创建暴露资源、提示和工具的MCP服务器 - 使用标准传输方式如stdio和SSE(即将推出) - 处理所有MCP协议消息和生命周期事件 好奇这些在实际操作中是什么样子?这里有一个示例服务器,它展示了IO的内容。FS作为资源。 你可以编译这个示例,并连接到Claude Desktop(或其他MCP客户端)。 - 模型上下文协议文档 - MCP 规范 - 示例服务器 大部分基础协议已实现。
概述(AI 翻译)
MCP Go SDK
模型上下文协议(MCP)的Go实现,提供客户端和服务器功能,用于与LLM表面集成。
概述
模型上下文协议允许应用程序以标准化方式为LLM提供上下文,将上下文提供与实际LLM交互的顾虑分开。该Go SDK实现了完整的MCP规范,使得以下操作变得简单:
- 构建能够连接任意MCP服务器的MCP客户端
- 创建暴露资源、提示和工具的MCP服务器
- 使用标准传输工具,如stdio和SSE(即将推出)
- 处理所有MCP协议消息和生命周期事件
一个小例子
好奇这些实际操作是什么样子?这里有一个示例服务器
将io.FS的内容暴露为资源。
你可以编译这个示例,并连接到Claude Desktop(或其他MCP客户端)。
文献资料
- 模型上下文协议文档
- MCP规范
- 示例服务器
路线图
基础协议的大部分内容已实现。以下功能已列入我们的路线图:
- 通知
- 抽样
- 根源
合法
在MIT许可下提供。
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
MCP Go SDK
A Go implementation of the Model Context Protocol (MCP), providing both client and server capabilities for integrating with LLM surfaces.
Overview
The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This Go SDK implements the full MCP specification, making it easy to:
- Build MCP clients that can connect to any MCP server
- Create MCP servers that expose resources, prompts and tools
- Use standard transports like stdio and SSE (coming soon)
- Handle all MCP protocol messages and lifecycle events
A small example
Curious what all this looks like in practice? Here’s an example server that
exposes the contents of an io.FS as resources.
package main
import (
"context"
"flag"
"io/fs"
"log"
"mime"
"os"
"path/filepath"
"strings"
"github.com/riza-io/mcp-go"
)
type FSServer struct {
fs fs.FS
mcp.UnimplementedServer
}
func (s *FSServer) Initialize(ctx context.Context, req *mcp.Request[mcp.InitializeRequest]) (*mcp.Response[mcp.InitializeResponse], error) {
return mcp.NewResponse(&mcp.InitializeResponse{
ProtocolVersion: req.Params.ProtocolVersion,
Capabilities: mcp.ServerCapabilities{
Resources: &mcp.Resources{},
},
}), nil
}
func (s *FSServer) ListResources(ctx context.Context, req *mcp.Request[mcp.ListResourcesRequest]) (*mcp.Response[mcp.ListResourcesResponse], error) {
var resources []mcp.Resource
fs.WalkDir(s.fs, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
resources = append(resources, mcp.Resource{
URI: "file://" + path,
Name: path,
MimeType: mime.TypeByExtension(filepath.Ext(path)),
})
return nil
})
return mcp.NewResponse(&mcp.ListResourcesResponse{
Resources: resources,
}), nil
}
func (s *FSServer) ReadResource(ctx context.Context, req *mcp.Request[mcp.ReadResourceRequest]) (*mcp.Response[mcp.ReadResourceResponse], error) {
contents, err := fs.ReadFile(s.fs, strings.TrimPrefix(req.Params.URI, "file://"))
if err != nil {
return nil, err
}
return mcp.NewResponse(&mcp.ReadResourceResponse{
Contents: []mcp.ResourceContent{
{
URI: req.Params.URI,
MimeType: mime.TypeByExtension(filepath.Ext(req.Params.URI)),
Text: string(contents), // TODO: base64 encode
},
},
}), nil
}
func main() {
flag.Parse()
root := flag.Arg(0)
if root == "" {
root = "/"
}
安装
This server does not publish a one-line install command.
Open the repository installation guide 新手?先看 mcpskill.org 教程 →配置
{
"mcpServers": {
"fs": {
"command": "/path/to/mcp-go-fs",
"args": [
"/path/to/root/directory"
]
}
}
}