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
- type CallToolParams
- type CallToolResult
- type ClientCapabilities
- type Content
- type GetPromptParams
- type GetPromptResult
- type Implementation
- type InitializeParams
- type InitializeResult
- type JSONRPCError
- type JSONRPCMessage
- func NewJSONRPCErrorResponse(id interface{}, code int, message string, data interface{}) (*JSONRPCMessage, error)
- func NewJSONRPCNotification(method string, params interface{}) (*JSONRPCMessage, error)
- func NewJSONRPCRequest(id interface{}, method string, params interface{}) (*JSONRPCMessage, error)
- func NewJSONRPCResponse(id interface{}, result interface{}) (*JSONRPCMessage, error)
- type ListPromptsResult
- type ListResourceTemplatesResult
- type ListResourcesResult
- type ListToolsResult
- type PaginatedRequest
- type PipeTransport
- type Prompt
- type PromptArgument
- type PromptCapabilities
- type PromptMessage
- type PromptsGetParams
- type PromptsListResult
- type Proxy
- type ReadResourceParams
- type ReadResourceResult
- type RequestID
- type Resource
- type ResourceCapabilities
- type ResourceTemplate
- type ResourceTemplatesListResult
- type ResourcesListResult
- type ResourcesReadParams
- type Server
- func (s *Server) Capabilities() *ServerCapabilities
- func (s *Server) FetchPrompts(ctx context.Context) ([]Prompt, error)
- func (s *Server) FetchResourceTemplates(ctx context.Context) ([]ResourceTemplate, error)
- func (s *Server) FetchResources(ctx context.Context) ([]Resource, error)
- func (s *Server) FetchTools(ctx context.Context) ([]Tool, error)
- func (s *Server) ForwardRequest(ctx context.Context, msg *JSONRPCMessage) (*JSONRPCMessage, error)
- func (s *Server) ID() string
- func (s *Server) Initialize(ctx context.Context, clientInfo Implementation, ...) error
- func (s *Server) IsRunning() bool
- func (s *Server) SendNotification(method string, params interface{}) error
- func (s *Server) SendRequest(ctx context.Context, method string, params interface{}) (*JSONRPCMessage, error)
- func (s *Server) ServerInfo() *Implementation
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) State() ServerState
- func (s *Server) Stop() error
- type ServerCapabilities
- type ServerConfig
- type ServerManager
- func (m *ServerManager) GetOrStartServer(ctx context.Context, id string) (*Server, error)
- func (m *ServerManager) GetServer(id string) (*Server, bool)
- func (m *ServerManager) InitializeAll(ctx context.Context, clientInfo Implementation, ...) error
- func (m *ServerManager) Servers() []string
- func (m *ServerManager) StartAll(ctx context.Context) error
- func (m *ServerManager) StopAll() error
- type ServerState
- type StdioTransport
- type Tool
- type ToolCallParams
- type ToolCapabilities
- type ToolsListResult
- type Transport
Constants ¶
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" )
const ( ParseError = -32700 InvalidRequest = -32600 MethodNotFound = -32601 InvalidParams = -32602 InternalError = -32603 )
JSON-RPC 2.0 Error Codes
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
const (
// NamespaceSeparator is used to prefix primitive names with server ID
NamespaceSeparator = "/"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientCapabilities ¶
type ClientCapabilities = sdk.ClientCapabilities
Implementation and capabilities 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 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 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
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) MarshalJSON ¶
MarshalJSON implements json.Marshaler
func (*RequestID) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler
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 ¶
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 ¶
FetchResources fetches the list of resources from the server
func (*Server) FetchTools ¶
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) Initialize ¶
func (s *Server) Initialize(ctx context.Context, clientInfo Implementation, clientCapabilities ClientCapabilities) error
Initialize sends the initialize request to the server
func (*Server) SendNotification ¶
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)
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 ¶
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) 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 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