tree: 5b61622491f24542f0245add80043b1f59ba6610 [path history] [tgz]
  1. design/
  2. examples/
  3. internal/
  4. jsonschema/
  5. testdata/
  6. client.go
  7. cmd.go
  8. cmd_test.go
  9. content.go
  10. content_test.go
  11. CONTRIBUTING.md
  12. features.go
  13. generate.go
  14. mcp.go
  15. mcp_test.go
  16. prompt.go
  17. prompt_test.go
  18. protocol.go
  19. README.md
  20. resource.go
  21. resource_go124.go
  22. resource_pre_go124.go
  23. resource_test.go
  24. root.go
  25. server.go
  26. server_example_test.go
  27. sse.go
  28. sse_example_test.go
  29. sse_test.go
  30. tool.go
  31. tool_test.go
  32. transport.go
  33. transport_test.go
  34. util.go
internal/mcp/README.md

MCP SDK prototype

PkgGoDev

The mcp package provides a software development kit (SDK) for writing clients and servers of the model context protocol. It is unstable, and will change in breaking ways in the future. 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"

	"golang.org/x/tools/internal/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"))
	session, err := client.Connect(ctx, transport)
    if err != nil {
		log.Fatal(err)
	}
    defer session.Close()
	// Call a tool on the server.
	if content, err := session.CallTool(ctx, "greet", map[string]any{"name": "you"}, nil); err != nil {
		log.Printf("CallTool failed: %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"

	"golang.org/x/tools/internal/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.NewTextContent("Hi " + params.Name),
	}, nil
}

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

Design

See design.md for the SDK design. That document is canonical: given any divergence between the design doc and this prototype, the doc reflects the latest design.

Testing

To test your client or server using stdio transport, you can use an in-memory transport. 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 the modelcontextprotocol organization, the license will be updated to the MIT License, and the license header will reflect the Go MCP SDK Authors.