mcp_golang

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2024 License: MIT Imports: 9 Imported by: 82

README

Statusphere logo

GitHub stars GitHub forks GitHub issues GitHub pull requests GitHub license GitHub contributors GitHub last commit GoDoc Go Report Card Tests

MCP Golang

MCP golang is an unofficial implementaion of the Model Context Protocol in Go.

Docs can be found at https://mcpgolang.com

Example Usage

type Content struct {
	Title       string  `json:"title" jsonschema:"required,description=The title to submit"`
	Description *string `json:"description" jsonschema:"description=The description to submit"`
}
type MyFunctionsArguments struct {
	Submitter string  `json:"submitter" jsonschema:"required,description=The name of the thing calling this tool (openai, google, claude, etc)"`
	Content   Content `json:"content" jsonschema:"required,description=The content of the message"`
}

func main() {
	done := make(chan struct{})

	server := mcp_golang.NewServer(stdio.NewStdioServerTransport())
	err := server.RegisterTool("hello", "Say hello to a person", func(arguments MyFunctionsArguments) (*mcp_golang.ToolResponse, error) {
		return mcp_golang.NewToolReponse(mcp_golang.NewTextContent(fmt.Sprintf("Hello, %server!", arguments.Submitter))), nil
	})
	if err != nil {
		panic(err)
	}

	err = server.RegisterPrompt("promt_test", "This is a test prompt", func(arguments Content) (*mcp_golang.PromptResponse, error) {
		return mcp_golang.NewPromptResponse("description", mcp_golang.NewPromptMessage(mcp_golang.NewTextContent(fmt.Sprintf("Hello, %server!", arguments.Title)), mcp_golang.RoleUser)), nil
	})
	if err != nil {
		panic(err)
	}

	err = server.RegisterResource("test://resource", "resource_test", "This is a test resource", "application/json", func() (*mcp_golang.ResourceResponse, error) {
		return mcp_golang.NewResourceResponse(mcp_golang.NewTextEmbeddedResource("test://resource", "This is a test resource", "application/json")), nil
	})

	err = server.Serve()
	if err != nil {
		panic(err)
	}

	<-done
}

This will start a server using the stdio transport (used by claude desktop), host a tool called "hello" that will say hello to the user who submitted it.

You can use raw go structs as the input to your tools, the library handles generating the messages, deserialization, etc.

Library Goals and non-goals

  • The library should bias towards maintaining type safety, even when adding extra complexity (reflection to check types etc)
  • The libary api should be simple and easy to use for basic cases of the protocol but we are explicitly aiming to support production use cases firs
  • Where complexity arises, the library will have sane defaults and allow the user to override them if needed.
  • The library aims to support servers first and foremost, when the server features are stable, we will work on adding client support.

Contributions

Contributions are more than welcome! Please check out our contribution guidelines.

Discord

Got any suggestions, have a question on the api or usage? Ask on the discord server. A maintainer will be happy to help you out.

Server Feature Implementation

Tools
  • Tool Calls
  • Programatically generated tool list endpoint
Prompts
  • Prompt Calls
  • Programatically generated prompt list endpoint
Resources
  • Resource Calls
  • Programatically generated resource list endpoint
Transports
  • Stdio
  • SSE
  • Custom transport support
  • HTTPS with custom auth support - in progress. Not currently part of the spec but we'll be adding experimental support for it.

Client Feature Implementation

Currently under development

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotations

type Annotations struct {
	// Describes who the intended customer of this object or data is.
	//
	// It can include multiple entries to indicate ToolResponse useful for multiple
	// audiences (e.g., `["user", "assistant"]`).
	Audience []Role `json:"audience,omitempty" yaml:"audience,omitempty" mapstructure:"audience,omitempty"`

	// Describes how important this data is for operating the server.
	//
	// A value of 1 means "most important," and indicates that the data is
	// effectively required, while 0 means "least important," and indicates that
	// the data is entirely optional.
	Priority *float64 `json:"priority,omitempty" yaml:"priority,omitempty" mapstructure:"priority,omitempty"`
}

type BlobResourceContents

type BlobResourceContents struct {
	// A base64-encoded string representing the binary data of the item.
	Blob string `json:"blob" yaml:"blob" mapstructure:"blob"`

	// The MIME type of this resource, if known.
	MimeType *string `json:"mimeType,omitempty" yaml:"mimeType,omitempty" mapstructure:"mimeType,omitempty"`

	// The URI of this resource.
	Uri string `json:"uri" yaml:"uri" mapstructure:"uri"`
}

type Content

type Content struct {
	Type             ContentType
	TextContent      *TextContent
	ImageContent     *ImageContent
	EmbeddedResource *EmbeddedResource
	Annotations      *Annotations
}

func NewBlobResourceContent

func NewBlobResourceContent(uri string, base64EncodedData string, mimeType string) *Content

NewBlobResourceContent creates a new ToolResponse that is a blob of binary data. The given data is base64-encoded; the client will decode it. The client will render this as a blob; it will not be human-readable.

func NewImageContent

func NewImageContent(base64EncodedStringData string, mimeType string) *Content

NewImageContent creates a new ToolResponse that is an image. The given data is base64-encoded

