From 921b915c2c439895dd77d8e9601b1b348ac2d78c Mon Sep 17 00:00:00 2001 From: Ed Zynda Date: Wed, 11 Dec 2024 11:41:21 +0300 Subject: [PATCH] update comments --- client/client.go | 1 + client/sse.go | 3 +++ client/stdio.go | 2 ++ server/server.go | 30 +++++++++++++++++++----------- server/sse.go | 12 ++++++++---- server/stdio.go | 3 +++ 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/client/client.go b/client/client.go index b6cf0f1..1d3cb10 100644 --- a/client/client.go +++ b/client/client.go @@ -1,3 +1,4 @@ +// Package client provides MCP (Model Control Protocol) client implementations. package client import ( diff --git a/client/sse.go b/client/sse.go index 12a7967..4538527 100644 --- a/client/sse.go +++ b/client/sse.go @@ -17,6 +17,9 @@ import ( "github.com/mark3labs/mcp-go/mcp" ) +// SSEMCPClient implements the MCPClient interface using Server-Sent Events (SSE). +// It maintains a persistent HTTP connection to receive server-pushed events +// while sending requests over regular HTTP POST calls. type SSEMCPClient struct { baseURL *url.URL endpoint *url.URL diff --git a/client/stdio.go b/client/stdio.go index 7a5a9f2..9163d1d 100644 --- a/client/stdio.go +++ b/client/stdio.go @@ -13,6 +13,8 @@ import ( "github.com/mark3labs/mcp-go/mcp" ) +// StdioMCPClient implements the MCPClient interface using stdio communication. +// It launches a subprocess and communicates with it via standard input/output streams. type StdioMCPClient struct { cmd *exec.Cmd stdin io.WriteCloser diff --git a/server/server.go b/server/server.go index 0e6e3c6..314995c 100644 --- a/server/server.go +++ b/server/server.go @@ -1,3 +1,4 @@ +// Package server provides MCP (Model Control Protocol) server implementations. package server import ( @@ -7,6 +8,24 @@ import ( "github.com/mark3labs/mcp-go/mcp" ) +// ServerOption is a function that configures an MCPServer. +type ServerOption func(*MCPServer) + +// ResourceHandlerFunc is a function that returns resource contents. +type ResourceHandlerFunc func() ([]interface{}, error) + +// ResourceTemplateHandlerFunc is a function that returns a resource template. +type ResourceTemplateHandlerFunc func() (mcp.ResourceTemplate, error) + +// PromptHandlerFunc handles prompt requests with given arguments. +type PromptHandlerFunc func(arguments map[string]string) (*mcp.GetPromptResult, error) + +// ToolHandlerFunc handles tool calls with given arguments. +type ToolHandlerFunc func(arguments map[string]interface{}) (*mcp.CallToolResult, error) + +// NotificationHandlerFunc handles incoming notifications. +type NotificationHandlerFunc func(notification mcp.JSONRPCNotification) + type MCPServer struct { name string version string @@ -34,16 +53,6 @@ type promptCapabilities struct { listChanged bool } -type ServerOption func(*MCPServer) - -type ResourceHandlerFunc func() ([]interface{}, error) -type ResourceTemplateHandlerFunc func() (mcp.ResourceTemplate, error) - -type PromptHandlerFunc func(arguments map[string]string) (*mcp.GetPromptResult, error) - -type ToolHandlerFunc func(arguments map[string]interface{}) (*mcp.CallToolResult, error) -type NotificationHandlerFunc func(notification mcp.JSONRPCNotification) - func WithResourceCapabilities(subscribe, listChanged bool) ServerOption { return func(s *MCPServer) { s.capabilities.resources = &resourceCapabilities{ @@ -61,7 +70,6 @@ func WithPromptCapabilities(listChanged bool) ServerOption { } } - func WithLogging() ServerOption { return func(s *MCPServer) { s.capabilities.logging = true diff --git a/server/sse.go b/server/sse.go index ace28d9..5142f35 100644 --- a/server/sse.go +++ b/server/sse.go @@ -9,8 +9,11 @@ import ( "sync" "github.com/google/uuid" + "github.com/mark3labs/mcp-go/mcp" ) +// SSEServer implements a Server-Sent Events (SSE) based MCP server. +// It provides real-time communication capabilities over HTTP using the SSE protocol. type SSEServer struct { server *MCPServer baseURL string @@ -18,6 +21,7 @@ type SSEServer struct { srv *http.Server } +// sseSession represents an active SSE connection. type sseSession struct { writer http.ResponseWriter flusher http.Flusher @@ -123,19 +127,19 @@ func (s *SSEServer) handleSSE(w http.ResponseWriter, r *http.Request) { func (s *SSEServer) handleMessage(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { - s.writeJSONRPCError(w, nil, -32600, "Method not allowed") + s.writeJSONRPCError(w, nil, mcp.INVALID_REQUEST, "Method not allowed") return } sessionID := r.URL.Query().Get("sessionId") if sessionID == "" { - s.writeJSONRPCError(w, nil, -32602, "Missing sessionId") + s.writeJSONRPCError(w, nil, mcp.INVALID_PARAMS, "Missing sessionId") return } sessionI, ok := s.sessions.Load(sessionID) if !ok { - s.writeJSONRPCError(w, nil, -32602, "Invalid session ID") + s.writeJSONRPCError(w, nil, mcp.INVALID_PARAMS, "Invalid session ID") return } session := sessionI.(*sseSession) @@ -143,7 +147,7 @@ func (s *SSEServer) handleMessage(w http.ResponseWriter, r *http.Request) { // Parse message as raw JSON var rawMessage json.RawMessage if err := json.NewDecoder(r.Body).Decode(&rawMessage); err != nil { - s.writeJSONRPCError(w, nil, -32700, "Parse error") + s.writeJSONRPCError(w, nil, mcp.PARSE_ERROR, "Parse error") return } diff --git a/server/stdio.go b/server/stdio.go index 3a540be..d24176f 100644 --- a/server/stdio.go +++ b/server/stdio.go @@ -15,6 +15,9 @@ import ( ) // StdioServer wraps a MCPServer and handles stdio communication +// StdioServer wraps a MCPServer and handles stdio communication. +// It provides a simple way to create command-line MCP servers that +// communicate via standard input/output streams. type StdioServer struct { server *MCPServer errLogger *log.Logger