Mirror of https://github.com/modelcontextprotocol/go-sdk — The official Go SDK for Model Context Protocol servers and clients. Maintained in collaboration with Google.
  • Go 99.7%
  • Shell 0.3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Rob Findley dbf57af24b internal/mcp: rename LocalTransport->InMemoryTransport, and document
Update the design to mention our in-memory transport, since it is
generally useful, and update the implementation to use distinct types
for InMemoryTransport and StdIOTransport (the fact that they are both
implemented as an io.ReadWriteCloser is an implementation detail).

Change-Id: I8228ccf2092a56a730c113c4db105fb492cce112
Reviewed-on: https://go-review.googlesource.com/c/tools/+/672715
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
Commit-Queue: Robert Findley <rfindley@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Bypass: Robert Findley <rfindley@google.com>
2025-05-14 10:11:26 -07:00
design internal/mcp: rename LocalTransport->InMemoryTransport, and document 2025-05-14 10:11:26 -07:00
examples internal/mcp: rename ServerConnection->ServerSession 2025-05-14 10:10:28 -07:00
internal internal/mcp: simplify content 2025-05-13 15:03:25 -07:00
jsonschema internal/mcp: pointerize all the things 2025-05-14 06:22:56 -07:00
mcp internal/mcp: rename LocalTransport->InMemoryTransport, and document 2025-05-14 10:11:26 -07:00
CONTRIBUTING.md internal/mcp: add README.md and CONTRIBUTING.md 2025-05-06 11:40:21 -07:00
README.md internal/mcp: rename ServerConnection->ServerSession 2025-05-14 10:10:28 -07:00

MCP package

PkgGoDev

The mcp package provides an SDK for writing model context protocol clients and servers. It is a work-in-progress. As of writing, it is a prototype to explore the design space of client/server transport and binding.

Installation

The mcp package is currently internal and cannot be imported using go get.

Quickstart

Here's an example that creates a client that talks to an MCP server running as a sidecar process:

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-client", "v1.0.0", nil)
	// Connect to a server over stdin/stdout
	transport := mcp.NewCommandTransport(exec.Command("myserver"))
	if err := client.Connect(ctx, transport, nil); err != nil {
		log.Fatal(err)
	}
	// Call a tool on the server.
	if content, err := client.CallTool(ctx, "greet", map[string]any{"name": "you"}); err != nil {
		log.Printf("CallTool returns error: %v", err)
	} else {
		log.Printf("CallTool returns: %v", content)
	}
}

Here is an example of the corresponding server, connected over stdin/stdout:

package main

import (
	"context"

	"github.com/modelcontextprotocol/go-sdk/mcp"
)

type HiParams struct {
	Name string `json:"name"`
}

func SayHi(ctx context.Context, cc *mcp.ServerSession, params *HiParams) ([]mcp.Content, error) {
	return []mcp.Content{
		mcp.TextContent{Text: "Hi " + params.Name},
	}, nil
}

func main() {
	// Create a server with a single tool.
	server := mcp.NewServer("greeter", "v1.0.0", nil)
	server.AddTools(mcp.MakeTool("greet", "say hi", SayHi))
	// Run the server over stdin/stdout, until the client diconnects
	_ = server.Run(context.Background(), mcp.NewStdIOTransport(), nil)
}

Core Concepts

The mcp package leverages Go's reflect package to automatically generate the JSON schema for your tools / prompts' input parameters. As an mcp server developer, ensure your input parameter structs include the standard "json" tags (as demonstrated in the HiParams example). Refer to the jsonschema package for detailed information on schema inference.

Tools

Tools in MCP allow servers to expose executable functions that can be invoked by clients and used by LLMs to perform actions. The server can add tools using

...
server := mcp.NewServer("greeter", "v1.0.0", nil)
server.AddTools(mcp.MakeTool("greet", "say hi", SayHi))
...

Prompts

Prompts enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs. The server can add prompts by using

...
server := mcp.NewServer("greeter", "v0.0.1", nil)
server.AddPrompts(mcp.MakePrompt("greet", "", PromptHi))
...

Resources

Resources are a core primitive in the Model Context Protocol (MCP) that allow servers to expose data and content that can be read by clients and used as context for LLM interactions.

Resources are not supported yet.

Testing

To test your client or server using stdio transport, you can use local transport instead of creating real stdio transportation. See example.

To test your client or server using sse transport, you can use the httptest package. See example.

Code of Conduct

This project follows the Go Community Code of Conduct. If you encounter a conduct-related issue, please mail conduct@golang.org.

License

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Upon a potential move to modelcontextprotocol, the license will be updated to the MIT License, and the license header will reflect the Go MCP SDK Authors.