Skip to content

Commit

Permalink
♻️ refactor: consolidate list operation response types
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Jan 21, 2025
1 parent 7815ef4 commit bf47934
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
10 changes: 9 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,12 @@ Fixed an issue where the SSE server would hang during shutdown due to improper h
- Added proper context cancellation for client goroutines
- Added WaitGroup to track and wait for client goroutines to finish
- Improved shutdown coordination between HTTP server and client cleanup
- Added timeout handling for client goroutine cleanup
- Added timeout handling for client goroutine cleanup

## Response Type Consolidation

Consolidated list operation response types into a shared location:

- Moved ListPromptsResult, ListResourcesResult, and ListToolsResult to responses.go
- Updated both SSE and stdio servers to use the shared types
- Ensured consistent response structure across all server implementations
15 changes: 0 additions & 15 deletions pkg/server/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ type SSEClient struct {
userAgent string
}

type ListPromptsResult struct {
Prompts []protocol.Prompt `json:"prompts"`
NextCursor string `json:"nextCursor"`
}

type ListResourcesResult struct {
Resources []protocol.Resource `json:"resources"`
NextCursor string `json:"nextCursor"`
}

type ListToolsResult struct {
Tools []protocol.Tool `json:"tools"`
NextCursor string `json:"nextCursor"`
}

// NewSSEServer creates a new SSE server instance
func NewSSEServer(logger zerolog.Logger, ps services.PromptService, rs services.ResourceService, ts services.ToolService, is services.InitializeService, port int) *SSEServer {
return &SSEServer{
Expand Down
42 changes: 33 additions & 9 deletions pkg/server/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ func (s *StdioServer) handleRequest(request protocol.Request) error {
if err != nil {
return s.sendError(&request.ID, -32603, "Internal error", err)
}
if prompts == nil {
prompts = []protocol.Prompt{}
}

return s.sendResult(&request.ID, map[string]interface{}{
"prompts": prompts,
"nextCursor": nextCursor,
return s.sendResult(&request.ID, ListPromptsResult{
Prompts: prompts,
NextCursor: nextCursor,
})

case "prompts/get":
Expand Down Expand Up @@ -161,10 +164,13 @@ func (s *StdioServer) handleRequest(request protocol.Request) error {
if err != nil {
return s.sendError(&request.ID, -32603, "Internal error", err)
}
if resources == nil {
resources = []protocol.Resource{}
}

return s.sendResult(&request.ID, map[string]interface{}{
"resources": resources,
"nextCursor": nextCursor,
return s.sendResult(&request.ID, ListResourcesResult{
Resources: resources,
NextCursor: nextCursor,
})

case "resources/read":
Expand Down Expand Up @@ -200,10 +206,13 @@ func (s *StdioServer) handleRequest(request protocol.Request) error {
if err != nil {
return s.sendError(&request.ID, -32603, "Internal error", err)
}
if tools == nil {
tools = []protocol.Tool{}
}

return s.sendResult(&request.ID, map[string]interface{}{
"tools": tools,
"nextCursor": nextCursor,
return s.sendResult(&request.ID, ListToolsResult{
Tools: tools,
NextCursor: nextCursor,
})

case "tools/call":
Expand Down Expand Up @@ -297,3 +306,18 @@ func (s *StdioServer) sendError(id *json.RawMessage, code int, message string, d
s.logger.Debug().Interface("response", response).Msg("Sending error response")
return s.writer.Encode(response)
}

type ListPromptsResult struct {
Prompts []protocol.Prompt `json:"prompts"`
NextCursor string `json:"nextCursor"`
}

type ListResourcesResult struct {
Resources []protocol.Resource `json:"resources"`
NextCursor string `json:"nextCursor"`
}

type ListToolsResult struct {
Tools []protocol.Tool `json:"tools"`
NextCursor string `json:"nextCursor"`
}

0 comments on commit bf47934

Please sign in to comment.