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
Jonathan Amsterdam 2a09582fa4 jsonschema: dynamic references
A dynamic reference is like a dynamic variable in a programming
language, with two twists:

- There must be a dynamic anchor in lexical scope for the ref to
  resolve. (Think of it as declaring the dynamic variable.)

- The ref resolves to the schema highest on the stack, not lowest.
  That feels odd, but it turns out to work for the intended purpose,
  which is to provide extension points for additional features.

There is some distracting code motion and refactoring in this CL:
resolveRefs was moved down in the file, and resolution of a single
ref was split off.

Change-Id: I17da6052e0782fc540d14b8a41b06a2645923872
Reviewed-on: https://go-review.googlesource.com/c/tools/+/674155
Auto-Submit: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-21 05:05:04 -07:00
design internal/mcp: client middleware 2025-05-19 11:15:45 -07:00
examples internal/mcp: keep an ordered list of feature keys 2025-05-20 13:37:12 -07:00
internal internal/mcp: simplify content 2025-05-13 15:03:25 -07:00
jsonschema jsonschema: dynamic references 2025-05-21 05:05:04 -07:00
mcp internal/mcp: keep an ordered list of feature keys 2025-05-20 13:37:12 -07:00
CONTRIBUTING.md internal/mcp: add README.md and CONTRIBUTING.md 2025-05-06 11:40:21 -07:00
README.md internal/mcp: update README 2025-05-14 17:50:53 -07:00

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"

	"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"))
	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"

	"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.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.