mcp

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package mcp implements the Model Context Protocol (MCP) proxy functionality. It provides types and utilities for JSON-RPC 2.0 communication and MCP message handling. This package uses types from the official MCP SDK where possible, with custom JSON-RPC transport layer for our proxy implementation.

Index

Constants

View Source
const (
	// JSONRPCVersion is the JSON-RPC protocol version
	JSONRPCVersion = "2.0"

	// MCPProtocolVersion is the MCP protocol version supported by this implementation
	MCPProtocolVersion = "2024-11-05"
)
View Source
const (
	ParseError     = -32700
	InvalidRequest = -32600
	MethodNotFound = -32601
	InvalidParams  = -32602
	InternalError  = -32603
)

JSON-RPC 2.0 Error Codes

View Source
const (
	MethodInitialize             = "initialize"
	MethodInitialized            = "notifications/initialized"
	MethodToolsList              = "tools/list"
	MethodToolsCall              = "tools/call"
	MethodResourcesList          = "resources/list"
	MethodResourcesRead          = "resources/read"
	MethodResourcesTemplatesList = "resources/templates/list"
	MethodPromptsList            = "prompts/list"
	MethodPromptsGet             = "prompts/get"
	MethodPing                   = "ping"
	MethodCancelled              = "notifications/cancelled"
	MethodProgress               = "notifications/progress"
)

MCP Method names

View Source
const (
	// NamespaceSeparator is used to prefix primitive names with server ID
	NamespaceSeparator = "/"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CallToolParams

type CallToolParams = sdk.CallToolParams

Request/Response types from SDK

type CallToolResult

type CallToolResult = sdk.CallToolResult

Request/Response types from SDK

type ClientCapabilities

type ClientCapabilities = sdk.ClientCapabilities

Implementation and capabilities from SDK

type Content

type Content = sdk.Content

Re-export SDK types for use in our implementation Core MCP primitives

type GetPromptParams

type GetPromptParams = sdk.GetPromptParams

Request/Response types from SDK

type GetPromptResult

type GetPromptResult = sdk.GetPromptResult

Request/Response types from SDK

type Implementation

type Implementation = sdk.Implementation

Implementation and capabilities from SDK

type InitializeParams

type InitializeParams struct {
	ProtocolVersion string             `json:"protocolVersion"`
	Capabilities    ClientCapabilities `json:"capabilities"`
	ClientInfo      Implementation     `json:"clientInfo"`
}

InitializeParams represents the parameters for the initialize request We define this ourselves for unmarshaling from our JSON-RPC layer

type InitializeResult

type InitializeResult = sdk.InitializeResult

Request/Response types from SDK

type JSONRPCError

type JSONRPCError struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

JSONRPCError represents a JSON-RPC 2.0 error

func (*JSONRPCError) Error

func (e *JSONRPCError) Error() string

Error implements the error interface

type JSONRPCMessage

type JSONRPCMessage struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      *RequestID      `json:"id,omitempty"`
	Method  string          `json:"method,omitempty"`
	Params  json.RawMessage `json:"params,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *JSONRPCError   `json:"error,omitempty"`
}

JSONRPCMessage represents a generic JSON-RPC 2.0 message (request, response, or notification)

func NewJSONRPCErrorResponse

func NewJSONRPCErrorResponse(id interface{}, code int, message string, data interface{}) (*JSONRPCMessage, error)

NewJSONRPCErrorResponse creates a new JSON-RPC error response message

func NewJSONRPCNotification

func NewJSONRPCNotification(method string, params interface{}) (*JSONRPCMessage, error)

NewJSONRPCNotification creates a new JSON-RPC notification message (no id)

func NewJSONRPCRequest

func NewJSONRPCRequest(id interface{}, method string, params interface{}) (*JSONRPCMessage, error)

NewJSONRPCRequest creates a new JSON-RPC request message

func NewJSONRPCResponse

func NewJSONRPCResponse(id interface{}, result interface{}) (*JSONRPCMessage, error)

NewJSONRPCResponse creates a new JSON-RPC response message

func (*JSONRPCMessage) IsNotification

func (m *JSONRPCMessage) IsNotification() bool

IsNotification returns true if this is a JSON-RPC notification (has method, no id)

func (*JSONRPCMessage) IsRequest

func (m *JSONRPCMessage) IsRequest() bool

IsRequest returns true if this is a JSON-RPC request (has method and id)

func (*JSONRPCMessage) IsResponse

func (m *JSONRPCMessage) IsResponse() bool

IsResponse returns true if this is a JSON-RPC response (has result or error)

type ListPromptsResult

type ListPromptsResult = sdk.ListPromptsResult

Request/Response types from SDK

type ListResourceTemplatesResult

type ListResourceTemplatesResult = sdk.ListResourceTemplatesResult

Request/Response types from SDK

type ListResourcesResult

type ListResourcesResult = sdk.ListResourcesResult

Request/Response types from SDK

type ListToolsResult

type ListToolsResult = sdk.ListToolsResult

Request/Response types from SDK

type PaginatedRequest

type PaginatedRequest struct {
	Cursor *string `json:"cursor,omitempty"`
}

PaginatedRequest represents a request with pagination

type PipeTransport

type PipeTransport struct {
	*StdioTransport
	// contains filtered or unexported fields
}

PipeTransport is a bidirectional transport using io.Reader and io.Writer. This is useful for communicating with subprocess MCP servers via their stdin/stdout.

func NewPipeTransport

func NewPipeTransport(stdin io.WriteCloser, stdout io.ReadCloser) *PipeTransport

NewPipeTransport creates a new pipe transport for subprocess communication

func (*PipeTransport) Close

func (t *PipeTransport) Close() error

Close closes both the stdin and stdout pipes

type Prompt

type Prompt = sdk.Prompt

Re-export SDK types for use in our implementation Core MCP primitives

type PromptArgument

type PromptArgument = sdk.PromptArgument

Re-export SDK types for use in our implementation Core MCP primitives

type PromptCapabilities

type PromptCapabilities = sdk.PromptCapabilities

Implementation and capabilities from SDK

type PromptMessage

type PromptMessage = sdk.PromptMessage

Re-export SDK types for use in our implementation Core MCP primitives

type PromptsGetParams

type PromptsGetParams struct {
	Name      string         `json:"name"`
	Arguments map[string]any `json:"arguments,omitempty"`
}

PromptsGetParams is our custom params type for prompts/get

type PromptsListResult

type PromptsListResult struct {
	Prompts    []Prompt `json:"prompts"`
	NextCursor *string  `json:"nextCursor,omitempty"`
}

PromptsListResult is our result type for prompts/list

type Proxy

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

Proxy implements the MCP proxy that aggregates multiple downstream servers

func NewProxy

func NewProxy(manager *ServerManager, transport Transport, version string) *Proxy

NewProxy creates a new MCP proxy

func (*Proxy) Run

func (p *Proxy) Run(ctx context.Context) error

Run starts the proxy and processes messages until the context is cancelled or EOF

func (*Proxy) Stop

func (p *Proxy) Stop() error

Stop stops the proxy and all downstream servers

type ReadResourceParams

type ReadResourceParams = sdk.ReadResourceParams

Request/Response types from SDK

type ReadResourceResult

type ReadResourceResult = sdk.ReadResourceResult

Request/Response types from SDK

type RequestID

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

RequestID represents a JSON-RPC request ID which can be string, number, or null

func NewRequestID

func NewRequestID(v interface{}) RequestID

NewRequestID creates a new RequestID from a value

func (RequestID) IsNull

func (id RequestID) IsNull() bool

IsNull returns true if the RequestID is null/nil

func (RequestID) MarshalJSON

func (id RequestID) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*RequestID) UnmarshalJSON

func (id *RequestID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler

func (RequestID) Value

func (id RequestID) Value() interface{}

Value returns the underlying value of the RequestID

type Resource

type Resource = sdk.Resource

Re-export SDK types for use in our implementation Core MCP primitives

type ResourceCapabilities

type ResourceCapabilities = sdk.ResourceCapabilities

Implementation and capabilities from SDK

type ResourceTemplate

type ResourceTemplate = sdk.ResourceTemplate

Re-export SDK types for use in our implementation Core MCP primitives

type ResourceTemplatesListResult

type ResourceTemplatesListResult struct {
	ResourceTemplates []ResourceTemplate `json:"resourceTemplates"`
	NextCursor        *string            `json:"nextCursor,omitempty"`
}

ResourceTemplatesListResult is our result type for resources/templates/list

type ResourcesListResult

type ResourcesListResult struct {
	Resources  []Resource `json:"resources"`
	NextCursor *string    `json:"nextCursor,omitempty"`
}

ResourcesListResult is our result type for resources/list

type ResourcesReadParams

type ResourcesReadParams struct {
	URI string `json:"uri"`
}

ResourcesReadParams is our custom params type for resources/read

type Server

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

Server represents a downstream MCP server instance

func NewServer

func NewServer(config ServerConfig, secrets map[string]string, inherit bool) *Server

NewServer creates a new server instance with the given configuration

func (*Server) Capabilities

func (s *Server) Capabilities() *ServerCapabilities

Capabilities returns the server's capabilities (available after initialization)

func (*Server) FetchPrompts

func (s *Server) FetchPrompts(ctx context.Context) ([]Prompt, error)

FetchPrompts fetches the list of prompts from the server

func (*Server) FetchResourceTemplates

func (s *Server) FetchResourceTemplates(ctx context.Context) ([]ResourceTemplate, error)

FetchResourceTemplates fetches the list of resource templates from the server

func (*Server) FetchResources

func (s *Server) FetchResources(ctx context.Context) ([]Resource, error)

FetchResources fetches the list of resources from the server

func (*Server) FetchTools

func (s *Server) FetchTools(ctx context.Context) ([]Tool, error)

FetchTools fetches the list of tools from the server

func (*Server) ForwardRequest

func (s *Server) ForwardRequest(ctx context.Context, msg *JSONRPCMessage) (*JSONRPCMessage, error)

ForwardRequest forwards a raw JSON-RPC message to the server and waits for a response

func (*Server) ID

func (s *Server) ID() string

ID returns the server's ID

func (*Server) Initialize

func (s *Server) Initialize(ctx context.Context, clientInfo Implementation, clientCapabilities ClientCapabilities) error

Initialize sends the initialize request to the server

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns true if the server is running

func (*Server) SendNotification

func (s *Server) SendNotification(method string, params interface{}) error

SendNotification sends a JSON-RPC notification to the server (no response expected)

func (*Server) SendRequest

func (s *Server) SendRequest(ctx context.Context, method string, params interface{}) (*JSONRPCMessage, error)

SendRequest sends a JSON-RPC request to the server and waits for a response

func (*Server) ServerInfo

func (s *Server) ServerInfo() *Implementation

ServerInfo returns the server's info (available after initialization)

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the downstream MCP server subprocess

func (*Server) State

func (s *Server) State() ServerState

State returns the current server state

func (*Server) Stop

func (s *Server) Stop() error

Stop stops the downstream MCP server

type ServerCapabilities

type ServerCapabilities = sdk.ServerCapabilities

Implementation and capabilities from SDK

type ServerConfig

type ServerConfig struct {
	ID      string   `yaml:"id"`
	Command string   `yaml:"command"`
	Args    []string `yaml:"args"`
}

ServerConfig represents the configuration for a downstream MCP server

type ServerManager

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

ServerManager manages multiple downstream MCP servers

func NewServerManager

func NewServerManager(configs []ServerConfig, secrets map[string]string, inherit bool) *ServerManager

NewServerManager creates a new server manager

func (*ServerManager) GetOrStartServer

func (m *ServerManager) GetOrStartServer(ctx context.Context, id string) (*Server, error)

GetOrStartServer returns a server, starting it if necessary (lazy initialization)

func (*ServerManager) GetServer

func (m *ServerManager) GetServer(id string) (*Server, bool)

GetServer returns a server by ID (does not start it)

func (*ServerManager) InitializeAll

func (m *ServerManager) InitializeAll(ctx context.Context, clientInfo Implementation, clientCapabilities ClientCapabilities) error

InitializeAll initializes all running servers

func (*ServerManager) Servers

func (m *ServerManager) Servers() []string

Servers returns a list of all server IDs

func (*ServerManager) StartAll

func (m *ServerManager) StartAll(ctx context.Context) error

StartAll starts all configured servers

func (*ServerManager) StopAll

func (m *ServerManager) StopAll() error

StopAll stops all running servers

type ServerState

type ServerState int

ServerState represents the current state of a server

const (
	ServerStateStopped ServerState = iota
	ServerStateStarting
	ServerStateRunning
	ServerStateStopping
	ServerStateError
)

type StdioTransport

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

StdioTransport implements the MCP stdio transport. Messages are newline-delimited JSON-RPC 2.0 messages.

func NewStdioTransport

func NewStdioTransport(reader io.Reader, writer io.Writer) *StdioTransport

NewStdioTransport creates a new stdio transport

func (*StdioTransport) Close

func (t *StdioTransport) Close() error

Close closes the transport

func (*StdioTransport) ReadMessage

func (t *StdioTransport) ReadMessage() (*JSONRPCMessage, error)

ReadMessage reads the next JSON-RPC message from stdin. Messages are expected to be newline-delimited JSON.

func (*StdioTransport) WriteMessage

func (t *StdioTransport) WriteMessage(msg *JSONRPCMessage) error

WriteMessage writes a JSON-RPC message to stdout. The message is written as a single line of JSON followed by a newline.

type Tool

type Tool = sdk.Tool

Re-export SDK types for use in our implementation Core MCP primitives

type ToolCallParams

type ToolCallParams struct {
	Name      string         `json:"name"`
	Arguments map[string]any `json:"arguments,omitempty"`
}

ToolCallParams is our custom params type for tools/call Using our own to avoid SDK's complex generic request types

type ToolCapabilities

type ToolCapabilities = sdk.ToolCapabilities

Implementation and capabilities from SDK

type ToolsListResult

type ToolsListResult struct {
	Tools      []Tool  `json:"tools"`
	NextCursor *string `json:"nextCursor,omitempty"`
}

ToolsListResult is our result type for tools/list

type Transport

type Transport interface {
	// ReadMessage reads the next JSON-RPC message from the transport
	ReadMessage() (*JSONRPCMessage, error)
	// WriteMessage writes a JSON-RPC message to the transport
	WriteMessage(msg *JSONRPCMessage) error
	// Close closes the transport
	Close() error
}

Transport defines the interface for MCP message transport

Jump to

Keyboard shortcuts

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