func NewTextContent

func NewTextContent(content string) *Content

NewTextContent creates a new ToolResponse that is a simple text string. The client will render this as a single string.

func NewTextResourceContent

func NewTextResourceContent(uri string, text string, mimeType string) *Content

NewTextResourceContent creates a new ToolResponse that is an embedded resource of type "text". The given text is embedded in the response as a TextResourceContents, which contains the given MIME type and URI. The text is not base64-encoded.

func (Content) MarshalJSON

func (c Content) MarshalJSON() ([]byte, error)

Custom JSON marshaling for ToolResponse Content

func (*Content) WithAnnotations

func (c *Content) WithAnnotations(annotations Annotations) *Content

type ContentType

type ContentType string
const (
	// The value is the value of the "type" field in the Content so do not change
	ContentTypeText             ContentType = "text"
	ContentTypeImage            ContentType = "image"
	ContentTypeEmbeddedResource ContentType = "resource"
)

type EmbeddedResource

type EmbeddedResource struct {
	EmbeddedResourceType embeddedResourceType
	TextResourceContents *TextResourceContents
	BlobResourceContents *BlobResourceContents
}

The contents of a resource, embedded into a prompt or tool call result.

It is up to the client how best to render embedded resources for the benefit of the LLM and/or the user.

func NewBlobEmbeddedResource

func NewBlobEmbeddedResource(uri string, base64EncodedData string, mimeType string) *EmbeddedResource

func NewTextEmbeddedResource

func NewTextEmbeddedResource(uri string, text string, mimeType string) *EmbeddedResource

func (EmbeddedResource) MarshalJSON

func (c EmbeddedResource) MarshalJSON() ([]byte, error)

Custom JSON marshaling for EmbeddedResource

type ImageContent

type ImageContent struct {
	// The base64-encoded image data.
	Data string `json:"data" yaml:"data" mapstructure:"data"`

	// The MIME type of the image. Different providers may support different image
	// types.
	MimeType string `json:"mimeType" yaml:"mimeType" mapstructure:"mimeType"`
}

An image provided to or from an LLM.

type PromptMessage

type PromptMessage struct {
	Content *Content `json:"content" yaml:"content" mapstructure:"content"`
	Role    Role     `json:"role" yaml:"role" mapstructure:"role"`
}

func NewPromptMessage

func NewPromptMessage(content *Content, role Role) *PromptMessage

type PromptResponse

type PromptResponse struct {
	// An optional description for the prompt.
	Description *string `json:"description,omitempty" yaml:"description,omitempty" mapstructure:"description,omitempty"`

	// Messages corresponds to the JSON schema field "messages".
	Messages []*PromptMessage `json:"messages" yaml:"messages" mapstructure:"messages"`
}

The server's response to a prompts/get request from the client.

func NewPromptResponse

func NewPromptResponse(description string, messages ...*PromptMessage) *PromptResponse

type ResourceResponse

type ResourceResponse struct {
	Contents []*EmbeddedResource `json:"contents"`
}

func NewResourceResponse

func NewResourceResponse(contents ...*EmbeddedResource) *ResourceResponse

type Role

type Role string
const RoleAssistant Role = "assistant"
const RoleUser Role = "user"

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(transport transport.Transport) *Server

func (*Server) RegisterPrompt

func (s *Server) RegisterPrompt(name string, description string, handler any) error

func (*Server) RegisterResource

func (s *Server) RegisterResource(uri string, name string, description string, mimeType string, handler any) error

func (*Server) RegisterTool

func (s *Server) RegisterTool(name string, description string, handler any) error

RegisterTool registers a new tool with the server

func (*Server) Serve

func (s *Server) Serve() error

type TextContent

type TextContent struct {
	// The text ToolResponse of the message.
	Text string `json:"text" yaml:"text" mapstructure:"text"`
}

Text provided to or from an LLM.

type TextResourceContents

type TextResourceContents struct {
	// The MIME type of this resource, if known.
	MimeType *string `json:"mimeType,omitempty" yaml:"mimeType,omitempty" mapstructure:"mimeType,omitempty"`

	// The text of the item. This must only be set if the item can actually be
	// represented as text (not binary data).
	Text string `json:"text" yaml:"text" mapstructure:"text"`

	// The URI of this resource.
	Uri string `json:"uri" yaml:"uri" mapstructure:"uri"`
}

type ToolResponse

type ToolResponse struct {
	Content []*Content
}

This is a union type of all the different ToolResponse that can be sent back to the client. We allow creation through constructors only to make sure that the ToolResponse is valid.

func NewToolReponse

func NewToolReponse(content ...*Content) *ToolResponse

Directories

Path Synopsis
examples
internal
protocol
This file implements the core protocol layer for JSON-RPC communication in the MCP SDK.
This file implements the core protocol layer for JSON-RPC communication in the MCP SDK.
sse
sse/internal/sse
/* Package mcp implements Server-Sent Events (SSE) transport for JSON-RPC communication.
/* Package mcp implements Server-Sent Events (SSE) transport for JSON-RPC communication.
stdio/internal/stdio
This file implements the stdio transport layer for JSON-RPC communication.
This file implements the stdio transport layer for JSON-RPC communication.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL