Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ezynda3 committed Dec 11, 2024
1 parent b317cdd commit 921b915
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package client provides MCP (Model Control Protocol) client implementations.
package client

import (
Expand Down
3 changes: 3 additions & 0 deletions client/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions client/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package server provides MCP (Model Control Protocol) server implementations.
package server

import (
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -61,7 +70,6 @@ func WithPromptCapabilities(listChanged bool) ServerOption {
}
}


func WithLogging() ServerOption {
return func(s *MCPServer) {
s.capabilities.logging = true
Expand Down
12 changes: 8 additions & 4 deletions server/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ 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
sessions sync.Map
srv *http.Server
}

// sseSession represents an active SSE connection.
type sseSession struct {
writer http.ResponseWriter
flusher http.Flusher
Expand Down Expand Up @@ -123,27 +127,27 @@ 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)

// 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
}

Expand Down
3 changes: 3 additions & 0 deletions server/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 921b915

Please sign in to comment.