Go SDK 퀵스타트
Go SDK 퀵스타트
MCP Go SDK로 첫 서버와 클라이언트를 만들어 보면서, Go 특유의 간결한 방식으로 어떻게 연결되는지 확인해 볼게요. 이 SDK는 mcp.Transport를 통해 통신해서 표준 입력/출력 위에서도, 다른 트랜스포트 위에서도 동작해요.
설치
go get github.com/modelcontextprotocol/go-sdk/mcp
시작하기
MCP 서버를 만들려면 mcp.Server 인스턴스를 생성하고 기능을 추가한 뒤 mcp.Transport 위에서 실행하면 돼요. 아래 예시는 단순한 도구 하나를 추가하고, STDIO(표준 입력/출력)로 클라이언트와 연결하는 서버예요:
package main
import (
"context"
"log"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
type Input struct {
Name string `json:"name" jsonschema:"the name of the person to greet"`
}
type Output struct {
Greeting string `json:"greeting" jsonschema:"the greeting to tell to the user"`
}
func SayHi(ctx context.Context, req *mcp.CallToolRequest, input Input) (
*mcp.CallToolResult,
Output,
error,
) {
return nil, Output{Greeting: "Hi " + input.Name}, nil
}
func main() {
// Create a server with a single tool.
server := mcp.NewServer(&mcp.Implementation{Name: "greeter", Version: "v1.0.0"}, nil)
mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi)
// Run the server over stdin/stdout, until the client disconnects.
if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
log.Fatal(err)
}
}
그 서버와 통신하려면 mcp.Client를 만들고, 서버 명령을 실행해 stdin/stdout으로 메시지를 주고받게 연결하면 돼요:
package main
import (
"context"
"log"
"os/exec"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func main() {
ctx := context.Background()
// Create a new client, with no features.
client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil)
// Connect to a server over stdin/stdout.
transport := &mcp.CommandTransport{Command: exec.Command("myserver")}
session, err := client.Connect(ctx, transport, nil)
if err != nil {
log.Fatal(err)
}
defer session.Close()
// Call a tool on the server.
params := &mcp.CallToolParams{
Name: "greet",
Arguments: map[string]any{"name": "you"},
}
res, err := session.CallTool(ctx, params)
if err != nil {
log.Fatalf("CallTool failed: %v", err)
}
if res.IsError {
log.Fatal("tool failed")
}
for _, c := range res.Content {
log.Print(c.(*mcp.TextContent).Text)
}
}
examples/ 디렉토리에는 더 많은 예시 클라이언트와 서버가 있습니다.
더 알아보기 (Learn more)
- Go SDK 서버 가이드 — 프롬프트·리소스·도구 등 서버 기능
- Go SDK 클라이언트 가이드 — 루트·샘플링·일렉시테이션 등 클라이언트 기능
- Go SDK 생명주기 — 프로토콜 라이프사이클·트랜스포트